How to concatenate a matrix in channle way by using `view`?

Igo312: <p>It’s hard to descript, so let’s see the example.  I get a tensor like below</p>
<pre><code class="lang-auto">[[[1,1],[1,1]],
[[2,2],[2,2]],
[[3,3],[3,3]],
[[4,4],[4,4]]]
</code></pre>
<p>the size is <code>(4,2,2)</code>. I want to concat diffrent channel and convert it into a new tensor with size <code>(1,4,4)</code>, but if using <code>view</code> it will like the left array, but I want to get the right format.</p>
<pre><code class="lang-auto">[[1,1,1,1],             [[1,1,2,2],
 [2,2,2,2],              [1,1,2,2],
 [3,3,3,3],              [3,3,4,4],
 [4,4,4,4]]              [3,3,4,4]]
</code></pre>
<p>Actually I would like a tensor whose size is <code>(N,C,H,W)</code> be converted into <code>(N, C//4, H*2, W*2)</code> in right format above format by using <code>view</code> instead using loop.<br>
What can I do? really appreciate your help!</p>

ptrblck: <p>This permutation should work:</p>
<pre><code class="lang-python">x = torch.tensor([[[1,1],[1,1]],
                  [[2,2],[2,2]],
                  [[3,3],[3,3]],
                  [[4,4],[4,4]]])

print(x.view(2, 2, 2, 2).permute(0, 2, 1, 3).contiguous().view(1, 4, 4))
&gt; tensor([[[1, 1, 2, 2],
           [1, 1, 2, 2],
           [3, 3, 4, 4],
           [3, 3, 4, 4]]])
</code></pre>
<p>Since you have repeated values, make sure that the values are indeed at the right position.</p>

Igo312: <aside class="quote no-group" data-username="Igo312" data-post="1" data-topic="113895">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/igo312/48/22283_2.png" class="avatar"> Igo312:</div>
<blockquote>
<pre><code class="lang-auto">[[[1,1],[1,1]],
[[2,2],[2,2]],
[[3,3],[3,3]],
[[4,4],[4,4]]]
</code></pre>
</blockquote>
</aside>
<p>I get what <a class="mention" href="/u/ptrblck">@ptrblck</a>  mean. here’s my new example</p>
<pre><code class="lang-auto">       # first element of batch
       [[[[1.1,1.2],[1.3,1.4]],
        [[2.1,2.2],[2.3,2.4]],
        [[3.1,3.2],[3.3,3.4]],
        [[4.1,4.2],[4.3,4.4]]],
       #  second element of batch
         [[[5.1,5.2],[5.3,5.4]],
        [[6.1,6.2],[6.3,6.4]],
        [[7.1,7.2],[7.3,7.4]],
        [[8.1,8.2],[8.3,8.4]]]]
</code></pre>
<p>And if I use</p>
<pre data-code-wrap="python"><code class="lang-python">N,C,H,W = a.size()
x.view(N,C//2,C//2,H,W).permute(0,1,3,2,4).contiguous().view(N,C//4,H*2,W*2)
</code></pre>
<p>it will return</p>
<pre><code class="lang-auto">tensor([[[[1.1000, 1.2000, 2.1000, 2.2000],
          [1.3000, 1.4000, 2.3000, 2.4000],
          [3.1000, 3.2000, 4.1000, 4.2000],
          [3.3000, 3.4000, 4.3000, 4.4000]]],
        [[[5.1000, 5.2000, 6.1000, 6.2000],
          [5.3000, 5.4000, 6.3000, 6.4000],
          [7.1000, 7.2000, 8.1000, 8.2000],
          [7.3000, 7.4000, 8.3000, 8.4000]]]])
</code></pre>

Igo312: <p>sorry for bothering, there is another question.<br>
Now I want to get a new format.left is my input, right is my expected output.</p>
<pre><code class="lang-python"># first element of batch
[[[[1.1,1.2],[1.3,1.4]],                       [[[1.1, 2.1, 3.1, 4.1],
[[2.1,2.2],[2.3,2.4]],                          [[1.2, 2.2, 3.2, 4.2],
[[3.1,3.2],[3.3,3.4]],                          [[1.3, 2.3, 3.3, 4.3],
[[4.1,4.2],[4.3,4.4]]],                         [[1.4, 2.4, 3.4, 4.4]],
 #  second element of batch            
[[[5.1,5.2],[5.3,5.4]],                          [[5.1, 6.1, 7.1, 8.1],
[[6.1,6.2],[6.3,6.4]],                           [5.2, 6.2, 7.2, 8.3],
[[7.1,7.2],[7.3,7.4]],                           [5.3, 6.3, 7.3, 8.3],
[[8.1,8.2],[8.3,8.4]]]]                          [5.4, 6.4, 7.4, 8.4]]]
</code></pre>
<p><code>F.pixel_shuffle</code> is similar but my input size is <code>(N, C*4, H, W)</code> and convert it into <code>(N, C, H*2, W*2)</code> instead of <code>(N, R**2, H, W)</code> into <code>(N, R, H*R, W*R)</code><br>
relly thank you for your help!</p>

ptrblck: <p>This should work:</p>
<pre><code class="lang-python">x = torch.tensor([[[[1.1,1.2],[1.3,1.4]],
                   [[2.1,2.2],[2.3,2.4]],
                   [[3.1,3.2],[3.3,3.4]], 
                   [[4.1,4.2],[4.3,4.4]]],
                  [[[5.1,5.2],[5.3,5.4]],
                   [[6.1,6.2],[6.3,6.4]],
                   [[7.1,7.2],[7.3,7.4]],
                   [[8.1,8.2],[8.3,8.4]]]])

print(x.permute(0, 2, 3, 1).view(2, 4, 4))
&gt; tensor([[[1.1000, 2.1000, 3.1000, 4.1000],
           [1.2000, 2.2000, 3.2000, 4.2000],
           [1.3000, 2.3000, 3.3000, 4.3000],
           [1.4000, 2.4000, 3.4000, 4.4000]],

          [[5.1000, 6.1000, 7.1000, 8.1000],
           [5.2000, 6.2000, 7.2000, 8.2000],
           [5.3000, 6.3000, 7.3000, 8.3000],
           [5.4000, 6.4000, 7.4000, 8.4000]]])
</code></pre>

Igo312: <p>you are the best! <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
But is there need a <code>contiguous()</code> after <code>permute</code>. And how you work it out？right now I regard the array as a one-dimension  list and then reshape it.<br>
Anyway it really helps! thank you very  much!</p>

ptrblck: <p>I add the <code>contiguous()</code> calls, if the next <code>view</code> operation raises an error. Otherwise I assume PyTorch does the right thing and doesn’t need to create a contiguous copy of the tensor. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="Igo312" data-post="7" data-topic="113895">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/igo312/48/22283_2.png" class="avatar"> Igo312:</div>
<blockquote>
<p>And how you work it out？right now I regard the array as a one-dimension list and then reshape it.</p>
</blockquote>
</aside>
<p>Do you mean, how do I come up with the solution?<br>
If so, I don’t think there is a better way than having looked at various ways to reshape tensors in the past and just staring long enough at it until you realize which dimension has to move where.<br>
At least I’m not using any formal way and just trust my gut feeling.</p>

Igo312: <p>I get it! relly thanks!</p>