When do I turn prediction numbers into 1 and 0 for binary classification?

Hi Julia!

In terms of comparing your predictions to the “zeroes and ones” of
your label, BCEWithLogitsLoss does precisely this (without converting
your predictions into 1s and 0s).

BCEWithLogitsLoss takes predictions that are raw-score logits
(such as those produced by your final Linear layer and that run
from -inf to inf) and compares them with ground-truth labels that
are zeros and ones (or more generally, with ground-truth labels that
are probabilities between zero and one). That is, not converting your
predictions (that are logits) into zeros and ones before passing them
to BCEWithLogitsLoss is the correct thing to do. (In this situation,
BCEWithLogitsLoss will likely be your loss function used for training.)

On the other hand, if you wish to compute the accuracy of your
predictions (an evaluation metric that you would most likely not use
for training), that is, the percentage of your yes-no predictions are
correct, you do want to convert your predictions to zeros and ones,
and then simply count how many are equal to your zero-and-one
ground-truth labels.

A logit of 0.0 corresponds to a probability (of being in the “1”-class)
of 0.5, so one would typically threshold the logit against 0.0:

accuracy = ((predictions > 0.0) == labels).float().mean()

Best.

K. Frank

2 Likes