Input to NLLLoss: label out of range?

Hi,
I have a softmax output over a list: [10, 11, 12, 13, 14, 15, 16, 17, 18], where each number represents the word token index. The softmax predicts the probability distribution as [1,9] tensor. And the ground truth label is 13.
I want to compute the NLL loss between ground truth label: 13, and the prediction. Since the length of list is 9, it gave me error as:
RuntimeError: Assertioncur_target >= 0 && cur_target < n_classes’ failed. at …/aten/src/THNN/generic/ClassNLLCriterion.c:94`

I thought the NLLLoss could take ground truth label as it is, is that true?

nn.NLLLoss expects the class labels to be in the range [0, nb_classes-1].
If you are dealing with class indices starting at e.g. 10, you should subtract the offset from your target tensor.

2 Likes