The reason for the error is that when the loss function is calculated in pytorch, the label is (batch, height, width). If the category is
10, then the value should be
0~9, that is:
0<= value<=C-1, where C is the number of channels, or the number of categories.
My category is
10 categories, and the value is
1~10, so I only need to subtract 1 as shown below.
c_loss = nn.CrossEntropyLoss() labels_v = labels_v-1 loss0 = c_loss(d0, labels_v.long())
The main reason is that the label data of your training data may exceed the number of labels set in the configuration file. Or the number of labels in the validation set exceeds the number of labels in the training set.