BCEWithLogitsLoss and model accuracy calculation

I have a simple binary classifier model, but I didn’t use Sigmoid at the end so I’ve trained my model with BCEWithLogitsLoss criterion.
My targets(true labels) is created based on a value like if value >= 0 then target is 1 else 0 .
The question is in testing what should I set as threshold? 0.5 or 0.0 ?

1 Like

Hi Mehdi!

You should use 0.0 as your threshold for class “1”.

Here are the details:

A logit is a sort of score that runs from -infinity to +infinity.
When you run it through the sigmoid function, you get a
probability that runs from 0 to 1. (BCEWithLogitsLoss has,
in effect, a sigmoid function inside of it.) We interpret this
probability as being the probability of class “1”. So we (usually)
convert such a probability to a yes-no prediction by saying if
the probability of being class “1” is greater than 1/2, then we
predict class “1” (and if it is less that 1/2, we predict class “0”).

The sigmoid function maps logits less than 0 to probabilities
less than 1/2 (and logits greater than 0 to probabilities greater
than 1/2, and sigmoid (0) = 1/2).

So, if your probability threshold is 1/2 (as it typically is), then
your logit threshold is 0.

To recap: As you’ve described it, the output of your model is a
logit (because that is what you’ve trained your model to output),
so to convert the output of your model to a yes-no prediction, use
a threshold of 0.


K. Frank


Thanks for your thorough answer!