'Pytorch: Weighting in BCEWithLogitsLoss, but with 'weight' instead of 'pos_weight'

I'm looking how to do class weighting using BCEWithLogitsLoss.

https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html

The example on how to use pos_weight seems clear to me. If there are 3x more negative samples than positive samples, then you can set pos_weight=3

Does the weight parameter do the same thing?

Say that I set it weight=torch.tensor([1, 3]). Is that the same thing as pos_weight=3

Also, is weight normalized? Is weight=torch.tensor([1, 3]) the same as weight=torch.tensor([3, 9]), or are they different in how they affect the magnitude of the loss?



Solution 1:[1]

They are different things. pos_weight is size n_classes. weight is size batch_size. In the formula in the page you linked, weight refers to the w_n variable, whereas pos_weight refers to the p_c variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 crypdick