Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

trace, reciprocal and rsqrt in PyTorch

Buy Me a Coffee

*Memos:

trace() can get the 0D tensor of the sum of the zero or more elements of diagonal from the 2D tensor of zero or more elements as shown below:

*Memos:

  • trace() can be used withtorch or a tensor.
  • The 1st argument(input) withtorch or using a tensor(Required-Type:tensor ofint,float orcomplex).
importtorchmy_tensor=torch.tensor([[0,1,2],[3,4,5],[6,7,8]])torch.trace(input=my_tensor)my_tensor.trace()# tensor(12)my_tensor=torch.tensor([[0.,1.,2.],[3.,4.,5.],[6.,7.,8.]])torch.trace(input=my_tensor)# tensor(12.)my_tensor=torch.tensor([[0.+0.j,1.+0.j,2.+0.j],[3.+0.j,4.+0.j,5.+0.j],[6.+0.j,7.+0.j,8.+0.j]])torch.trace(input=my_tensor)# tensor(12.+0.j)my_tensor=torch.tensor([[0,1,2,3],[4,5,6,7],[8,9,10,11]])torch.trace(input=my_tensor)# tensor(15)my_tensor=torch.tensor([[0,1,2],[3,4,5]])torch.trace(input=my_tensor)# tensor(4)my_tensor=torch.tensor([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])torch.trace(input=my_tensor)# tensor(12)my_tensor=torch.tensor([[]])torch.trace(input=my_tensor)# tensor(0.)
Enter fullscreen modeExit fullscreen mode

reciprocal() can get the 0D or more D tensor of zero or more reciprocals from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • reciprocal() can be used withtorch or a tensor.
  • The 1st argument(input) withtorch or using a tensor(Required-Type:tensor ofint,float,complex orbool).
  • There isout argument withtorch(Optional-Default:None-Type:tensor):*Memos:
    • out= must be used.
    • My post explainsout argument.
  • reciprocal() returns afloat type tensor except wheninput or a tensor is acomplex type tensor.
importtorchmy_tensor=torch.tensor(-4.)torch.reciprocal(input=my_tensor)my_tensor.reciprocal()# tensor(-0.2500)my_tensor=torch.tensor([-4.,-3.,-2.,-1.,0.,1.,2.,3.])torch.reciprocal(input=my_tensor)# tensor([-0.2500, -0.3333, -0.5000, -1.0000,inf,1.0000,0.5000,0.3333])my_tensor=torch.tensor([[-4.,-3.,-2.,-1.],[0.,1.,2.,3.]])torch.reciprocal(input=my_tensor)# tensor([[-0.2500, -0.3333, -0.5000, -1.0000],#         [inf, 1.0000, 0.5000, 0.3333]])my_tensor=torch.tensor([[[-4.,-3.],[-2.,-1.]],[[0.,1.],[2.,3.]]])torch.reciprocal(input=my_tensor)# tensor([[[-0.2500, -0.3333], [-0.5000, -1.0000]],#         [[inf, 1.0000], [0.5000, 0.3333]]])my_tensor=torch.tensor([[[-4,-3],[-2,-1]],[[0,1],[2,3]]])torch.reciprocal(input=my_tensor)# tensor([[[-0.2500, -0.3333], [-0.5000, -1.0000]],#         [[inf, 1.0000], [0.5000, 0.3333]]])my_tensor=torch.tensor([[[-4.+0.j,-3.+0.j],[-2.+0.j,-1.+0.j]],[[0.+0.j,1.+0.j],[2.+0.j,3.+0.j]]])torch.reciprocal(input=my_tensor)# tensor([[[-0.2500-0.j, -0.3333-0.j], [-0.5000-0.j, -1.0000-0.j]],#         [[nan+nanj, 1.0000-0.j], [ 0.5000-0.j, 0.3333-0.j]]])my_tensor=torch.tensor([[[True,False],[True,False]],[[False,True],[False,True]]])torch.reciprocal(input=my_tensor)# tensor([[[1., inf], [1., inf]],#         [[inf, 1.], [inf, 1.]]])
Enter fullscreen modeExit fullscreen mode

rsqrt() can get the 0D or more D tensor of the zero or more reciprocals of square root from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • rsqrt() can be used withtorch or a tensor.
  • The 1st argument(input) withtorch or using a tensor(Required-Type:tensor ofint,float,complex orbool).
  • There isout argument withtorch(Optional-Default:None-Type:tensor):*Memos:
    • out= must be used.
    • My post explainsout argument.
  • rsqrt() returns afloat type tensor except wheninput or a tensor is acomplex type tensor.
importtorchmy_tensor=torch.tensor(-3.)torch.rsqrt(input=my_tensor)my_tensor.rsqrt()# tensor(nan)my_tensor=torch.tensor([-3.,-2.,-1.,0.,1.,2.,3.,4.])torch.rsqrt(input=my_tensor)# tensor([nan, nan, nan, inf, 1.0000, 0.7071, 0.5774, 0.5000])my_tensor=torch.tensor([[-3.,-2.,-1.,0.],[1.,2.,3.,4.]])torch.rsqrt(input=my_tensor)# tensor([[nan, nan, nan, inf],#         [1.0000, 0.7071, 0.5774, 0.5000]])my_tensor=torch.tensor([[[-3.,-2.],[-1.,0.]],[[1.,2.],[3.,4.]]])torch.rsqrt(input=my_tensor)# tensor([[[nan, nan],#          [nan, inf]],#         [[1.0000, 0.7071],#          [0.5774, 0.5000]]])my_tensor=torch.tensor([[[-3,-2],[-1,0]],[[1,2],[3,4]]])torch.rsqrt(input=my_tensor)# tensor([[[nan, nan],#          [nan, inf]],#         [[1.0000, 0.7071],#          [0.5774, 0.5000]]])my_tensor=torch.tensor([[[-3.+0.j,-2.+0.j],[-1.+0.j,0.+0.j]],[[1.+0.j,2.+0.j],[3.+0.j,4.+0.j]]])torch.rsqrt(input=my_tensor)# tensor([[[0.0000-0.5774j, 0.0000-0.7071j],#          [0.0000-1.0000j, nan+nanj]],#         [[1.0000-0.0000j, 0.7071-0.0000j],#          [0.5774-0.0000j, 0.5000-0.0000j]]])my_tensor=torch.tensor([[[True,False],[True,False]],[[False,True],[False,True]]])torch.rsqrt(input=my_tensor)# tensor([[[1., inf],#          [1., inf]],#         [[inf, 1.],#          [inf, 1.]]])
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a web developer.Buy Me a Coffee: ko-fi.com/superkaiSO: stackoverflow.com/users/3247006/super-kai-kazuya-itoX(Twitter): twitter.com/superkai_kazuyaFB: facebook.com/superkai.kazuya
  • Joined

More fromSuper Kai (Kazuya Ito)

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp