Loss function - multi target


I built a classification model which takes a vector as input and which gives me 3 labels for example [score_1, score_2, score_3] = [3, 7, 9] with the score 3,7,9 it is a score chosen from 11 possible values

3 \ in {0,…, 11}
7 \ in {0,…, 11}
9 \ in {0,…, 11}

I calculated the loss function losse as follows:

loss = criterion (pred, y.long ()) with criterion = nn.CrossEntropyLoss (), but I get an error in the code

multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15

i dont know how to solve it. Could you please help me?

Assuming that all 3 “scored” correspond to active classes, the use case sounds like a multi-class classification and you would use nn.BCEWithLogitsLoss as the criterion.


Thank you for your answer. May with this error function I have a dimension error

“Target size (torch.Size([200, 3])) must be the same as input size (torch.Size([200, 11]))”

I did not understand the usefulness of this function in my case?

nn.BCEWithLogitsLoss expects the model output and target to have the same shape, so you would need to provide a model output in [batch_size, nb_classes] (same as the target) where a high value indicates an active class and a low one an inactive.