Effective way to repeat indices in batch with batch of indices lengths?

username: hadaev8
cooked: <p>Lets say i have to tensors<br>
x (len, batch, feature) and duration (len, batch)</p>
<p>And I want to convert it for example like this:</p>
<p>x (1, 2, 3, 4) + dur (3, 3, 2, 1) = &gt; (1, 1, 1, 2, 2, 2, 3, 3, 4)</p>
<p>So repeat each element with value for second tensor times.</p>
<p>For now i made this code, but it is super ineffective</p>
<pre><code>max_len = dur.sum(0).max()

out_tensor = torch.zeros((max_len, x.size(1), x.size(2))).cuda()
for idx in range(x.size(1)):
    text_expanded = torch.cat([text_.repeat(len_, 1)
                                for text_, len_ in zip(x[:, idx], dur[:, idx])])
    out_tensor[:len(text_expanded), idx] = text_expanded</code></pre>

username: kshitij
cooked: <p>Try this.</p>
<pre><code class="lang-python">&gt;&gt;&gt; import torch
&gt;&gt;&gt; x = torch.tensor([1, 2, 3, 4])
&gt;&gt;&gt; torch.repeat_interleave(x, torch.tensor([3,3,2,1]))
tensor([1, 1, 1, 2, 2, 2, 3, 3, 4])
</code></pre>
<p><a href="https://pytorch.org/docs/stable/torch.html#torch.repeat_interleave" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/torch.html#torch.repeat_interleave</a></p>

username: hadaev8
cooked: <p>I need repeat with batches<br>
For example<br>
x is input tensor and dur tensor with indices lengths from 0 to 3</p>
<pre><code>import torch
x = torch.rand((50, 16, 128))
dur = (torch.rand((50, 16))*3).long()
</code></pre>
<p>Then torch.repeat_interleave(x, dur) will raise RuntimeError: repeats must be 0-dim or 1-dim tensor</p>

username: kshitij
cooked: <p>I don’t think that can be possible for more than 0-dim or 1-dim as for 2-d case, if the <code>dur</code> is different for each row that will imply that each row will have different number of elements, which is not supported.</p>
<p>Eg.</p>
<pre><code class="lang-python">x = [[1,2,3,4],
     [1,2,3,4]]

dur = [[1,1,1,1],
       [2,2,2,2]] # Notice different for 2nd row.

# Expected output (which is not supported)
output = [[1,2,3,4],
          [1,1, 2, 2, 3, 3, 4, 4]]

</code></pre>

username: hadaev8
cooked: <p>Well, zero padding would be ok.<br>
But i didnt found way to do so in “batch” style, and iterating 2 times consume a lot of time.</p>

username: kshitij
cooked: <p>Sorry but even I am not aware of a way to do it directly using the operators for more than 1-d Tensor.</p>
<p>Not sure if this answer is helpful or not, but do check.<br>
<aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/57495789/how-to-repeat-a-numpy-array-along-a-new-dimension-with-padding" target="_blank" rel="nofollow noopener">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/2939369/raven" target="_blank" rel="nofollow noopener">
    <img alt="Raven" src="https://graph.facebook.com/100001668585360/picture?type=large" class="thumbnail onebox-avatar" width="200" height="200">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/57495789/how-to-repeat-a-numpy-array-along-a-new-dimension-with-padding" target="_blank" rel="nofollow noopener">How to repeat a numpy array along a new dimension with padding?</a>
</h4>

<div class="tags">
  <strong>python, numpy</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/2939369/raven" target="_blank" rel="nofollow noopener">
    Raven
  </a>
  on <a href="https://stackoverflow.com/questions/57495789/how-to-repeat-a-numpy-array-along-a-new-dimension-with-padding" target="_blank" rel="nofollow noopener">01:31PM - 14 Aug 19 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>