DEV Community

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

Posted on • Edited on

trace, reciprocal and rsqrt in PyTorch

Buy Me a Coffee

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 with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or complex).
import torch

my_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 mode Exit 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 with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • reciprocal() returns a float type tensor except when input or a tensor is a complex type tensor.
import torch

my_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 mode Exit 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 with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • rsqrt() returns a float type tensor except when input or a tensor is a complex type tensor.
import torch

my_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 mode Exit fullscreen mode

Top comments (0)