DEV Community

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

Posted on • Updated on

trace(), reciprocal() and rsqrt() in PyTorch

trace() can sum of the zero or more values of the diagonal of a 2D tensor as shown below:

*Memos:

  • trace() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or complex) with torch or using a tensor(tensor of int, float or complex) is input(Required).
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 zero or more reciprocals of the zero or more values of a 0D or more D tensor as shown below:

*Memos:

  • reciprocal() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
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 zero or more reciprocals of the zero or more square-roots of the zero or more values of a 0D or more D tensor as shown below:

*Memos:

  • rsqrt() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
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)