RuntimeError nn.CrossEntropyLoss() shape issue

I’m running into: Predictions and targets are expected to have the same shape, but got torch.Size([1013, 100]) and torch.Size([1013]). I am using class indices as the predictions of which there are 100 and the batch size is 1013. What am I missing? I’ve gotten this to work on other code and the example in PyTorch is very similar with targets as [5,3] and preds as [3].

Hi John!

The probable cause of your error is that your target is a Float tensor
when it should be a Long tensor.

CrossEntropyLoss supports both integer-class-index and floating-point
probabilistic (“soft”) targets. The former is required to have shape
[nBatch] (no nClass dimension) and have values that run from 0 to
nClass - 1, while the latter is required to have shape [nBatch, nClass]
with values that run from 0.0 to 1.0.

If your target is a Float tensor, CrossEntropyLoss assumes that you
are using a probabilistic target and requires it to have the same shape
as your predictions (the input to CrossEntropyLoss), namely
[nBatch, nClass].


K. Frank

Hi K. Frank,

Thanks for the reponse. I thought that same thing too, but after checking it in the debugger my prediction is HalfTensor and my target is LongTensor. I’m super confused. I went back and reran some old code that was fully functional solving a similar problem and the shapes agreed with what I’m doing now.


I ran through the debugger and it looks like it’s actually happening in my TorchMetrics accuracy function. I’ll dig in deep and get back to everyone.