DEV Community

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

Posted on • Edited on

diagonal in PyTorch

Buy Me a Coffee

*Memos:

diagonal() can extract the view of 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 or a tensor.
  • The 1st argument(input) with torch or using a tensor(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).
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]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)