Pytorch: Error in nll_loss - multi-target not supported

Created on 13 Nov 2017  路  1Comment  路  Source: pytorch/pytorch

I'm having some trouble with my CrossEntropyLoss:
I have a small ConvNet with 7 classes. I'm using nn.CrossEntropyLoss(size_average=False) as my criterion for the loss function.
However, when I run the code, I'm having errors when I try to compute the loss:

  File "C:/***/classifier_ce.py", line 166, in <module>
    loss = criterion(y_pred,labels) # compute the loss

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 224, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 482, in forward
    self.ignore_index)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py", line 746, in cross_entropy
    return nll_loss(log_softmax(input), target, weight, size_average, ignore_index)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py", line 672, in nll_loss
    return _functions.thnn.NLLLoss.apply(input, target, weight, size_average, ignore_index)

  File "C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\_functions\thnn\auto.py", line 47, in forward
    output, *ctx.additional_args)

RuntimeError: multi-target not supported at d:\projects\pytorch\torch\lib\thnn\generic/ClassNLLCriterion.c:22

I've looked in the following issue (after solving a problem where the target tensor was a FloatTensor and changed it to a LongTensor): https://github.com/torch/cutorch/issues/227

So I've changed my labels, to be a 1x1 LongTensor with a number raging from 0<= x <=6 (I have 7 classes).

I'm using a trainloader, and I try to train my model with 32-batch.
I expected the labels (after the error was raised), and indeed the label that is passed to the criterion is a 32x1 LongTensor, which follows the guidlines from the issue above. However, the problem still happening.

Would love some help on this issue, thanks!

Most helpful comment

CrossEntropyLoss takes a 1D tensor. If your target has size (32, 1), you need to squeeze the last dimension with target.squeeze(1) so it becomes a 1D tensor.

>All comments

CrossEntropyLoss takes a 1D tensor. If your target has size (32, 1), you need to squeeze the last dimension with target.squeeze(1) so it becomes a 1D tensor.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajarshd picture rajarshd  路  3Comments

szagoruyko picture szagoruyko  路  3Comments

miguelvr picture miguelvr  路  3Comments

bartolsthoorn picture bartolsthoorn  路  3Comments

cdluminate picture cdluminate  路  3Comments