Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a mask of diagonal matrix, but starting from the 2nd column?

So here is what I can get with torch.eye(3,4) now

The matrix I get:

[[1, 0, 0, 0],
 [0, 1, 0, 0],
 [0, 0, 1, 0]]

Is there any (easy)way to transform it, or make such a mask in this format:

The matrix I want:

[[0, 1, 0, 0],
 [0, 0, 1, 0],
 [0, 0, 0, 1]]
like image 554
ashered Avatar asked Nov 24 '25 10:11

ashered


1 Answers

You can do it by using torch.diagonal and specifying the diagonal you want:

>>> torch.diag(torch.tensor([1,1,1]), diagonal=1)[:-1]

tensor([[0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]])
  • If :attr:diagonal = 0, it is the main diagonal.
  • If :attr:diagonal > 0, it is above the main diagonal.
  • If :attr:diagonal < 0, it is below the main diagonal.
like image 134
Andreas K. Avatar answered Nov 27 '25 01:11

Andreas K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!