How to load data with the same number of samples per class?

Certainly there are advantages in random sampling; however, there are circumstances where the numbers of samples must be the same or pre-determined. So, I wonder how to implement this in pytorch? Should I rewrite every datasets or this can be achieved by a customized sampler used in dataloader? Thanks!\

To make myself clear:
what i need now is, for example, a batch of 10 samples from class A, 10 from class B, 10 from class C, ETC…

If I understand you correctly, you need to use a sampler but keep the data balance. If your data is large enough, then even after random sampling it will be (almost) balanced.
If not, the easiest way would be to generate a weight vector denoting your data balance, then to pass this vector to the loss function.
See for example,

class CrossEntropyLoss(_WeightedLoss) 
...
Args:
        weight (Tensor, optional): a manual rescaling weight given to each class.
            If given, has to be a Tensor of size `C`

in:
https://pytorch.org/docs/stable/_modules/torch/nn/modules/loss.html

This is a good solution to the data imbalance problem! However, what i need now is, for example, a batch of 10 samples from class A, 10 from class B, 10 from class C, ETC… Thanks anyway!

This is a good solution to the data imbalance problem! However, what i need now is, for example, a batch of 10 samples from class A, 10 from class B, 10 from class C, ETC… Thanks anyway!

In this case, maybe you can try the BatchSampler

class BatchSampler(Sampler):

https://pytorch.org/docs/stable/_modules/torch/utils/data/sampler.html

Sorry, but i do not find this link useful. Thanks anyway!

Just wondering if you got a solution for this?