Solved by ptrblck in post #2 
              
              
                You could move all classes, which should be ignores to a fixed class index and pass it as ignore_index to nn.CrossEntropyLoss: 
batch_size = 100
nb_classes = 3
output = torch.randn(batch_size, nb_classes, requires_grad=True)
target = torch.randint(0, nb_classes+10, (batch_size,))
# move all unwanted…