Hello Niki!



 Niki:

Thank you for your reply @KFrank. 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.


Here are ways to draw two different kinds of random batches.
Let nTest be the total number of test data items you have.
I will assume that nTest > 2 * k (because this will matter
for one of the kinds of random batches).
First (ba in the example code below) we can construct random
batches of two items, where the items are drawn randomly from
your test data, with replacement.  This means that a batch
might (but is rather unlikely to) contain two copies of the
same item, and two different batches might similarly contain
the same item.
If instead, you want no item to occur more than once in your
batches, I find it easiest to randomly permute the test data
(or, as in the example below, randomly permute indices into
the test data), and then loop through the random permutation
two at a time.
To make your batches (pseudo-)random, you use a
(pseudo-)random number generator.  Both pytorch and
numpy have such things.  I’ve used the ones from numpy.
This example assumes that you have your test data in data, a
torch.tensor, and creates k = 5 random batches of two items
each, using both the “with replacement” (ba) and disjoint (bb)
approaches:
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)

(Note, as far as I can tell, the number “128” doesn’t enter into
the specific question you are asking.)
Best.
K. Frank