teyimo3726:

My concern is that as my total number of classes (~2000) is more than the batch size (~16) which is length of sampler


The length of the sampler is usually defined as the length of the dataset, not the batch size.
If you are dealing with 2000 classes, a batch size of 16, and e.g. 1 million samples, you could still use the WeightedRandomSampler approach to create batches “as if the dataset would be balanced”.
Of course a single batch won’t contain all 2000 classes, which is impossible due to batch_size < nb_classes.



 teyimo3726:

Is there some way in which I could actually verify that instances from all classes came up in an balanced way


You could count the drawn classes in each batch e.g. via torch.unique(target, return_counts=True).