It depends on the shape of your model output. If you are using nn.CrossEntropyLoss the targets should have the same shape as the output without the channel dimension. See the docs 5 for more information.


Your target should be a torch.LongTensor containing the class indices. The second error message means, that your target contains invalid indices, i.e. smaller than 0 or bigger than number_of_classes - 1.


I’m not sure I’m understanding the question properly.


Most likely your hidden_size is fixed, since you are using it for your target tensor. The shape mismatch will be probably thrown in your criterion.


Based on this use case (multiple keys can be pressed at the same same), it looks like you are dealing with a multi-label classification. A way to deal with such a task would be to use nn.BCELoss instead of nn.CrossEntropyLoss to allow more than one “active” class. Have a look at the docs 1 for more information about the shape etc.