How to sample atleast one sample from each class for a batch in dataloaders

I have a dataset with 100 classes, when I introduce a dataloader with a batch size of 128 I get a batch with only 64(varies randomly but never 100) unique classes. How can I ensure every batch to have at least 1 sample from each class?

Hello,

Take a look at this post

Writing a custom sampler as described e.g. here might also be a valid approach, as it would allow you to create the batch indices beforehand and thus make sure which class distribution is used.