TensorFlow
binary classification
logits to probability
machine learning
neural networks

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.

Practice ML system design

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.

python
1import tensorflow as tf
2
3logits = tf.constant([[-2.0], [0.0], [2.0]], dtype=tf.float32)
4probabilities = tf.nn.sigmoid(logits)
5
6print(probabilities.numpy())

Output:

text
[[0.11920292]
 [0.5       ]
 [0.8807971 ]]

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:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1)  # logits
7])
8
9model.compile(
10    optimizer="adam",
11    loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
12    metrics=["accuracy"]
13)

If the last layer uses activation="sigmoid", then the model already outputs probabilities and from_logits must be False.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1, activation="sigmoid")
7])
8
9model.compile(
10    optimizer="adam",
11    loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),
12    metrics=["accuracy"]
13)

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.

python
1import tensorflow as tf
2
3sample_logits = tf.constant([[1.75], [-0.25]], dtype=tf.float32)
4sample_probs = tf.nn.sigmoid(sample_logits)
5predicted_labels = tf.cast(sample_probs >= 0.5, tf.int32)
6
7print(sample_probs.numpy())
8print(predicted_labels.numpy())

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.sigmoid during 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 - p in 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.