How to transform pytorch tensor to view such as ((value0, row0, column0), (value1, row0, column1)...)?
How do I transform PyTorch tensor to view such as ((value0, row0, column0), (value1, row0, column1)...)? For example, if I have a tensor
a = torch.tensor([[5, 7],
                  [9, 11]])

The function should return:
((5, 0, 0), (7, 0, 1), (9, 1, 0), (11, 1, 1)). Also, I want it function to be applied for constants, 1D, 3D, 4D, etc. tensors, e.g. for 3D I want it to output (value0, dim1, dim2, dim3).
I tried to use for loops but didn't realize how to use them for this task: I can make a function only for a constant number of dimensions, such as only for 2D.

welcome to StackOverflow !
If the assumption that your tensor contains only non-zero values, then I guess the easiest way is to use to_sparse :
a = torch.tensor([[5, 7], [9, 11]])
def coords(t):
    sp_t = t.to_sparse()
    idx = sp_t.indices().T
    vals = sp_t.values()
    return torch.cat([vals.view(-1,1), idx],1)
coords(a)
&gt;&gt;&gt; tensor([[ 5,  0,  0],
    [ 7,  0,  1],
    [ 9,  1,  0],
    [11,  1,  1]])

If that cannot be assumed, please tell me, a workaround with repeat and repeat_interleave should be possible
