Setting weight to the NLLLoss for imbalanced data

WeightedRandomSampler might be helpful to you.
Here’s a discussion about it.