How to slice a matrice in PyTorch

I know in tensorflow I can do it with tf.slice(my_tensor, begin, size)
Many thanks

In Torch / PyTorch you can use the narrow(dimension, start, length) method (ref.), which allows you to get a smaller range (length) of a given dimension at a specific starting point (so, you’ll end up with the same number of dimensions).
Use instead the select(dimension, index) method (ref.) to choose a specific index of one dimension (you’ll end up with one less number of dimensions).

2 Likes

Or better just use the regular indexing syntax, as if you were operating on numpy arrays: arr[0, 2:4] will select first slice along the first dimension and slices 2 and 3 from the second dimension.

7 Likes