Tensor Manipulation: Splitting then Padding a New Dimension

username: HHousen
cooked: <p>Lets say I have a tensor of torch.Size([32, 53, 768]). How would I go about converting it to torch.Size([32, 12, 5, 768])? When this conversion is computed, the dimension with original size 53 should be split at variable indexes (there will be 11 indexes so that 12 new sequences are formed). If the distance between two indexes is less than 5 then zeros should be added so that each sequence is 5 units long.</p>
<p>Essentially, I would like to split a sequence at certain indexes, pad the new sequences to some value, and end up with a tensor of torch.Size([15, 10]) (if starting with torch.Size([100]), splitting to create 15 sequences, and padding to 10).</p>
<p>I would like to do this without a loop since it will happen in the forward pass of a model and loops dramatically decrease performance in my understanding. It would be fine if a loop was used if it would not greatly impact model training time or performance.</p>
<p>My end goal is to be able to take the mean across the newly formed padded dimension and, in the case of the first example, end up with torch.Size([32, 12, 1, 768].</p>
<p><strong>Example Image:</strong> <a href="https://discuss.pytorch.org/t/tensor-manipulation-splitting-then-padding-a-new-dimension/74862/5">See Post 5</a></p>
<p>Thank you. Any tips or suggestions are welcome.</p>

username: charan_Vjy
cooked: <p>A couple of clarifications</p>
<ul>
<li>
<p>How do you decide the split at 53? Is it arbitarary(number dependent)? To rephrase, how do you decide that 53 has to split into (12,5). 12*5 = 60, which means there 7 values would have been padded.</p>
</li>
<li>
<p>Is the padding arbitarary or do you want to pad all the sequences equally? For example, I could pad 7 to 53 to obtain a length of 60 and then split as (12,5). Or do you have any other scheme in mind?</p>
</li>
</ul>

username: HHousen
cooked: <p>Thanks for the help.</p>
<ol>
<li>It is arbitrary in this example, meaning those numbers will vary, but they will always be provided. It sometimes may occur that no sequences need padding but it will never happen such that all sequences need padding.</li>
<li>For the padding, all sequences should be padded equally. So, once the large original sequence is split at the indexes provided, each sub-sequence will be padded to the constant length.</li>
</ol>

username: charan_Vjy
cooked: <p>Can you clarify the padding scheme with an example?</p>

username: HHousen
cooked: <p>Yeah, sure.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/e/3e896138de1c6e1b692a8c00e67b4d09193542d7.png" data-download-href="https://discuss.pytorch.org/uploads/default/3e896138de1c6e1b692a8c00e67b4d09193542d7" title="Example"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/e/3e896138de1c6e1b692a8c00e67b4d09193542d7.png" alt="Example" data-base62-sha1="8VdW9zYzJj29RLWZZ2btAAmACcn" width="666" height="500" data-dominant-color="2F2F2F"><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">Example</span><span class="informations">960×720 31 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>

username: charan_Vjy
cooked: <p>I’m not sure whether this can be done without the use of loops. Will try. If I able to do it, will let you know.</p>

username: HHousen
cooked: <p>Thank you so much for trying. <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:"></p>