I am working with a multilabel multiclass classification problem. The output of the neural network is a tensor of size ([batch size, number of labels, number of class]). In my case, it is ([2,6,4]):
tensor([[[0.2287, 0.2657, 0.2634, 0.2423],
[0.2663, 0.2563, 0.2132, 0.2642],
[0.2989, 0.2733, 0.2064, 0.2215],
[0.2235, 0.2659, 0.1971, 0.3135],
[0.2344, 0.2361, 0.2968, 0.2328],
[0.2639, 0.2191, 0.3216, 0.1954]],
[[0.2287, 0.2657, 0.2634, 0.2423],
[0.2663, 0.2563, 0.2132, 0.2642],
[0.2989, 0.2733, 0.2064, 0.2215],
[0.2235, 0.2659, 0.1971, 0.3135],
[0.2344, 0.2361, 0.2968, 0.2328],
[0.2639, 0.2191, 0.3216, 0.1954]]], grad_fn=<SoftmaxBackward0>)
The target size is ([2,6]) for 2 images and 6 labels. Each label can have value from 0 to 3 like
tensor([[0, 0, 2, 1, 3, 0],
[0, 1, 3, 1, 2, 0]])
I am trying to use torch.nn.CrossEntropyLoss()
but it is throwing shape mismatch error. What is the best way handle this situation? Do I have to use one-hot encoded
target?