how to convert logits to probability in binary classification in tensorflow?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In TensorFlow binary classification, a logit is the raw score produced before a sigmoid is applied. To turn that score into a probability for the positive class, use the sigmoid function, and make sure the model output, prediction code, and loss function all agree on whether the network emits logits or probabilities.
Apply Sigmoid to Binary Logits
For a single-output binary classifier, the conversion is direct: probability equals tf.nn.sigmoid(logit). TensorFlow applies the mathematical sigmoid element by element.
Output:
A large negative logit gives a probability near zero, zero gives 0.5, and a large positive logit gives a probability near one. If you want the probability of class zero, compute 1.0 - p.
Match the Loss Function to the Output Layer
Most confusion comes from mixing two different model designs.
If the last layer has no activation, the model outputs logits:
If the last layer uses activation="sigmoid", then the model already outputs probabilities and from_logits must be False.
These two styles are both valid, but you must not mix them. Passing sigmoid outputs into a loss configured with from_logits=True will distort training.
Many TensorFlow examples prefer logits during training because BinaryCrossentropy(from_logits=True) uses a numerically stable internal formulation. The practical benefit is that you get the stability of the fused loss during optimization and still convert to probabilities explicitly only when you need them for reporting or thresholding.
Converting Predictions After Inference
If your trained model returns logits, convert them before displaying or thresholding them.
This is especially useful when you want human-readable scores in an API response, notebook, or report. A threshold of 0.5 is common, but it is only a default. In imbalanced problems, you may choose a different threshold based on validation metrics.
One Logit Versus Two-Class Softmax
Binary classification is often implemented with a single logit, not two output neurons. That keeps the model smaller and makes the interpretation clear: one sigmoid output is the probability of the positive class.
You only need a two-logit softmax setup when the rest of the pipeline is explicitly designed around categorical targets and two-class probabilities. For the usual binary-label case, one logit plus sigmoid is the simpler option.
Common Pitfalls
- Applying
tf.nn.sigmoidduring inference even though the model already ends with a sigmoid layer. - Using
BinaryCrossentropy(from_logits=True)with probability outputs. - Treating a single logit as though it were already in the range from zero to one.
- Forgetting that the negative-class probability is
1 - pin a one-logit model. - Mixing one-logit binary classification with a two-class softmax design without a reason.
Summary
- Convert binary logits to probabilities with
tf.nn.sigmoid. - Decide whether the model outputs logits or probabilities and keep that choice consistent.
- If the final layer has no activation, train with
from_logits=True. - If the final layer already uses sigmoid, do not apply sigmoid again.
- Use thresholds on probabilities for labels, and tune the threshold when the data distribution requires it.
Related reading
- How to convert numpy arrays to standard TensorFlow format?
- How to convert numpy arrays to standard TensorFlow format?
- how to convert numpy to tfrecords and then generate batches?
- How to convert pandas dataframe to tensorflow dataset?
- How to convert one-hot encodings into integers?
- How to convert .pb to TFLite format?
- How to convert the half-spaces that constitute a convex hull to a set of extreme points?
- How to count each digit in a range of integers?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.