I am trying to understand how the pos_weight argument is being used in BCEWithLogitsLoss in order to be able to correctly define the pos_weight Tensor. The documentation only mentions: "a weight of positive examples. Must be a vector with length equal to the number of classes.". As I was not able to get a sufficient understanding by looking at the code (the actual code is hidden behind multiple function loaders and I didn't even manage to reach a point where pos_weight is used), I have several questions regarding the pos_weight argument:
- Is the weight for negative samples always 1?
- If the weight for negative samples is always 1 and assuming I want every sample to contribute equally to the loss I would do something like the following. Let
l = [100, 10, 5, 15]wherel[0]is the amount of negative samples andl[1:]is the amount of positive samples from each label. In pseudo code this looks like:
l = [100, 10, 5, 15]
lcm = LCM(l) # 300
weights = lcm / l # weights = [3, 30, 60, 20]
weights = weights / l[0] # weights = [1, 10, 20, 6.6667]
positive_weights = weights[1:] # [10, 20, 6.66667]
criterion = nn.BCEWithLogitsLoss(pos_weight=positive_weights)
Can someone please confirm if my understanding of how pos_weight should be used is correct?