How is weighted NLL loss calculated

Can anyone give me the formula for weighted NLL. I want to know where weights are getting multiplied. I have read this https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html but I am not able to understand what $x_{n,y_n}$ is basically. I want to write the formula for weight NLL for C classes. Is the following correct (consider I have correct labels and incorrect labels):

image

x corresponds to the logits given as the model output.
Here is a comparison between a manual approach and the weighted criterion.

1 Like

Hi. Thanks for the answer. Can you please tell me whether the formula I have written above for reduction==‘mean’ is correct or not?

I don’t know how y and y' are defined, e.g. is y a one-hot encoded target tensor acting as a mask (so alternatively also indexing would work using the class indices directly)?
If so, I guess the class index in w would be missing but you could also compare it to the used formula in nn.CrossEntropyLoss to make sure it’s equivalent.