How to define some classes as don't care for time series classification?

I have a time series classification task in which I should output a classification of 3 classes for every time stamp t.

All data is labeled per frame.

In the data set are more than 3 classes [which are also imbalanced].

I want to use CrossEntropyLoss.

My net should see all samples sequentially, because it uses that for historical information.
Thus, I can’t just eliminate all irrelevant class samples at preprocessing time.

In case of a prediction on a frame which is labeled differently than those 3 classes, I don’t care about the result.


My model outputs a vector of 3 log probabilities, but the label batch has more than 3 labels. Using the weight= parameter with 0 weights for classes I want to ignore yields

{RuntimeError}weight tensor should be defined either for all 3 classes or no classes but got weight tensor of shape: [5] at C:/w/b/windows/pytorch/aten/src\THCUNN/generic/ClassNLLCriterion.cu:44

because the weight's shape was larger than that of the model output.


How to do this correctly in Pytorch?

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()