Create a view using subsampling

For example with a tensor

[ [0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]

create a view exposing

[ [0,2],[4,6],[8,10],[12,14]]

Obviously this is not a reshaping with AFAIK is all you can do with view. However, it doesn’t seem like it would be impossible so I though I would ask.

Direct indexing should work:

x = torch.tensor([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]])
y = x[:, ::2]
print(y)
# tensor([[ 0,  2],
#         [ 4,  6],
#         [ 8, 10],
#         [12, 14]])

y[0, 0].fill_(100)
print(x)
# tensor([[100,   1,   2,   3],
#         [  4,   5,   6,   7],
#         [  8,   9,  10,  11],
#         [ 12,  13,  14,  15]])
1 Like

Yes, that can be used to create the vector of indices, and gather can then be used to subsample.