Ignore_index available in CrossEntropyLoss but not in BCEWITHLOGITSLOSS

I have a segmentation problem where I feed my model images and want it to recognize only a single class. Therefore, I want to use the BCEWithLogitsLoss. However, since there are areas for which I don’t have annotation (but still want to include them in the dataset for context), I want to specify the ignore_index value so that model’s predictions in these areas are not used for computing gradients.

However, the ignore_index is only available in CrossEntropyLoss and not BCEWithLogitsLoss. I don’t understand why? Is there a reason for why it doesn’t make sense that I am missing ?

Also, is my understanding correct that I can accomplish identical behavior to ignore_index by using pos_weight?

Thank you very much!

nn.BCEWithLogitsLoss is used for a binary or multi-label classification and accepts “soft” targets. Assuming you work on a binary classification use case it would be uncommon to exclude one class as you would end up predicting the other one only. If you want to mask the loss of specific samples, use an unreduced loss and multiply the sample loss with zeros.

Ok, thanks a lot for the quick response. I think I misunderstood how the parameter works but now it’s clearer :slight_smile: