Hi @KFrank,
I am deeply grateful for your kind and clear responses and sincerely appreciate your help.
Your comments clarified most of my problems. I understood that as far as pos_weight and weight parameters are broadcastable to inputs and targets, no more strict consideration about their size is required. Actually, what the PyTorch documentation has mentioned about the weight parameter size seems to be wrong!
So, as you said, the tensor given to the weight parameter is the same as class_weights tensor in my case. Actually, final_loss1 and loss2 are the same here:
num_batch = 2
num_class = 3
targets = torch.randn (num_batch, num_class)
inputs = torch.randn (num_batch, num_class)
class_weights = torch.tensor ([1., 2., 3.])
criterion1 = torch.nn.BCEWithLogitsLoss (reduction = 'none')
criterion2 = torch.nn.BCEWithLogitsLoss (weight = class_weights)

loss1 = criterion1 (inputs, targets)
final_loss1 = (loss1 * class_weights).mean ()

loss2 = criterion2 (inputs, targets)




 KFrank:

Although we often use BCEWithLogitsLoss with target values
(ground-truth labels) that are binary no-yes labels (expressed as
0.0-1.0 floating-point numbers), BCEWithLogitsLoss is more
general and accepts a probabilistic target whose elements are
floating-point values that run from 0.0 to 1.0 and represent the
probability that the sample in question is in class-“1”.
Is this more general case we don’t have samples that are purely
“negative” or “positive,” so, strictly speaking, pos_weight doesn’t
weight the “positive” samples. Rather, it weights the “positive” part of
the binary-cross-entropy formula used for each individual element-wise
loss computation.


It was an interesting comment which I have not considered, but I have a question in this regard.
In this more general case of BCEWithLogitsLoss, how can I calculate the pos_weight tensor exactly?
Also, regarding my first post, was the way of calculating the pos_weight tensor in the case of a binary target correct?



 KFrank:

An aside about terminology: This is not “one-hot” encoding (and, as a
rule of thumb, there’s never really any reason to use one-hot encoding
with pytorch). You have a multi-label use case and your sample labels
are “multi-hot encoded,” if you will.
The term “one-hot encoding” is often used imprecisely, but doing so
can be quite misleading. A one-hot encoded single-label class label
is a vector where exactly one element is 1 and all the others are 0.


Yes. You are right and I made a mistake here.