abs() and sqrt() in PyTorch
Tags: pytorch, abs, sqrt, function
Published: 2024-05-17 12:34:52

*[My post](https://dev.to/hyperkai/gcd-and-lcm-in-pytorch-a47) explains [gcd()](https://pytorch.org/docs/stable/generated/torch.gcd.html) and [lcm()](https://pytorch.org/docs/stable/generated/torch.lcm.html).

[abs()](https://pytorch.org/docs/stable/generated/torch.abs.html) 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](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float` or `complex`). *If the type of `input` is `complex`, `float` is returned.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- [absolute()](https://pytorch.org/docs/stable/generated/torch.absolute.html) is the alias of `abs()`.

```python
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.]]])
```

[sqrt()](https://pytorch.org/docs/stable/generated/torch.sqrt.html) 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 with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
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.]]])
```