Randomness in SubsetRandomSampler

Hi! I have a question about the usage of SubsetRandomSampler.
I have multiple dataloader in my code and say it’s defined this way:

gen = torch.Generator()
data_loader = DataLoader(train_set,

eval_loader = DataLoader(eval_set,

The first loader is used before trainin and testing, the latter is used in testing. If I exclude testing in each epoch, with a fixed seed, the first dataloader gives the same batch order per epoch. But if I include testing in each epoch with a same seed, the first dataloader will give a different batch order.

It’s still the case even if I set the generator in data_loader. So what happened to the SubsetRandomSampler when I do testing right after it in every epoch?
@ptrblck sorry to bother you again, but could you please help me look at this?

In short, how can I make sure when I iterate data_loader with SubsetRandomSampler with the same seed, it’s giving a fixed order of batches per epoch?

Also in this case I don’t think SubsetRandomSampler and RandomSampler are different, so what’s the “subset” for?

from torch.utils.data import SequentialSampler, BatchSampler, SubsetRandomSampler, RandomSampler
list(BatchSampler(SubsetRandomSampler(range(10)), batch_size=3, drop_last=False))
Out[7]: [[2, 9, 5], [1, 0, 4], [8, 7, 3], [6]]
list(BatchSampler(SequentialSampler(range(10)), batch_size=3, drop_last=False))
Out[8]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
list(BatchSampler(RandomSampler(range(10)), batch_size=3, drop_last=False))
Out[9]: [[2, 6, 7], [9, 5, 4], [8, 1, 3], [0]]