0

So I have a 4d tensor with shape [4,1,128,678] and I would like to view/reshape it as [4,678,128].

I have to do this for multiple tensors where the last shape value 678 is not always know and could be different, so [4,1,128,575]should also go to [4,575,128]

Any idea on what is the optimal operation to transform the tensor? view/reshape? and how?

Thanks

eljiwo
  • 687
  • 1
  • 8
  • 29

2 Answers2

1

You could also use (less to write and IMO cleaner):

# x.shape == (4, 1, 128, 678)
x.squeeze().permute(0, 2, 1)

If you were to use view you would lose dimension information (but maybe that is what you want), in this case it would be:

x.squeeze().view(4, -1, 128)

permute reorders tensors, while shape only gives a different view without restructuring underlying memory. You can see the difference between those two operations in this StackOverflow answer.

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
-1

Use einops instead, it can do all operations in one turn and verify known dimensions:

from einops import reshape
y = rearrange(x, 'x 1 y z -> x z y', x=4, y=128)
Alleo
  • 7,891
  • 2
  • 40
  • 30