Hi Bruno!



 CasellaJr:

# Compute accuracy
pred_labels = (pred >= 0.5).long() # Binarize predictions to 0 and 1
batch_accuracy = (pred_labels == labels).sum().item()/input.size(0)

And now, accuracy is over 97% 
Do you think is a good solution?


This potentially makes sense.  Let me offer a few words of explanation:
You have a binary classifier that has a single output (per batch
sample).  If this value is the output of your final Linear layer,
without a Sigmoid, it should be understood as a raw-score
logit that runs from -inf to inf and you should feed it into
BCEWithLogitsLoss (preferred).  If you have a Sigmoid then
you are converting the logit to a probability in [0.0, 1.0] and
you should feed it into BCELoss (not preferred).
This probability (after the Sigmoid) is your predicted probability of
your sample being in the “1” or “yes” class.  The probability of your
sample being in the “0” or “no” class is one minus the “1”-class
probability.
How do you covert such a probabilistic prediction into a hard yes-no
prediction?  A reasonable and straightforward approach is to say that
if your predicted probability (for your “1” class) is greater than 1/2,
then you will make a hard prediction of your sample being in the
“1” class.  Conversely, if your predicted probability is less than 1/2,
you make the “0”-class prediction.
So your (pred >= 0.5).long() is thresholding  your probabilistic
prediction, pred, against 1/2 to get a hard prediction.  This is correct
if pred is a probability (that is, if you have a Sigmoid after your final
Linear layer), but is imperfect if pred is a logit (that is, no Sigmoid).
What should you do if pred is a logit?  Note that Sigmoid maps logits
that are negative to probabilities that are less than 1/2 (and positive
logits to probabilities that are greater than 1/2).  So thresholding the
logit against 0 is equivalent to thresholding the corresponding
probability against 1/2.
Therefore if your pred is a logit you should tweak your code to read
(pred >= 0.0).long().
As noted before, BCEWithLogitsLoss is numerically more stable
than BCELoss, so you are better of using BCEWithLogitsLoss
without Sigmoid, having your predictions be logits, and therefore
thresholding your predictions against 0.
Best.
K. Frank