Tensor slice in pytorch?

Is there any function that behaves similarly to the function of tf.slice in pytorch?

1 Like

torch.Tensor.narrow

3 Likes

I just found a similar post regarding using indexing to slice a tensor. So my question is “Is index slicing same as using narrow function?”

Thanks

If by index slicing you mean using a[2:5], then yes, it’s equivalent to applying narrows.

4 Likes

Thanks. This is what I mean.

I have a question regarding tensor slices. My variables vtf and a have torch.FloatTensor of size 5x25 and torch.LongTensor of size 5x5 respectively. When I do vtf[:,a[0,:]], it gives me torch.FloatTensor of size 5x5. However if I do vtf[0,a[0,:]] then I received TypeError: indexing a tensor with an object of type torch.LongTensor. The only supported types are integers, slices, numpy scalars and torch.LongTensor or torch.ByteTensor as the only argument.

Please let me know if I am doing something wrong

so, how do we slice the tensor become patches using narrow?