# Multi-dimensional slicing

**URL:** <https://discuss.pytorch.org/t/multi-dimensional-slicing/128230>\
**Category:** Uncategorized\
**Created:** [August 1, 2021, 3:41am UTC](https://discuss.pytorch.org/t/multi-dimensional-slicing/128230 "2021-08-01T03:41:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NicoAdamo](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/ac91a4/32.png) [@NicoAdamo](https://discuss.pytorch.org/u/NicoAdamo)\
**Post date:** [August 1, 2021, 3:41am UTC](https://discuss.pytorch.org/t/multi-dimensional-slicing/128230/1 "2021-08-01T03:41:22Z")

</div>

I have a tensor of indices \> 0 and \< 256, of shape `[1,5,8]`, and a tensor of images of shape `[1,5,256,3,84,84]`. All I want to do is create a new tensor with the images at those indices - i.e. a tensor of size `[1,5,8,3,84,84]`. But nothing I do seems to work. torch.index\_select seems to do this across one axis, but not multiple, and torch.gather would work, except it requires the index tensor and input tensor to be the same size. Do I really have to resort to a for loop here, or is there a nice way to do this?

---

<div class="post-metadata">

**Author:** ![nitaifingerhut](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nitaifingerhut/32/29872_2.png) [@nitaifingerhut](https://discuss.pytorch.org/u/nitaifingerhut)\
**Post date:** [August 1, 2021, 6:32am UTC](https://discuss.pytorch.org/t/multi-dimensional-slicing/128230/2 "2021-08-01T06:32:07Z")

</div>

[torch.gather](https://pytorch.org/docs/stable/generated/torch.gather.html) should do just this  
You don’t need them to have the same shape, or you can just I squeeze it to the proper dimension

---

<div class="post-metadata">

**Author:** ![NicoAdamo](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/ac91a4/32.png) [@NicoAdamo](https://discuss.pytorch.org/u/NicoAdamo)\
**Post date:** [August 1, 2021, 6:56am UTC](https://discuss.pytorch.org/t/multi-dimensional-slicing/128230/3 "2021-08-01T06:56:20Z")

</div>

Except torch.gather does require the same _number_ of dimensions. I assume that if I take the index tensor to size `[1,5,8,1,1,1]`, it’ll just give me the first pixel of each image, so how do I leave the images intact when taking the parts?
