DEV Community

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

Posted on

abs() and sqrt() in PyTorch

*My post explains gcd() and lcm().

abs() can get zero or more absolute values from a 0D or more D tensor as shown below:

*Memos:

  • abs() 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).
  • If the type of input is complex, float is returned.
  • absolute() is the alias of abs().
import torch

my_tensor = torch.tensor(7)

torch.abs(input=my_tensor)
my_tensor.abs()
# tensor(7)

my_tensor = torch.tensor([7, -1, 5, -7, -9, -3, 0, 6])

torch.abs(input=my_tensor)
# tensor([7, 1, 5, 7, 9, 3, 0, 6])

my_tensor = torch.tensor([[-7, 1, -5, 7],
                          [9, -3, 0, -6]])
torch.abs(input=my_tensor)
# tensor([[7, 1, 5, 7],
#         [9, 3, 0, 6]])

my_tensor = torch.tensor([[7, -1, 5, -7],
                          [-9, -3, 0, 6]])
torch.abs(input=my_tensor)
# tensor([[[7, 1], [5, 7]],
#         [[9, 3], [0, 6]]])

my_tensor = torch.tensor([[[7., -1.], [5., -7.]],
                          [[-9., -3.], [0., 6.]]])
torch.abs(input=my_tensor)
# tensor([[[7., 1.], [5., 7.]],
#         [[9., 3.], [0., 6.]]])

my_tensor = torch.tensor([[[7.+0.j, -1.+0.j], [5.+0.j, -7.+0.j]],
                          [[-9.+0.j, -3.+0.j], [0.+0.j, 6.+0.j]]])
torch.abs(input=my_tensor)
# tensor([[[7., 1.], [5., 7.]],
#         [[9., 3.], [0., 6.]]])
Enter fullscreen mode Exit fullscreen mode

sqrt() can get zero or more square-roots(float) from a 0D or more D tensor as shown below:

*Memos:

  • sqrt() 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).
  • If the type of input is complex, float is returned.
import torch

my_tensor = torch.tensor(7)

torch.sqrt(input=my_tensor)
my_tensor.sqrt()
# tensor(2.6458)

my_tensor = torch.tensor([7, -1, 5, -7, -9, -3, 0, 6])

torch.sqrt(input=my_tensor)
# tensor([2.6458, nan, 2.2361, nan, nan, nan, 0.0000, 2.4495])

my_tensor = torch.tensor([[7, -1, 5, -7],
                          [-9, -3, 0, 6]])
torch.sqrt(input=my_tensor)
# tensor([[2.6458, nan, 2.2361, nan],
#         [nan, nan, 0.0000, 2.4495]])

my_tensor = torch.tensor([[[7, -1],
                           [5, -7]],
                          [[-9, -3],
                           [0, 6]]])
torch.sqrt(input=my_tensor)
# tensor([[[2.6458, nan],
#          [2.2361, nan]],
#         [[nan, nan],
#          [0.0000, 2.4495]]])

my_tensor = torch.tensor([[[7., -1.],
                           [5., -7.]],
                          [[-9., -3.],
                           [0., 6.]]])
torch.sqrt(input=my_tensor)
# tensor([[[2.6458, nan],
#          [2.2361, nan]],
#         [[nan, nan],
#          [0.0000, 2.4495]]])

my_tensor = torch.tensor([[[7.+0.j, -1.+0.j],
                           [5.+0.j, -7.+0.j]],
                          [[-9.+0.j, -3.+0.j],
                           [0.+0.j, 6.+0.j]]])
torch.sqrt(input=my_tensor)
# tensor([[[2.6458+0.0000j, 0.0000+1.0000j],
#          [2.2361+0.0000j, 0.0000+2.6458j]],
#         [[0.0000+3.0000j, 0.0000+1.7321j],
#          [0.0000+0.0000j, 2.4495+0.0000j]]])

my_tensor = torch.tensor([[[True, False],
                           [True, False]],
                          [[False, True],
                           [False, True]]])
torch.sqrt(input=my_tensor)
# tensor([[[1., 0.],
#          [1., 0.]],
#         [[0., 1.],
#          [0., 1.]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)