DEV Community

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

Posted on • Edited on

abs and sqrt in PyTorch

Buy Me a Coffee

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

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

*Memos:

  • abs() 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). *If the type is complex, float is returned.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • 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 the 0D or more D tensor of zero or more square roots from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • sqrt() 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.
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)