Torch.repeat different per row, like np.repeat

username: Diego999
cooked: <p>Hi everybody,</p>
<p>Is there a current API that’s behave similarly to numpy, where we can repeat each row individually ?</p>
<pre><code class="lang-auto">x = np.array([[1,2],[3,4]])
np.repeat(x, [1, 2], axis=0)
array([[1, 2],
       [3, 4],
       [3, 4]])
</code></pre>
<p>I didn’t find any similar function. What would be the most efficient way of doing this and still backpropagating ?</p>
<p>Thank you in advance</p>

username: otutay
cooked: <p>search for torch.repeat in the following document</p>
<pre><code class="lang-auto">https://pytorch.org/docs/stable/tensors.html
</code></pre>

username: justusschock
cooked: <pre><code class="lang-auto">x = torch.tensor([[1,2],[3,4]])
x.repeat(1,2)
</code></pre>
<p>Should work the way you want</p>

username: Diego999
cooked: <aside class="quote no-group" data-username="justusschock" data-post="3" data-topic="41323">
<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/justusschock/48/4637_2.png" class="avatar"> justusschock:</div>
<blockquote>
<p>x = torch.tensor([[1,2],[3,4]]) x.repeat(1,2)</p>
</blockquote>
</aside>
<p>I don’t obtain the same output:</p>
<pre><code class="lang-auto">In [9]: import numpy as np                                                                                                                                                                                                                                                       

In [10]: import torch                                                                                                                                                                                                                                                            

In [11]: x = torch.tensor([[1,2],[3,4]]) 
    ...: x.repeat(1,2)                                                                                                                                                                                                                                                           
Out[11]: 
tensor([[1, 2, 1, 2],
        [3, 4, 3, 4]])

In [12]: x = np.array([[1,2],[3,4]])                                                                                                                                                                                                                                             

In [13]: np.repeat(x, [1,2], axis=0)                                                                                                                                                                                                                                             
Out[13]: 
array([[1, 2],
       [3, 4],
       [3, 4]])
</code></pre>

username: Diego999
cooked: <p>Could you provide an example that produces the same output as numpy ?</p>
<pre><code class="lang-auto">In [9]: import numpy as np                                                                                                                                                                                                                                                       

In [10]: import torch                                                                                                                                                                                                                                                            

In [11]: x = torch.tensor([[1,2],[3,4]]) 
    ...: x.repeat(1,2)                                                                                                                                                                                                                                                           
Out[11]: 
tensor([[1, 2, 1, 2],
        [3, 4, 3, 4]])

In [12]: x = np.array([[1,2],[3,4]])                                                                                                                                                                                                                                             

In [13]: np.repeat(x, [1,2], axis=0)                                                                                                                                                                                                                                             
Out[13]: 
array([[1, 2],
       [3, 4],
       [3, 4]])
</code></pre>

username: Diego999
cooked: <p>Do you think there would be a better way than</p>
<pre><code class="lang-auto">In [1]: import torch                                                                                                                  

In [2]: import numpy as np                                                                                                            

In [3]: x = torch.FloatTensor([[1,2],[3,4]]) 
   ...: xx = x.split(1) 
   ...:                                                                                                                               

In [4]: xx                                                                                                                            
Out[4]: (tensor([[1., 2.]]), tensor([[3., 4.]]))

In [5]: out = torch.FloatTensor([]) 
   ...:  
   ...: for x_sub, num_repeat in zip(xx, [1,2]): 
   ...: ^Iout = torch.cat([out, x_sub.expand(num_repeat, -1)]) 
   ...:                                                                                                                               

In [6]: out                                                                                                                           
Out[6]: 
tensor([[1., 2.],
        [3., 4.],
        [3., 4.]])

In [7]: x = np.array([[1,2],[3,4]])                                                                                                   

In [8]: np.repeat(x, [1,2], axis=0)                                                                                                   
Out[8]: 
array([[1, 2],
       [3, 4],
       [3, 4]])
</code></pre>
<p>Is this work for sure with backpropagation ?</p>

username: MariosOreo
cooked: <p>Hello,</p>
<p>I tried the code below, and it seems that it works with backpropagation.</p>
<pre><code class="lang-auto">x = torch.tensor([[1, 2], [3, 4]], requires_grad=True)
a, b = x.split(1)
a.requires_grad
&gt;&gt; True
a.grad_fn
&gt;&gt; &lt;SplitBackward at 0x7f1ed6f25128&gt;
out = torch.FloatTensor([])
torch.cat([out, a.expand(1,-1)])
&gt;&gt; tensor([[1., 2.]], grad_fn=&lt;CatBackward&gt;)

</code></pre>

username: Diego999
cooked: <p>Thank you for your answer !</p>
<p>I’m just thinking in terms of efficiency, if it makes the computation much slower. Do you have an idea ?</p>

username: arogozhnikov
cooked: <p><a href="https://github.com/arogozhnikov/einops" rel="nofollow noopener">Einops</a> recently got support for repeat-like patterns. Examples:</p>
<pre><code class="lang-auto"># np.repeat behavior, repeat rows (copies are in succession like aaabbbcccddd)
einops.repeat(x, 'i j -&gt; (i copy) j', copy=3)
# np.repeat behavior, repeat columns (copies are in succession like aaabbbcccddd)
einops.repeat(x, 'i j -&gt; i (j copy)', copy=3)
# np.tile behavior (whole sequence is repeated 3 times like abcdabcdabcd)
einops.repeat(x, 'i j -&gt; (copy i) j', copy=3)
</code></pre>
<p>You can repeat/tile multiple axes independently within one operation.</p>

username: rodrigobraz
cooked: <p>You can do this with <a href="https://pytorch.org/docs/stable/generated/torch.repeat_interleave.html#torch.repeat_interleave" rel="noopener nofollow ugc">repeat_interleave</a>.</p>
<p>It even includes your exact example in its documentation (I am guessing they introduced it after your post and perhaps even because of it):</p>
<pre><code class="lang-auto">&gt;&gt;&gt; y = torch.tensor([[1, 2], [3, 4]])
&gt;&gt;&gt; torch.repeat_interleave(y, 2)
tensor([1, 1, 2, 2, 3, 3, 4, 4])
&gt;&gt;&gt; torch.repeat_interleave(y, 3, dim=1)
tensor([[1, 1, 1, 2, 2, 2],
        [3, 3, 3, 4, 4, 4]])
&gt;&gt;&gt; torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
tensor([[1, 2],
        [3, 4],
        [3, 4]])
</code></pre>

username: Mohit_Kumar_Pandey
cooked: <pre><code class="lang-auto">x = torch.Tensor(([[1,2],[3,4]]))
torch.repeat_interleave(x, torch.tensor([1, 2]), dim=0)
</code></pre>