TensorFlow
Machine Learning
Predictions
AI Models
Deep Learning

Making predictions with a TensorFlow model

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Making predictions with TensorFlow is usually straightforward once the model is trained, but the important part is not the one-line predict call. The real work is giving the model input in the same shape, dtype, and preprocessing format it saw during training.

If those conditions drift, prediction quality collapses even when the model loads successfully. So prediction code is really the combination of model loading, preprocessing, inference, and output interpretation.

Load or Reuse the Model

For Keras-style models, a common entry point is:

python
import tensorflow as tf

model = tf.keras.models.load_model("my_model.keras")

If the model is already in memory because you just trained it, you can skip loading and call it directly.

The important point is that the object you use for inference should be the same model architecture and weights that were validated during training.

Prepare Input With the Same Pipeline

Suppose the model was trained on numeric features with shape (batch_size, 2):

python
1import numpy as np
2import tensorflow as tf
3
4x_train = np.array([
5    [0.0, 0.0],
6    [0.0, 1.0],
7    [1.0, 0.0],
8    [1.0, 1.0],
9], dtype="float32")
10
11y_train = np.array([0, 1, 1, 1], dtype="float32")
12
13model = tf.keras.Sequential([
14    tf.keras.layers.Input(shape=(2,)),
15    tf.keras.layers.Dense(8, activation="relu"),
16    tf.keras.layers.Dense(1, activation="sigmoid"),
17])
18
19model.compile(optimizer="adam", loss="binary_crossentropy")
20model.fit(x_train, y_train, epochs=20, verbose=0)

Then a new sample must follow the same shape and dtype expectations:

python
x_new = np.array([[1.0, 0.0]], dtype="float32")
prediction = model.predict(x_new, verbose=0)
print(prediction)

The extra brackets matter. The model expects a batch, even if the batch contains only one sample.

predict Versus Direct Model Calls

Both of these work:

python
pred = model.predict(x_new, verbose=0)

and

python
pred = model(x_new, training=False).numpy()

predict is convenient for batch inference. A direct call can be handy when you are already inside TensorFlow code or want tighter control over the call.

The important part is training=False when you call the model directly. That ensures layers such as dropout and batch normalization behave in inference mode.

Interpret the Output Correctly

The output shape depends on the task.

For binary classification with a sigmoid output:

python
score = float(prediction[0][0])
label = 1 if score >= 0.5 else 0
print(score, label)

For multi-class classification with softmax:

python
1import numpy as np
2
3probabilities = np.array([[0.1, 0.7, 0.2]])
4predicted_class = int(np.argmax(probabilities, axis=1)[0])
5print(predicted_class)

For regression, the raw number is usually the prediction itself rather than a class score.

Preprocessing Is Part of the Model System

The most common inference bug is not a broken TensorFlow call. It is a preprocessing mismatch.

Examples:

  • training used normalized values, inference uses raw values
  • training used one-hot encoded categories, inference uses plain strings
  • training used resized images, inference uses original size

If your preprocessing is not built into the model, make sure the same code path is used in production inference.

Batch Prediction Example

Predicting several samples at once is the normal fast path:

python
1x_batch = np.array([
2    [0.0, 1.0],
3    [1.0, 0.0],
4    [1.0, 1.0],
5], dtype="float32")
6
7predictions = model.predict(x_batch, verbose=0)
8print(predictions)

TensorFlow is optimized for batches, so this is generally better than looping one sample at a time unless the calling context requires streaming.

Common Pitfalls

The biggest pitfall is sending input with the wrong shape. A single sample still needs a batch dimension unless the model specifically expects something else.

Another common problem is forgetting to match training-time preprocessing. A correct model on incorrectly prepared data still gives bad predictions.

People also misread outputs. A sigmoid output is not a class label by itself, and a softmax vector usually needs argmax or threshold logic.

Finally, if you call the model directly, remember inference mode. Omitting training=False can change behavior for some layers.

Summary

  • Load the correct model and keep preprocessing consistent with training.
  • TensorFlow models usually expect batched input, even for one sample.
  • Use model.predict(...) or call the model directly with training=False.
  • Interpret outputs according to task type: classification, multi-class, or regression.
  • Most bad prediction bugs come from input shape or preprocessing mismatches, not the prediction API itself.

Course illustration
Course illustration

All Rights Reserved.