4

I have a torch tensor of size torch.Size([1, 128, 56, 128])

1 is channel, 128 is the width, and height. 56 are the stacks of images.

How can I resize it to torch.Size([1, 56, 128, 128]) ?

iacob
  • 20,084
  • 6
  • 92
  • 119
Stefann
  • 65
  • 1
  • 1
  • 5
  • Do you want to switch the dimensions so that the image stack dimension becomes the 2nd dimension? Or do you want to reshape the tensor so that you have one channel, 56 width, 128 images, and 128 height? – Cecilia May 07 '20 at 22:40
  • 7
    Does this answer your question? [How I can swap 3 dimensions with each other in Pytorch?](https://stackoverflow.com/questions/61104138/how-i-can-swap-3-dimensions-with-each-other-in-pytorch) – Cecilia May 07 '20 at 22:41
  • @Cecilia Thankyou so much, it worked :) – Stefann May 07 '20 at 23:13

2 Answers2

3

You could simply use permute or transpose.

iacob
  • 20,084
  • 6
  • 92
  • 119
geekbird
  • 56
  • 1
  • 5
3

You can use the swapaxes function:

my_tensor.swapaxes(1,2)
iacob
  • 20,084
  • 6
  • 92
  • 119