WeightedRandomSampler(WRS) is used when you don’t want to or can’t under-/over-sample your dataset. This blog 2 explains WRS really well, would recommend checking it out.
When you assign class-weights to Cross Entropy Loss / BCELoss, every time your model makes an incorrect prediction, the loss is multiplied with the given class-weight. So ultimately, the model will push itself to learn meaningful representations of the minority class to minimize the loss. This works adequately 90% of the time.
For example: If you have two classes: Dog(2000 images), and Cat(100 images). If you define your loss as:
BCELoss(weights = Tensor([1.0 , 20.0]))
every time your model predicts cat image as a dog image while training, the loss will be multipled by 20.0, so to minimize the loss, model has to learn the meaningful representations of cat. Hence, you’ll get a better accuracy and precision.
But, still, over-sampling your dataset properly is the best option if you want better overall performance.
Hope it helps!