Using only subset of class labels for training

Hi all, I have an issue related to following error. The cause for the error is known but I cannot find a workaround as I need to run the algorithm that way.

RuntimeError: cuda runtime error (59) : device-side assert triggered at /tmp/pip-req-build-4baxydiv/aten/src/THCUNN/generic/ClassNLLCriterion.cu:110

I have a large set of classes in a dataset and I want to train the model with only a subset at a time. Lets say I have 12 classes [0-11] and classifier’s last layer output shape is 5. With data subset with data labels [0,1,2,3,4], model works perfectly. However, it throws the above error for following class label combinations.

[1,2,3,4,5]
[2,5,6,8,11]

I am using cross_entropy criterion and last layer is a linera layer. I am fairly new to Pytorch and also searched some solutions in the forum so cound’t find anything related to my requirement. Appreciate any help that can be provided.

The error is expected in this case, since nn.CrossEntropyLoss expects targets in the range [0, nb_classes-1] in order to use it as indices to calculate the loss.

If you want to train the model with different subsets, your model should most likely still output the logits for all 12 classes, shouldn’t it?
I guess your current approach wants to map the new labels to the 5 outputs of your model, which would mix different targets for the same class output of the model so I’m unsure if that’s really what you want.