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 classes to "ignore_index"
target[target>=3] = 3

criterion = nn.CrossEntropyLoss(ignore_index=3)
loss = criterion(output, target)
loss.backward()