Hello bfeeny!



 bfeeny:

print(class_weights)
[ 0.50897302 28.36130137]
criterion = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(class_weights, device=device))

RuntimeError: output with shape [136, 1] doesn't match the broadcast shape [136, 2]


Because your output has shape [136, 1], I conclude that your
network has a single output, and you are performing a single-label
(in contrast to multi-label) binary classification problem.
Even though you technically have two classes (the “0” or “no”
class and the “1” or “yes” class), the pos_weight constructor
argument of BCEWithLogitsLoss only takes a single weight,
namely that for the “1” (“positive”) class.
So you want something like:
criterion = nn.BCEWithLogitsLoss(pos_weight=torch.FloatTensor ([28.36 / 0.5090], device=device))

where I’ve chosen to pass in as pos_weight the relative weight
given by what I presume are your weights for class “0” and
class “1”.
Best.
K. Frank