TensorFlow
classifier
probability prediction
machine learning
neural networks

Predicting probabilities in classfier 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, predicting probabilities means asking the model for class confidence scores rather than only the final label. The exact code depends on whether your model already outputs probabilities, such as from softmax or sigmoid, or whether it outputs raw logits that still need a probability transform.

Binary Classification: sigmoid Output

For binary classification, the final layer often has one unit with a sigmoid activation. In that setup, model.predict already returns probabilities between zero and one.

python
1import tensorflow as tf
2import numpy as np
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Dense(16, activation="relu"),
6    tf.keras.layers.Dense(1, activation="sigmoid")
7])
8
9model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
10
11x = np.array([[0.2, 0.8], [0.9, 0.1]], dtype="float32")
12probs = model.predict(x, verbose=0)
13
14print(probs)

Each output value is the model’s estimated probability of the positive class.

Multiclass Classification: softmax Output

For multiclass classification, the final layer often uses softmax. In that case, the output vector is already a probability distribution over classes.

python
1import tensorflow as tf
2import numpy as np
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Dense(32, activation="relu"),
6    tf.keras.layers.Dense(3, activation="softmax")
7])
8
9model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
10
11x = np.array([[1.0, 0.5, 0.2], [0.1, 0.2, 0.9]], dtype="float32")
12probs = model.predict(x, verbose=0)
13
14print(probs)
15print(np.sum(probs, axis=1))

Each row contains class probabilities that sum to one.

If the Model Outputs Logits Instead

Some models intentionally leave off the final activation and return logits. This is common when training with from_logits=True for numerical stability.

Example model:

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Dense(32, activation="relu"),
3    tf.keras.layers.Dense(3)
4])
5
6model.compile(
7    optimizer="adam",
8    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
9    metrics=["accuracy"]
10)

Now model.predict returns logits, not probabilities. Convert them with tf.nn.softmax:

python
1logits = model.predict(x, verbose=0)
2probs = tf.nn.softmax(logits, axis=-1)
3
4print(probs.numpy())

For binary logits, use tf.nn.sigmoid instead.

Check the Output Shape Before Using It

A quick shape check avoids many mistakes. Binary classifiers often return shape (batch_size, 1), while multiclass models return (batch_size, num_classes).

python
predictions = model.predict(x, verbose=0)
print(predictions.shape)

That tells you whether you should flatten a single-column result, run argmax, or apply a probability transform first.

Getting the Predicted Class and Its Probability

A probability vector is often most useful when paired with the winning class.

python
1probs = model.predict(x, verbose=0)
2predicted_class = np.argmax(probs, axis=1)
3predicted_confidence = np.max(probs, axis=1)
4
5print(predicted_class)
6print(predicted_confidence)

This is the usual pattern for top-1 classification output in an application.

Why Probabilities Matter

Probabilities let you apply thresholds and reason about confidence. That matters when a wrong answer is costly or when low-confidence predictions should be reviewed manually.

For example, in binary classification:

python
probs = model.predict(x, verbose=0).reshape(-1)
preds = (probs >= 0.8).astype(int)

That uses a stricter threshold than the usual 0.5, which may improve precision at the expense of recall.

Common Pitfalls

The most common mistake is assuming model.predict always returns probabilities. If the final layer has no activation and the loss was configured with from_logits=True, the outputs are logits.

Another pitfall is applying softmax twice. If the model already ends with softmax, the predictions are already probabilities.

Developers also sometimes take argmax immediately and throw away the probability vector, which removes useful confidence information.

Finally, probability values are not always perfectly calibrated. A model can be accurate while still being overconfident or underconfident, so treat the scores as estimates rather than guarantees.

Summary

  • 'model.predict returns probabilities only if the model output layer already uses sigmoid or softmax.'
  • If the model returns logits, convert them with tf.nn.sigmoid or tf.nn.softmax.
  • Binary classifiers usually output one probability for the positive class.
  • Multiclass classifiers usually output a probability vector across classes.
  • Keep the probability output when you need thresholds, confidence checks, or downstream ranking logic.

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.