How to use WeightedRandomSampler for imbalanced data

The WeightedRandomSampler expects a weight tensor, which assigns a weight to each sample, not the class labels.
Here is an example of its usage.

Based on your description it also seems that you are working on a multi-label classification, where each sample might belong to zero, one, or more classes.
If that’s the case, not that over-/undersampling might not be trivial, since you could increase the class count of multiple classes by drawing specific samples. Here is a post, which discusses this use case a bit more.

2 Likes