diagonal() in PyTorch
Tags: pytorch, diagonal, tensor, function
Published: 2024-05-22 00:27:12

*Memos:
- [My post](https://dev.to/hyperkai/diagembed-in-pytorch-a2o) explains [diag_embed()](https://pytorch.org/docs/stable/generated/torch.diag_embed.html).
- [My post](https://dev.to/hyperkai/eye-in-pytorch-1mjj) explains [eye()](https://pytorch.org/docs/stable/generated/torch.eye.html).
- [My post](https://dev.to/hyperkai/diag-and-diagflat-in-pytorch-22n9) explains [diag()](https://pytorch.org/docs/stable/generated/torch.diag.html) and [diagflat()](https://pytorch.org/docs/stable/generated/torch.diagflat.html).

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

*Memos:
- `diagonal()` 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`, `complex` or `bool`).
- The 2nd argument with torch or the 1st argument with a tensor is `offset`(Optional-Default:`0`-Type:`int`).
- The 3rd argument with torch or the 2nd argument with a tensor is `dim1`(Optional-Default:`0`-Type:`int`).
- The 4th argument with torch or the 3rd argument with a tensor is `dim2`(Optional-Default:`1`-Type:`int`).

```python
import torch

my_tensor = torch.tensor([[7, -4, 5],
                          [-6, -3, 8],
                          [9, 1, -2]])
torch.diagonal(input=my_tensor)
my_tensor.diagonal()
torch.diagonal(input=my_tensor, offset=0, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=0, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=0, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=0, dim1=1, dim2=-2)
torch.diagonal(input=my_tensor, offset=0, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=0, dim1=-1, dim2=-2)
torch.diagonal(input=my_tensor, offset=0, dim1=-2, dim2=1)
torch.diagonal(input=my_tensor, offset=0, dim1=-2, dim2=-1)
# tensor([7, -3, -2])

torch.diagonal(input=my_tensor, offset=1, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=1, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=1, dim1=-2, dim2=1)
torch.diagonal(input=my_tensor, offset=1, dim1=-2, dim2=-1)
torch.diagonal(input=my_tensor, offset=-1, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=-1, dim1=1, dim2=-2)
torch.diagonal(input=my_tensor, offset=-1, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=-1, dim1=-1, dim2=-2)
# tensor([-4, 8])

torch.diagonal(input=my_tensor, offset=1, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=1, dim1=1, dim2=-2)
torch.diagonal(input=my_tensor, offset=1, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=1, dim1=-1, dim2=-2)
torch.diagonal(input=my_tensor, offset=-1, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=-1, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=-1, dim1=-2, dim2=1)
torch.diagonal(input=my_tensor, offset=-1, dim1=-2, dim2=-1)
# tensor([-6, 1])

torch.diagonal(input=my_tensor, offset=2, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=2, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=2, dim1=-2, dim2=1)
torch.diagonal(input=my_tensor, offset=2, dim1=-2, dim2=-1)
torch.diagonal(input=my_tensor, offset=-2, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=-2, dim1=1, dim2=-2)
torch.diagonal(input=my_tensor, offset=-2, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=-2, dim1=-1, dim2=-2)
# tensor([5])

torch.diagonal(input=my_tensor, offset=2, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=2, dim1=1, dim2=-2)
torch.diagonal(input=my_tensor, offset=2, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=2, dim1=-1, dim2=-2)
torch.diagonal(input=my_tensor, offset=-2, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=-2, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=-2, dim1=-2, dim2=1)
torch.diagonal(input=my_tensor, offset=-2, dim1=-2, dim2=-1)
# tensor([9])

my_tensor = torch.tensor([[7., -4., 5.],
                          [-6., -3., 8.],
                          [9., 1., -2.]])
torch.diagonal(input=my_tensor)
# tensor([7., -3., -2.])

my_tensor = torch.tensor([[7.+0.j, -4.+0.j, 5.+0.j],
                          [-6.+0.j, -3.+0.j, 8.+0.j],
                          [9.+0.j, 1.+0.j, -2.+0.j]])
torch.diagonal(input=my_tensor)
# tensor([7.+0.j, -3.+0.j, -2.+0.j])

my_tensor = torch.tensor([[False, True, True],
                          [True, False, True],
                          [True, True, False]])
torch.diagonal(input=my_tensor)
# tensor([False, False, False])

my_tensor = torch.tensor([[[7, -4, 5], [-6, -3, 8], [9, 1, -2]],
                          [[3, -1, 8], [0, 1, 6], [-7, 4, -9]],
                          [[6, -8, -9], [-4, 5, 0], [-3, -5, 2]]])
torch.diagonal(input=my_tensor)
torch.diagonal(input=my_tensor, offset=0, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=0, dim1=0, dim2=-2)
torch.diagonal(input=my_tensor, offset=0, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=0, dim1=1, dim2=-3)
torch.diagonal(input=my_tensor, offset=0, dim1=-2, dim2=0)
torch.diagonal(input=my_tensor, offset=0, dim1=-2, dim2=-3)
torch.diagonal(input=my_tensor, offset=0, dim1=-3, dim2=1)
torch.diagonal(input=my_tensor, offset=0, dim1=-3, dim2=-2)
# tensor([[7, 0, -3],
#         [-4, 1, -5],
#         [5, 6, 2]])

torch.diagonal(input=my_tensor, offset=0, dim1=0, dim2=2)
torch.diagonal(input=my_tensor, offset=0, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=0, dim1=2, dim2=0)
torch.diagonal(input=my_tensor, offset=0, dim1=2, dim2=-3)
torch.diagonal(input=my_tensor, offset=0, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=0, dim1=-1, dim2=-3)
torch.diagonal(input=my_tensor, offset=0, dim1=-3, dim2=2)
torch.diagonal(input=my_tensor, offset=0, dim1=-3, dim2=-1)
# tensor([[7, -1, -9],
#         [-6, 1, 0],
#         [9, 4, 2]])

torch.diagonal(input=my_tensor, offset=0, dim1=1, dim2=2)
torch.diagonal(input=my_tensor, offset=0, dim1=1, dim2=-1)
torch.diagonal(input=my_tensor, offset=0, dim1=2, dim2=1)
torch.diagonal(input=my_tensor, offset=0, dim1=2, dim2=-2)
torch.diagonal(input=my_tensor, offset=0, dim1=-1, dim2=1)
torch.diagonal(input=my_tensor, offset=0, dim1=-1, dim2=-2)
torch.diagonal(input=my_tensor, offset=0, dim1=-2, dim2=2)
torch.diagonal(input=my_tensor, offset=0, dim1=-2, dim2=-1)
# tensor([[7, -3, -2],
#         [3, 1, -9],
#         [6, 5, 2]])

torch.diagonal(input=my_tensor, offset=1, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=1, dim1=0, dim2=-2)
torch.diagonal(input=my_tensor, offset=1, dim1=-3, dim2=1)
torch.diagonal(input=my_tensor, offset=1, dim1=-3, dim2=-2)
torch.diagonal(input=my_tensor, offset=-1, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=-1, dim1=1, dim2=-3)
torch.diagonal(input=my_tensor, offset=-1, dim1=-2, dim2=0)
torch.diagonal(input=my_tensor, offset=-1, dim1=-2, dim2=-3)
# tensor([[-6, -7],
#         [-3, 4],
#         [8, -9]])

torch.diagonal(input=my_tensor, offset=1, dim1=0, dim2=2)
torch.diagonal(input=my_tensor, offset=1, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=1, dim1=-3, dim2=2)
torch.diagonal(input=my_tensor, offset=1, dim1=-3, dim2=-1)
torch.diagonal(input=my_tensor, offset=-1, dim1=2, dim2=0)
torch.diagonal(input=my_tensor, offset=-1, dim1=2, dim2=-3)
torch.diagonal(input=my_tensor, offset=-1, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=-1, dim1=-1, dim2=-3)
# tensor([[-4, 8],
#         [-3, 6],
#         [1, -9]])

torch.diagonal(input=my_tensor, offset=1, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=1, dim1=1, dim2=-3)
torch.diagonal(input=my_tensor, offset=1, dim1=-2, dim2=0)
torch.diagonal(input=my_tensor, offset=1, dim1=-2, dim2=-3)
torch.diagonal(input=my_tensor, offset=-1, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=-1, dim1=0, dim2=-2)
torch.diagonal(input=my_tensor, offset=-1, dim1=-3, dim2=1)
torch.diagonal(input=my_tensor, offset=-1, dim1=-3, dim2=-2)
# tensor([[3, -4],
#         [-1, 5],
#         [8, 0]])

torch.diagonal(input=my_tensor, offset=1, dim1=1, dim2=2)
torch.diagonal(input=my_tensor, offset=1, dim1=1, dim2=-1)
torch.diagonal(input=my_tensor, offset=1, dim1=-2, dim2=2)
torch.diagonal(input=my_tensor, offset=1, dim1=-2, dim2=-1)
torch.diagonal(input=my_tensor, offset=-1, dim1=2, dim2=1)
torch.diagonal(input=my_tensor, offset=-1, dim1=2, dim2=-2)
torch.diagonal(input=my_tensor, offset=-1, dim1=-1, dim2=1)
torch.diagonal(input=my_tensor, offset=-1, dim1=-1, dim2=-2)
# tensor([[-4, 8],
#         [-1, 6],
#         [-8, 0]])

torch.diagonal(input=my_tensor, offset=1, dim1=2, dim2=0)
torch.diagonal(input=my_tensor, offset=1, dim1=2, dim2=-3)
torch.diagonal(input=my_tensor, offset=1, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=1, dim1=-1, dim2=-3)
torch.diagonal(input=my_tensor, offset=-1, dim1=0, dim2=2)
torch.diagonal(input=my_tensor, offset=-1, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=-1, dim1=-3, dim2=2)
torch.diagonal(input=my_tensor, offset=-1, dim1=-3, dim2=-1)
# tensor([[3, -8],
#         [0, 5],
#         [-7, -5]])

torch.diagonal(input=my_tensor, offset=1, dim1=2, dim2=1)
torch.diagonal(input=my_tensor, offset=1, dim1=2, dim2=-2)
torch.diagonal(input=my_tensor, offset=1, dim1=-1, dim2=1)
torch.diagonal(input=my_tensor, offset=1, dim1=-1, dim2=-2)
torch.diagonal(input=my_tensor, offset=-1, dim1=1, dim2=2)
torch.diagonal(input=my_tensor, offset=-1, dim1=1, dim2=-1)
torch.diagonal(input=my_tensor, offset=-1, dim1=-2, dim2=2)
torch.diagonal(input=my_tensor, offset=-1, dim1=-2, dim2=-1)
# tensor([[-6, 1],
#         [0, 4],
#         [-4, -5]])

torch.diagonal(input=my_tensor, offset=2, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=2, dim1=0, dim2=-2)
torch.diagonal(input=my_tensor, offset=2, dim1=-3, dim2=1)
torch.diagonal(input=my_tensor, offset=2, dim1=-3, dim2=-2)
torch.diagonal(input=my_tensor, offset=-2, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=-2, dim1=1, dim2=-3)
torch.diagonal(input=my_tensor, offset=-2, dim1=-2, dim2=0)
torch.diagonal(input=my_tensor, offset=-2, dim1=-2, dim2=-3)
# tensor([[9],
#         [1],
#         [-2]])

torch.diagonal(input=my_tensor, offset=2, dim1=0, dim2=2)
torch.diagonal(input=my_tensor, offset=2, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=2, dim1=-3, dim2=2)
torch.diagonal(input=my_tensor, offset=2, dim1=-3, dim2=-1)
torch.diagonal(input=my_tensor, offset=-2, dim1=2, dim2=0)
torch.diagonal(input=my_tensor, offset=-2, dim1=2, dim2=-3)
torch.diagonal(input=my_tensor, offset=-2, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=-2, dim1=-1, dim2=-3)
# tensor([[5],
#         [8],
#         [-2]])

torch.diagonal(input=my_tensor, offset=2, dim1=1, dim2=0)
torch.diagonal(input=my_tensor, offset=2, dim1=1, dim2=-3)
torch.diagonal(input=my_tensor, offset=2, dim1=-2, dim2=0)
torch.diagonal(input=my_tensor, offset=2, dim1=-2, dim2=-3)
torch.diagonal(input=my_tensor, offset=-2, dim1=0, dim2=1)
torch.diagonal(input=my_tensor, offset=-2, dim1=0, dim2=-2)
torch.diagonal(input=my_tensor, offset=-2, dim1=-3, dim2=1)
torch.diagonal(input=my_tensor, offset=-2, dim1=-3, dim2=-2)
# tensor([[6],
#         [-8],
#         [-9]])

torch.diagonal(input=my_tensor, offset=2, dim1=1, dim2=2)
torch.diagonal(input=my_tensor, offset=2, dim1=1, dim2=-1)
torch.diagonal(input=my_tensor, offset=2, dim1=-2, dim2=2)
torch.diagonal(input=my_tensor, offset=2, dim1=-2, dim2=-1)
torch.diagonal(input=my_tensor, offset=-2, dim1=2, dim2=1)
torch.diagonal(input=my_tensor, offset=-2, dim1=2, dim2=-2)
torch.diagonal(input=my_tensor, offset=-2, dim1=-1, dim2=1)
torch.diagonal(input=my_tensor, offset=-2, dim1=-1, dim2=-2)
# tensor([[5],
#         [8],
#         [-9]])

torch.diagonal(input=my_tensor, offset=2, dim1=2, dim2=0)
torch.diagonal(input=my_tensor, offset=2, dim1=2, dim2=-3)
torch.diagonal(input=my_tensor, offset=2, dim1=-1, dim2=0)
torch.diagonal(input=my_tensor, offset=2, dim1=-1, dim2=-3)
torch.diagonal(input=my_tensor, offset=-2, dim1=0, dim2=2)
torch.diagonal(input=my_tensor, offset=-2, dim1=0, dim2=-1)
torch.diagonal(input=my_tensor, offset=-2, dim1=-3, dim2=2)
torch.diagonal(input=my_tensor, offset=-2, dim1=-3, dim2=-1)
# tensor([[6],
#         [-4],
#         [-3]])

torch.diagonal(input=my_tensor, offset=2, dim1=2, dim2=1)
torch.diagonal(input=my_tensor, offset=2, dim1=2, dim2=-2)
torch.diagonal(input=my_tensor, offset=2, dim1=-1, dim2=1)
torch.diagonal(input=my_tensor, offset=2, dim1=-1, dim2=-2)
torch.diagonal(input=my_tensor, offset=-2, dim1=1, dim2=2)
torch.diagonal(input=my_tensor, offset=-2, dim1=1, dim2=-1)
torch.diagonal(input=my_tensor, offset=-2, dim1=-2, dim2=2)
torch.diagonal(input=my_tensor, offset=-2, dim1=-2, dim2=-1)
# tensor([[9],
#         [-7],
#         [-3]])
```