Wednesday, July 24, 2019

"multi-target not supported" error in PyTorch NLLoss

I recently came across this Exception when training a model with PyTorchand calculating the loss using NLLLoss function (NLLLoss).
RuntimeError: multi-target not supported at /pytorch/aten/src/THNN/generic/ClassNLLCriterion.c:20

This exception is because I provided the wrong dimensions for the labels passed to the Criteria (NLLLoss) function. from the documentation I find that if the input to NLLLoss function is N x C (where N is the batch size, and C is the number of classifications), then the target (second argument) should be of size N (one class for each batch element).