Loss function for a classifier with output and target shape: (batch_size, N, M)

Hi,
I have a model with output shape: (batch_size, N, M) and target shape: (batch_size, N, M)
this is a classifier.
so I should use NLL or CrossEntropy as the loss function.
but torch gives me 1D target tensor expected, multi-target not supported error
how can I fix this?

Hi Ali!

Well, you should tell us what “output” means, what “target” means,
and what “N” and “M” are. Otherwise, we will have to guess.

But let me guess a little bit, and make the following comment:

If you are performing a multi-class classification problem (but not
a multi-label problem), then the shapes of the input (output of
your model) and target passed to your CrossEntropyLoss loss
function must match up in the following way:

Your input can have shape [batch_size, nClass], and then your
target must have shape [batch_size] and consist of integer (long)
class labels that run from 0 to nClass - 1.

Or, in the “K-dimensional case,” your input could have shape, say,
[batch_size, nClass, height, width, depth], and your target
would have to have shape [batch_size, height, width, depth]
(and consist of values from 0 to nClass - 1).

Good luck.

K. Frank

2 Likes

thanks Frank, that was helpful