Crossentropyloss entry weights?

Is it possible to add weights to every entry in the input for CrossEntropyLoss, using only python code while maintaining efficiency?

Alternatively, I looked at the underlying C code: ClassNLLCriterion.c and it seems that it may be modified to make use of the class weights to do entry weighting. Is this feasible for running some experiments?

Thank you!

I’d probably use reduce=False in the loss and do the weighting and summing myself. I don’t think you will notice the performance difference if you have any nontrivial computation in the model you waant to train.

Best regard


Hi Thomas,

Thanks so much for the reply. I’m worried that reduce=False will only give me access to mini batch level weights. Is there any way to get to the individual entries?

Thanks again,

Do try and if it doesn’t do what you want, print the shapes of the loss, weights, and targets and then we’ll see.

Best regards


You’re totally right, I think. From the documentation I thought it would only give minibatch level access but the reduce=False flag uses the original input dimension for the output.