Reshaping/removing dimension while keeping location of elements unchanged

username: rs27
cooked: <p>Hello,</p>
<p>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:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>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:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/8/08ac16d00b47920ac7313557c73919a74f0fe3d6.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/08ac16d00b47920ac7313557c73919a74f0fe3d6" title="desired_transformation"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/8/08ac16d00b47920ac7313557c73919a74f0fe3d6_2_690x306.jpeg" alt="desired_transformation" data-base62-sha1="1eIwaYO3IfUm53ql9pqjlPg1m0C" width="690" height="306" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/8/08ac16d00b47920ac7313557c73919a74f0fe3d6_2_690x306.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/0/8/08ac16d00b47920ac7313557c73919a74f0fe3d6.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/8/08ac16d00b47920ac7313557c73919a74f0fe3d6.jpeg 2x" data-dominant-color="A7A7A0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">desired_transformation</span><span class="informations">871×387 38.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I have not been getting the desired result with view, reshape or permute<br>
torch.reshape(z,(4,4)) gives the following which positions the element incorrectly.</p>
<pre><code class="lang-auto"> reshape: 

tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 2., 0.],
        [0., 0., 0., 2.]], dtype=torch.float64)
</code></pre>
<p>I need to this for a 1600x9x1600x2 tensor, to make it a 1600^2 x 18 tensor.</p>
<p>Thank you in advance!</p>

username: KFrank
cooked: <p>Hi Rajiv!</p>
<aside class="quote no-group" data-username="rs27" data-post="1" data-topic="148068" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/b782af/48.png" class="avatar"> rs27:</div>
<blockquote>
<pre><code class="lang-auto"> reshape: 

tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 2., 0.],
        [0., 0., 0., 2.]], dtype=torch.float64)
</code></pre>
</blockquote>
</aside>
<p><code>permute()</code> followed by <code>reshape()</code> will do the trick:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.2'
&gt;&gt;&gt;
&gt;&gt;&gt; s = torch.tensor([[[[1., 0.],
...           [0., 0.]],
...          [[0., 1.],
...           [0., 0.]]],
...         [[[0., 0.],
...           [2., 0.]],
...          [[0., 0.],
...           [0., 2.]]]], dtype=torch.float64)
&gt;&gt;&gt; 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)
&gt;&gt;&gt;
&gt;&gt;&gt; t = torch.randn (1600, 9, 1600, 2)
&gt;&gt;&gt; t.permute (0, 2, 1, 3).reshape (1600**2, 18).shape
torch.Size([2560000, 18])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

username: rs27
cooked: <p>Thank you sir! <a class="mention" href="/u/kfrank">@KFrank</a></p>