Reshaping/removing dimension while keeping location of elements unchanged

Hello,

I would like to reshape a tensor (or alter the dimension of the tensor) such that the elements do not change location. Here is a simple example:

torch.Size([2, 2, 2, 2])
tensor([[[[1., 0.],
          [0., 0.]],

         [[0., 1.],
          [0., 0.]]],


        [[[0., 0.],
          [2., 0.]],

         [[0., 0.],
          [0., 2.]]]], dtype=torch.float64)

This is a 2x2 tensor of 2x2 tensors, which looks like the left tensor in the image. I would like the tensor to become a 4x4 tensor, which looks like the right tensor in the image:

I have not been getting the desired result with view, reshape or permute
torch.reshape(z,(4,4)) gives the following which positions the element incorrectly.

 reshape: 

tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 2., 0.],
        [0., 0., 0., 2.]], dtype=torch.float64)

I need to this for a 1600x9x1600x2 tensor, to make it a 1600^2 x 18 tensor.

Thank you in advance!

Hi Rajiv!

permute() followed by reshape() will do the trick:

>>> import torch
>>> torch.__version__
'1.10.2'
>>>
>>> s = torch.tensor([[[[1., 0.],
...           [0., 0.]],
...          [[0., 1.],
...           [0., 0.]]],
...         [[[0., 0.],
...           [2., 0.]],
...          [[0., 0.],
...           [0., 2.]]]], dtype=torch.float64)
>>> s.permute (0, 2, 1, 3).reshape (4, 4)
tensor([[1., 0., 0., 1.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [2., 0., 0., 2.]], dtype=torch.float64)
>>>
>>> t = torch.randn (1600, 9, 1600, 2)
>>> t.permute (0, 2, 1, 3).reshape (1600**2, 18).shape
torch.Size([2560000, 18])

Best.

K. Frank

Thank you sir! @KFrank