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!
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.
Most helpful comment
CrossEntropyLoss takes a 1D tensor. If your
target
has size(32, 1)
, you need to squeeze the last dimension withtarget.squeeze(1)
so it becomes a 1D tensor.