Batch version of torch.randperm

enterthevoidf22: <p>i have a batch of input vectors and i want to shuffle them before feeding to a nn.<br>
i noticed to torch.randperm doesn’t have an option to generate multiple samples at once. in such a case, which is better in terms of computation time but also setbacks with learning:</p>
<p>my input is x with shape (batch_size,vec_size)</p>
<ol>
<li>
</li></ol>
<pre><code class="lang-auto">shuffled_indices = torch.empty(0,vec_size)
for i in range(batch_size):
shuffled_indices = torch.cat([shuffled_indices,torch.randperm(vec_size)])
x = x[shuffled_indices]
</code></pre>
<ol start="2">
<li>
</li></ol>
<pre><code class="lang-auto">shuffled_indices = torch.randperm(vec_size).unsqueeze(0).repeat(batch_size,1)
x=x[shuffled_indices]
</code></pre>
<p>notice that these are two different approaches. in one i use a loop to generate a batch of shuffled indices, in the other i just let all samples in the batch be shuffled in the same order. i’m trying to figure out if shuffling the entire batch in one order would add an unwanted bias to the batch, and if this is less significant than the time consumption of a for loop as in example 1.</p>
<p>p.s<br>
also notice i assign the new order by simply getting the shuffled indices in the original tensor, but in torch this doesn’t give the desired outcome. so that’s another bonus question - how do you treat a multidimensional tensor as coordinates to get certain elements from another multidimensional tesnor?</p>

tom: <p>You should never use that <code>x = cat([x, y])</code> pattern. It does O(n^2) copying and does so in a way that shows. You can preallocate using empty and then use randperm with out on the rows.</p>
<p>An alternative to generate a batch in one go you might benchmark that, could be to generate a matrix of random values and sort that in one dimension. This isn’t ideal in terms of complexity but should generally work very well.</p>
<p>Of course, using a single permutation is faster and if you have enough minibatches it might not matter as much, but I would be very weary of spurious correlations (just like people found out that the interdependence for batch norm often does not matter as much but in some cases turns out to be really bad).</p>
<p>Best regards</p>
<p>Thomas</p>

Eta_C: <p>Hello</p>
<pre><code class="lang-python">import torch 


x = torch.rand(3, 5)
print(x)

# CASE 1
indices = torch.argsort(torch.rand(*x.shape), dim=-1)
result = x[torch.arange(x.shape[0]).unsqueeze(-1), indices]
print(indices)
print(result)

# CASE 2
indices = torch.randperm(x.shape[1])
result = x[:, indices]
print(indices)
print(result)
</code></pre>

BramVanroy: <p>Hi, sorry for bumping/topic stealing.</p>
<p><a class="mention" href="/u/tom">@tom</a> You mention that the <code>x = cat([x, y])</code> pattern should be avoided. Conversely, <a class="mention" href="/u/smth">@smth</a> <a href="https://discuss.pytorch.org/t/appending-to-a-tensor/2665/3">mentions</a> that cat is extremely optimized. So what are the use cases when (not) to use it?</p>

tom: <p>The problem with the pattern is not the speed of a single <code>cat</code> operation (which, as <a class="mention" href="/u/smth">@smth</a> says, is quite fast), but constructing a pattern where “N” data points are copied “N” times in a loop (there will be a factor here, but N/2 of the data points would be copied &gt;= N/2 times) - this square factor of N is the problematic bit and e.g. building a long Python list and then using a single cat or pre-allocating avoids it.</p>
<p>Best regards</p>
<p>Thomas</p>

YodaEmbedding: <p>If <code>shape == (b, c)</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">torch.stack([torch.randperm(c) for _ in range(b)])
</code></pre>
<hr>
<p>Example:</p>
<pre data-code-wrap="python"><code class="lang-python">&gt;&gt;&gt; b, c = 8, 3
&gt;&gt;&gt; torch.stack([torch.randperm(c) for _ in range(b)])
tensor([[2, 0, 1],
        [1, 2, 0],
        [2, 0, 1],
        [2, 1, 0],
        [1, 2, 0],
        [2, 0, 1],
        [1, 2, 0],
        [0, 1, 2]])
</code></pre>