I have a one-dimensional tensor a = torch.tensor([0,2,3,4])
.
From this tensor, I want to create another two-dimensional tensor which looks like this:
tensor([[1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1]])
The output tensor’s dimensions should be equal to (torch.sum(a), torch.sum(a))
which is (9,9)
. The tensor a
determines which elements of the output should be 1
. Since, the first two elements of a are 0
and 2
, the values in output[0:0+2, 0:0+2]
should be 1
. And output[2:2+3, 2:2+3]
should be 1
since the next two elements are 2
and 3
. And finally output[2+3:2+3+4, 2+3:2+3+4]
should be 1
.
If there an efficient way of doing this in PyTorch?