DEV Community

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

Posted on • Edited on

abs and sqrt in PyTorch

Buy Me a Coffee

*Memos:

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

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay