Hi Alice!



 Alice_NL:

I am trying to use the weights for the binary classification problem for CrossEntropyLoss and by now I am so lost in it….


For a binary classification problem (two classes, “yes” and “no”)
you will prefer to use BCEWithLogitsLoss 9 rather than
CrossEntropyLoss.
You will want your model to have a single output (so that the shape
of the output is [nBatch, 1]) and have no final Sigmoid activation
layer.
For your use case you will probably want to use the pos_weight
argument to BCEWithLogitsLoss's constructor.


showing that I have five times more zeroes.


In this case one would typically use:
loss_function = torch.nn.BCEWithLogitsLoss (pos_weight = torch.tensor (5.0))

so that you weight your positive sample five times more heavily than
your five-times-more-frequent negative samples.
If you have questions about how or why to use BCEWithLogitsLoss,
please follow up.
Best.
K. Frank