Hi Cici!



 Cici1:

I’m training a CNN model and got this error:


The short answer is that your target contains a value of 23, but
you are training a 10-class classifier for which your class labels
must run from 0 to 9.
(As an aside, please don’t post screen-shots of textual information.
They can’t be searched or copy-pasted.)


This is my code for training the model:
...
        loss = F.cross_entropy(output, target)
...

This is my model
...
        self.fc2 = nn.Linear(128, 10)
...
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output



Your output has shape [nBatch, 10] so you are predicting 10
classes.  But you have passed your loss function a target containing
a value of 23, hence this particular error message.
(Also, you are using cross_entropy(), which has log_softmax()
built in.  You should either get rid of log_softmax() or use nll_loss(),
which expects its input to have been passed through log_softmax()
already.)
Good luck.
K. Frank