Concatenate given dimensions in a tensor

username: Yann
cooked: <p>Hello,</p>
<p>I am trying to find an efficient solution for the following problem and it seems I cannot get it right.</p>
<p>I have a tensor like:</p>
<p>tensor([[[ 1,  2,  3],<br>
[ 4,  5,  6],<br>
[ 7,  8,  9],<br>
[10, 11, 12]],<br>
[[13, 14, 15],<br>
[16, 17, 18],<br>
[19, 20, 21],<br>
[22, 23, 24]]])</p>
<p>I want to turn this into:</p>
<p>tensor([[ 1,  2,  3, 13, 14, 15],<br>
[ 4,  5,  6, 16, 17, 18],<br>
[ 7,  8,  9, 19, 20, 21],<br>
[10, 11, 12, 22, 23, 24]])</p>
<p>In a computationally efficient fashion because this happens intensively in my application.</p>
<p>Could you help me please?<br>
Thanks!</p>
<p>PS: I have this solution so far but it is looks suboptimal:<br>
torch.cat(tuple(t[:]),1)</p>

username: KFrank
cooked: <p>Hi Yann!</p>
<aside class="quote no-group" data-username="Yann" data-post="1" data-topic="127031" data-full="true">
<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/yann/48/12518_2.png" class="avatar"> Yann:</div>
<blockquote>
<p>I want to turn this into:</p>
<p>tensor([[ 1,  2,  3, 13, 14, 15],<br>
[ 4,  5,  6, 16, 17, 18],<br>
[ 7,  8,  9, 19, 20, 21],<br>
[10, 11, 12, 22, 23, 24]])</p>
<p>In a computationally efficient fashion because this happens intensively in my application.</p>
</blockquote>
</aside>
<p>I think that, in general, your solution is the best you can do.  The<br>
issue is that if your original tensor is <em>contiguous,</em> there is no way<br>
you can construct your desired tensor as a view into your original<br>
tensor because there is no way you can get the elements in the<br>
order you want by “striding” through the elements of the original<br>
tensor.  So you have to make a copy of the elements of the original<br>
tensor in order to put them in the desired order.</p>
<p>Having said that, it would be possible to get your desired tensor<br>
without a copy in the unlikely event that your original tensor is<br>
not contiguous and happens to have its elements in an order from<br>
which you can create your desired tensor by striding.</p>
<p>For example:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.9.0'
&gt;&gt;&gt;
&gt;&gt;&gt; t = torch.arange (24).reshape (2, 4, 3) + 1
&gt;&gt;&gt; t
tensor([[[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9],
         [10, 11, 12]],

        [[13, 14, 15],
         [16, 17, 18],
         [19, 20, 21],
         [22, 23, 24]]])
&gt;&gt;&gt; t.is_contiguous()
True
&gt;&gt;&gt; t_result = torch.cat (t.split (1), dim = -1).squeeze()
&gt;&gt;&gt; t_result
tensor([[ 1,  2,  3, 13, 14, 15],
        [ 4,  5,  6, 16, 17, 18],
        [ 7,  8,  9, 19, 20, 21],
        [10, 11, 12, 22, 23, 24]])
&gt;&gt;&gt; t_special = torch.as_strided (t_result, (2, 4, 3), (3, 6, 1))
&gt;&gt;&gt; t_special
tensor([[[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9],
         [10, 11, 12]],

        [[13, 14, 15],
         [16, 17, 18],
         [19, 20, 21],
         [22, 23, 24]]])
&gt;&gt;&gt; t_special.is_contiguous()
False
&gt;&gt;&gt; t_result_as_strided = torch.as_strided (t_special, (4, 6), (6, 1))
&gt;&gt;&gt; t_result_as_strided
tensor([[ 1,  2,  3, 13, 14, 15],
        [ 4,  5,  6, 16, 17, 18],
        [ 7,  8,  9, 19, 20, 21],
        [10, 11, 12, 22, 23, 24]])
</code></pre>
<p>Best.</p>
<p>K. Frank</p>