Thanks for your reply.

In the latter case, you would use e.g.  nn.CrossEntropyLoss  and the target tensor shape should contain the class indices in the range  [0, nb_classes-1]  and miss the “class dimension” (usually the channel dim).

I got it.

Both approaches expect logits, so you should remove your softmax layer and just pass the last output to the criterion.

Thanks for your suggestion, ’so you should remove your softmax layer and just pass the last output to the criterion.', that’s really the point.
In addition to these, there are questions:

Both approaches expect logits, so you should remove your softmax layer and just pass the last output to the criterion.


Does above means the description here ( CrossEntropyLoss) 13 like below?


The input is expected to contain raw, unnormalized scores for each class.


But why is it? In my opinion, the input of CrossEntropyLoss is:
(1) the prediction (the output of model); (2) the label.
And  the prediction data should be converted to probability, which means that I need to place a layer of soft-max layer at the end of the model.

Is that my wrong understanding that the input data of CrossEntropyLoss should be probability?
(So  that put the model output data to the CrossEntropyLoss directly is correct from mathematical theory?)
Or alternatively the CrossEntropyLoss function in PyTorch will do that (computes the probability of prediction of model) automatically?