Hi David!



 davidsriker:

Just another question, at the docs of BCEWithLogitsLoss the weight argument is batch weight.
I wasn’t sure whether to even use it. Did you had some other source for it?


I’m not sure how I concluded that BCEWithLogitsLoss's weight argument
could be used for class weights – maybe from experimenting.  Anyway, I
agree with you that the documentation is inobvious at best, and probably
wrong in some details.
It appears that weight can be used both for batch and class weights.  Note the
dimensions in the following examples that I think sheds some light on what is
going on:
>>> import torch
>>> torch.__version__
'1.7.1'
>>> p = 0.7 * torch.ones ((2, 3))
>>> t = torch.ones ((2, 3))
>>> w3 = 0.5 * torch.ones ((3,))
>>> w2 = 0.5 * torch.ones ((2,))
>>> w23 = 0.5 * torch.ones ((2,3))
>>> w32 = 0.5 * torch.ones ((3,2))
>>> torch.nn.BCEWithLogitsLoss() (p, t)
tensor(0.4032)
>>> torch.nn.BCEWithLogitsLoss (weight = w3) (p, t)
tensor(0.2016)
>>> torch.nn.BCEWithLogitsLoss (weight = w23) (p, t)
tensor(0.2016)
>>> torch.nn.BCEWithLogitsLoss (weight = w2) (p, t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 629, in forward
    return F.binary_cross_entropy_with_logits(input, target,
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/functional.py", line 2582, in binary_cross_entropy_with_logits
    return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)
RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 1
>>> torch.nn.BCEWithLogitsLoss (weight = w32) (p, t)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 629, in forward
    return F.binary_cross_entropy_with_logits(input, target,
  File "/home/user/miniconda3/lib/python3.8/site-packages/torch/nn/functional.py", line 2582, in binary_cross_entropy_with_logits
    return torch.binary_cross_entropy_with_logits(input, target, weight, pos_weight, reduction_enum)
RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 1

Best.
K. Frank