CrossEntropyLoss Expected Target Size error

Hi! I am trying to use CrossEntropyLoss as the loss function for a Linear model but I am getting the following error:

Expected target size (98, 1), got torch.Size([98, 20, 1])

I realise there is a number of posts about this error already, but none of the proposed solutions worked for me.

The predictions from my Linear model are in the shape [98,20,1] and my ground-truth values are in the shape [98,20,1].

From this response, I know that the output should be [batch_size, nb_classes] so [98, 20] in my case and the target should be [batch_size] or [98] in my case.

Now my question is, how do I make it so that I can use CrossEntropyLoss without transforming my ground-truth values to [98]? Is this even possible?

Edit: if I transform the ground-truth values to be [98,1] with y = y[:,1], I get the following error:

Thank you!

Can you just print out an example target?

Sure:

image

^ this is a print(y[0]).

Ok now I am confused. What are those labels supposed to represent? Are those the softmax scores? Or something else?

Softmax…I think. Here’s my model:

image

I am calling it as such:

net = Net(inputs=3, hiddens=5, outputs=1)

The input to the model is in the shape [batch_size x 3]

Hope that makes it more clear?

Ok if they are supposed to be the softmax score then one thing you could do is try just getting the argmax of them using torch.argmax. That would give it the correct shape and it would provide the loss function with the correct label.

Sorry I am a little confused. Do you mean to get the argmax of the predictions? Or the actual ground-truth values?

the actual ground truth. I don’t think your targets are softmax however because they do not add up to 1. So I am still a little lost on what they are.