Hello Rafael!



 auyer:

…
Just placing the BCE in place of the CE trows me this error:
ValueError: Target size (torch.Size([64])) must be the same as input size (torch.Size([64, 2]))
…


Without seeing the rest of your code – your model, in particular –
I’m guessing somewhat, but I believe that your problem could be
the following:
If you have a multi-class problem (where “multi” implies more
than two, and two classes is what we call a “binary” problem)
with nClass classes, then the output of your model should be
nClass logits.  (This is what CrossEntropyLoss expects.)
In contrast, for a binary problem, the output of your model should
be a single logit (not two), conventionally taken to be the logit for
your “positive” class.  This is what BCEWithLogitsLoss expects.
If you build your binary-problem model as a two-class multi-class
model, then you will (redundantly) have two logits as your output
(one for your “negative” class, as well as for your “positive” class).
This won’t match what BCEWithLogitsLoss expects, so I would
think you would get the error you report.
(You can build your binary model as a two-class multi-class
model, but then you should feed the model’s output into
CrossEntropyLoss.  If you match everything up right, you
should get the same results as you would with a conventional
binary model feeding BCEWithLogitsLoss, but you probably
lose a more or less insignificant bit of efficiency.)
Best.
K. Frank