trypag
(Pierre Antoine Ganaye)
October 25, 2017, 12:53pm
1
Hi,
Is there any equivalent in torch to indexing with list of slices. The point is to be able to index without depending on the number of dimension. Example with numpy :
>>> x = torch.arange(0, 25).view(5, 5).numpy()
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.],
[ 15., 16., 17., 18., 19.],
[ 20., 21., 22., 23., 24.]], dtype=float32)
>>> slicers=[slice(0,2), slice(0,2)]
>>> a[slicers]
array([[ 0., 1.],
[ 5., 6.]], dtype=float32)
Thanks
trypag
(Pierre Antoine Ganaye)
October 25, 2017, 12:55pm
2
I am not looking for a solution with slices specifically, just something that can index over dimensions, without dimension dependant writing.
albanD
(Alban D)
October 25, 2017, 1:02pm
3
Hi, just put your slices in a tuple and it will work:
>>> x = torch.arange(0, 25).view(5, 5)
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
[torch.FloatTensor of size 5x5]
>>> slicers=(slice(0,2), slice(0,2))
>>> x[slicers]
0 1
5 6
[torch.FloatTensor of size 2x2]
1 Like
trypag
(Pierre Antoine Ganaye)
October 25, 2017, 1:21pm
4
So simple, thanks @albanD