Hi Anil!



 ayadav01:

rather than passing the weight argument (which is the rescaling weight of each bach element) to the loss function we pass the pos_weight argument (which is weight of positive example).


That is correct.


In what scenarios would you want to use one or the other ?


If your sample weight only depends on the class of the sample,
you can use either.  I tend to think that pos_weight is a little more
convenient.
If your sample weight depends on something other than the sample’s
class – for example, if you’re “hard mining” and want to weight “hard”
samples more heavily – then you would have to use weight and
provide per-sample weights.
Also, if you are using the different, but related loss class, BCELoss
(which you generally shouldn’t be using), you will have to use weight
because, for whatever reason, BCELoss doesn’t have a pos_weight
argument for its constructor.


Also, if i am calculating pos_weight argument for each batch, don’t i still have to instantiate the loss function for each batch to pass the pos_weight argument.


Yes (although, as you note below, it does appear to be possible to
modify  the pos_weight property after BCEWithLogitsLoss has
been constructed).  But, again, my preference is to use the same
pos_weight for the whole dataset, rather than calculate it for each
batch.


Will something like this work:
    weights = torch.FloatTensor ([count_of_lbl0 / count_of_lbl1])
    criterion.pos_weight = weights



I don’t see this discussed in the documentation, but it does appear to
work for me (using pytorch 0.3.0, and weight rather than pos_weight).
I would probably avoiding doing it this way because I’m not sure that
it’s officially supported.
Best.
K. Frank