How should I do resolve this?

criterion = nn.CrossEntropyLoss()
loss = criterion(output[0],y)

It says like this :
RuntimeError: 1D target tensor expected, multi-target not supported

output[0] = tensor([[0.1302, 0.1725, 0.1690, 0.0921, 0.1245, 0.1362, 0.1755]]
y = tensor([[0, 0, 0, 0, 1, 0, 0]])

Your tensor looks like it is 1-hot encoded. The quickest is to take y.argmax(-1) but you might look into just providing the class indices.
Also, your tensor suspiciously looks like probabilities (summing to one and all). Note that CrossEntropyLoss expects logits, so if you have softmax at the end, you might drop that.

Best regards

Thomas

1 Like