Hi guys I’m new to PyTorch and I was wondering what was the difference between:
nn.BCEWithLogitsLoss and nn.functional.binary_cross_entropy_with_logits?
Thanks
nn.BCEWithLogitsLoss
is the class and nn.functional.binary_cross_entropy_with_logits
is the function of the binary cross-entropy with logits loss.
They both have the same results, but are used in a different way:
-
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
Then you can docriterion(output, target)
loss = nn.functional.binary_cross_entropy_with_logits(output, target)
Using BCEWithLogitsLoss
you are able to define certain parameters when creating the loss (e.g. weight
). Please look at the documentation for details.
Thank you so much, in fact I am working on a multi-class, multi-label, classification problem. In my situation, which one would you suggest me to use?
Also I have a sever problem of unbalanced data, so I was thinking about using parameters such as weight.
Then I would certainly advise you to use criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
as you can then select the weighting required for your specific dataset.
Thank you so much!, One last thing, I have the weight of the classes, should I use them as pos_weight? Or it has to be computed differently, and in such case could you tell me how?