How to slice a tensor based on another tensor

username: ck172
cooked: <p>for example, I have a target tensor like this, <img src="https://discuss.pytorch.org/uploads/default/original/3X/d/3/d383e08788fbd6a34c999e41ff19c5887c8b5e9d.png" alt="image" data-base62-sha1="ub9i4PH7spCQG4kxuZSRKmVDNSJ" width="604" height="204"><br>
and I have a length tensor [5,3,2],<br>
if I directly flatten the tensor on dimension 2, the shape will be [1,3,15]<br>
How to flatten the tensor based on the length of dimension 3 so that the result can be [1,3,10] where 10 = 5+3+2?<br>
The desired output will be:<br>
tensor([[[0.4076, 0.2769, 0.2220, 0.4076, 0.3851, 0.3649, 0.3190, 0.4107,<br>
0.3881, 0.3823, ],<br>
[0.3337, 0.3804, 0.3061, 0.3337, 0.3179, 0.3422, 0.3616, 0.3879,<br>
0.3302, 0.3508, ],<br>
[0.2588, 0.3427, 0.4719, 0.2588, 0.2970, 0.2930, 0.3194, 0.2014,<br>
0.2817, 0.2669, ]]],<br>
device=‘cuda:0’, grad_fn=)</p>
<p>Thanks!</p>

username: ptrblck
cooked: <p>I’m a bit confused about the mentioned shapes.</p>
<aside class="quote no-group" data-username="ck172" data-post="1" data-topic="135897">
<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/c/aca169/48.png" class="avatar"> ck172:</div>
<blockquote>
<p>and I have a length tensor [5,3,2],<br>
if I directly flatten the tensor on dimension 2, the shape will be [1,3,15]</p>
</blockquote>
</aside>
<p>This shouldn’t be possible via reshaping the tensor as the former one has <code>5*3*2=30</code> elements while the latter has <code>1*3*15=45</code>.</p>
<aside class="quote no-group" data-username="ck172" data-post="1" data-topic="135897">
<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/c/aca169/48.png" class="avatar"> ck172:</div>
<blockquote>
<p>How to flatten the tensor based on the length of dimension 3 so that the result can be [1,3,10] where 10 = 5+3+2?</p>
</blockquote>
</aside>
<p>“Flattening” dimensions would be a product of their shapes.<br>
If you want to create a tensor in the shape <code>[1, 3, 10]</code> by flattening <code>dim0</code> with <code>dim2</code> you could use:</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(5, 3, 2)
y = x.permute(1, 0, 2).contiguous().view(1, 3, -1)
print(y.shape)
&gt; torch.Size([1, 3, 10])
</code></pre>

username: my3bikaht
cooked: <p>tensor you show has shape (1,3,3,5)</p>
<p>You can imagine it as a 1x parallelogram with dimensions 3x3x5<br>
Flattening is pretty much ‘scanning’ it line by line, so you can flatten (3,3,5) to (3,15)</p>
<p>torch.flatten(x, -2, -1) will flatten your 1x3x3x5 tensor to 1x3x15 you were looking for (other option is to  use view)</p>

username: ck172
cooked: <p>Thanks for your reply! I am sorry for the confusion. What I meant is that I could easily flatten the orginal tensor(1,3,3,5) to a (1,3,15) tensor. But I only want to keep some of the values in the original tensor like this:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/0/5071dcfe35bc2322ed551929ad2505c383f29db3.png" data-download-href="https://discuss.pytorch.org/uploads/default/5071dcfe35bc2322ed551929ad2505c383f29db3" title="Screenshot from 2021-11-03 17-05-23"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/0/5071dcfe35bc2322ed551929ad2505c383f29db3_2_690x261.png" alt="Screenshot from 2021-11-03 17-05-23" data-base62-sha1="btE9cXqw5IrVut3LLPB5OEWGVTJ" width="690" height="261" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/0/5071dcfe35bc2322ed551929ad2505c383f29db3_2_690x261.png, https://discuss.pytorch.org/uploads/default/original/3X/5/0/5071dcfe35bc2322ed551929ad2505c383f29db3.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/0/5071dcfe35bc2322ed551929ad2505c383f29db3.png 2x" data-dominant-color="EBE5E7"><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">Screenshot from 2021-11-03 17-05-23</span><span class="informations">956×362 117 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>. So that the last ouput can be (1,3, 10)</p>

username: my3bikaht
cooked: <p>You can slice specific parts then concatenate them to one tensor</p>
<pre><code class="lang-auto">&gt;&gt;&gt; a = torch.rand((1,3,3,5))
tensor([[[[0.1562, 0.1055, 0.5032, 0.0966, 0.2088],
          [0.8520, 0.3314, 0.6602, 0.4182, 0.7371],
          [0.5483, 0.2338, 0.9695, 0.6638, 0.9435]],

         [[0.2520, 0.9377, 0.7031, 0.9447, 0.9292],
          [0.3504, 0.1614, 0.0280, 0.7294, 0.4252],
          [0.6095, 0.9667, 0.6628, 0.9642, 0.2766]],

         [[0.8955, 0.5914, 0.3912, 0.8431, 0.9219],
          [0.4871, 0.5351, 0.9024, 0.3061, 0.1959],
          [0.2931, 0.2810, 0.7195, 0.8711, 0.6927]]]])
&gt;&gt;&gt; c = a[:,:,0,:]
tensor([[[0.1562, 0.1055, 0.5032, 0.0966, 0.2088],
         [0.2520, 0.9377, 0.7031, 0.9447, 0.9292],
         [0.8955, 0.5914, 0.3912, 0.8431, 0.9219]]])
&gt;&gt;&gt; d = a[:,:,1,:3]
tensor([[[0.8520, 0.3314, 0.6602],
         [0.3504, 0.1614, 0.0280],
         [0.4871, 0.5351, 0.9024]]])
&gt;&gt;&gt; e = a[:,:,2,:2]
tensor([[[0.5483, 0.2338],
         [0.6095, 0.9667],
         [0.2931, 0.2810]]])
&gt;&gt;&gt; f = torch.cat((c,d,e),2)
tensor([[[0.1562, 0.1055, 0.5032, 0.0966, 0.2088, 0.8520, 0.3314, 0.6602,
          0.5483, 0.2338],
         [0.2520, 0.9377, 0.7031, 0.9447, 0.9292, 0.3504, 0.1614, 0.0280,
          0.6095, 0.9667],
         [0.8955, 0.5914, 0.3912, 0.8431, 0.9219, 0.4871, 0.5351, 0.9024,
          0.2931, 0.2810]]])
</code></pre>
<p>I think slices and torch.cat should keep the gradients</p>