trace(), reciprocal() and rsqrt() in PyTorch
Tags: pytorch, trace, reciprocal, rsqrt
Published: 2024-05-02 22:09:19

[trace()](https://pytorch.org/docs/stable/generated/torch.trace.html) can get the 0D tensor of the sum of the zero or more elements of diagonal from the 2D tensor of zero or more elements as shown below:

*Memos:
- `trace()` 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`).

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

[reciprocal()](https://pytorch.org/docs/stable/generated/torch.reciprocal.html) can get the 0D or more D tensor of zero or more reciprocals from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `reciprocal()` 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.
- `reciprocal()` returns a `float` type tensor except when`input` is a `complex` type tensor.

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

[rsqrt()](https://pytorch.org/docs/stable/generated/torch.rsqrt.html) can get the 0D or more D tensor of the zero or more reciprocals of square root from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `rsqrt()` 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.
- `rsqrt()` returns a `float` type tensor except when`input` is a `complex` type tensor.

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