You can subclass torch.utils.data.Sampler torch.utils.data — PyTorch 1.10.0 documentation 1 and pass an instance of this when constructing the DataLoader:
torch.utils.data — PyTorch 1.10.0 documentation 1
Since it looks like you are sampling batches here, you might also want to take a look at using batch_sampler instead.