Hi Liu!



 tyron:

How I can to weight the loss function in order to penalize errors on the smaller class. I am using nn.NLLLoss()


You can use the weight argument that is passed to NLLLoss's
constructor.
As an aside, if you do, in fact, have only two classes, you could
treat your problem as a binary classification problem (rather than
a general multi-class problem that happens to have two classes),
use BCEWithLogitsLoss, and use its pos_weight argument.
(Aman’s WeightedRandomSampler suggestion is also reasonable.
It and adding weights to the loss function are two somewhat different
approaches for compensating for imbalanced data.)
Best.
K. Frank