How drawing k random batches?

username: Niki
cooked: <p>I am looking for drawing k <strong>random</strong> batches of 2 objects from test dataset, CIFAR10. Let’s say for batch_size=128, k=64, any ideas how can I do that?</p>

username: KFrank
cooked: <p>Hi Niki!</p>
<aside class="quote no-group" data-username="Niki" data-post="1" data-topic="44538" data-full="true">
<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/n/c57346/48.png" class="avatar"> Niki:</div>
<blockquote>
<p>I am looking for drawing k <strong>random</strong> batches of 2 objects from test dataset, CIFAR10. Let’s say for batch_size=128, k=64, any ideas how can I do that?</p>
</blockquote>
</aside>
<p>Could you clarify what you want to do?  You say that you<br>
want “batches of 2 objects,” but you also say that you want<br>
“batch_size=128.”  If 2 is not that batch size (2 items per<br>
batch), what role does 2 play in your question?</p>
<p>Best regards.</p>
<p>K. Frank</p>

username: Niki
cooked: <p>Thank you for your reply <a class="mention" href="/u/kfrank">@KFrank</a>. 128 is the initial number of batches for the test data, I want to put 2 items per batch in k random batches. I want to create k random batches in the test function.</p>

username: KFrank
cooked: <p>Hello Niki!</p>
<aside class="quote no-group" data-username="Niki" data-post="3" data-topic="44538" data-full="true">
<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/n/c57346/48.png" class="avatar"> Niki:</div>
<blockquote>
<p>Thank you for your reply <a class="mention" href="/u/kfrank">@KFrank</a>. 128 is the initial number of batches for the test data, I want to put 2 items per batch in k random batches. I want to create k random batches in the test function.</p>
</blockquote>
</aside>
<p>Here are ways to draw two different kinds of random batches.<br>
Let <code>nTest</code> be the total number of test data items you have.<br>
I will assume that <code>nTest &gt; 2 * k</code> (because this will matter<br>
for one of the kinds of random batches).</p>
<p>First (<code>ba</code> in the example code below) we can construct random<br>
batches of two items, where the items are drawn randomly from<br>
your test data, <em>with replacement</em>.  This means that a batch<br>
might (but is rather unlikely to) contain two copies of the<br>
same item, and two different batches might similarly contain<br>
the same item.</p>
<p>If instead, you want no item to occur more than once in your<br>
batches, I find it easiest to randomly permute the test data<br>
(or, as in the example below, randomly permute indices into<br>
the test data), and then loop through the random permutation<br>
two at a time.</p>
<p>To make your batches (pseudo-)random, you use a<br>
(pseudo-)random number generator.  Both pytorch and<br>
numpy have such things.  I’ve used the ones from numpy.</p>
<p>This example assumes that you have your test data in <code>data</code>, a<br>
<code>torch.tensor</code>, and creates <code>k = 5</code> random batches of two items<br>
each, using both the “with replacement” (<code>ba</code>) and disjoint (<code>bb</code>)<br>
approaches:</p>
<pre data-code-wrap="python"><code class="lang-python">import numpy as np
import torch

nTest = 30
k = 5
data = torch.randn (nTest, 3)
p = np.random.permutation (nTest)
# each ba is a batch of 2 drawn independently with replacement
# the bb's are a set of batches of 2 without duplicates, either
# within or among the batches
for  i in range (k):
     ba = data[[np.random.randint (nTest), np.random.randint (nTest)]]
     bb = data[p[2*i : 2*i + 2]]
     print (ba)
     print (bb)
</code></pre>
<p>(Note, as far as I can tell, the number “128” doesn’t enter into<br>
the specific question you are asking.)</p>
<p>Best.</p>
<p>K. Frank</p>

username: Niki
cooked: <p>Thank you very much for your reply <a class="mention" href="/u/kfrank">@KFrank</a>.</p>