Output result is:
tensor([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
Example
import torch
# Extract Diagonal Elements from Matrix
x = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
# Extract Main Diagonal
y = torch.diag(x)
print(y)
Output result is:
tensor([1, 5, 9])
YouTip