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.
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.
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.
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:
Now model.predict returns logits, not probabilities. Convert them with tf.nn.softmax:
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).
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.
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:
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.predictreturns probabilities only if the model output layer already usessigmoidorsoftmax.' - If the model returns logits, convert them with
tf.nn.sigmoidortf.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
- Predicting the next word using the LSTM ptb model tensorflow example
- Prediction from model saved with tf.estimator.Estimator in Tensorflow
- Prediction is depending on the batch size in Keras
- Preload whole dataset on gpu for training Keras model
- Predicting Values with k-Means Clustering Algorithm
- Prediction After One-hot encoding
- Prefix sums weighted by a polynomial expression, can you do faster?
- Previous power of 2

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.