predict() is used purely for inference, generating the model's output for new data where you don't have, or don't want to use, ground-truth labels — unlike evaluate(), which requires labels to compute loss/metrics, predict() only needs input data and simply returns the model's raw output, such as class probabilities for a classifier or continuous values for a regressor. Converting those raw outputs into a final answer, like the single most likely class, typically requires an extra step afterward, such as calling tf.argmax() on the output.
1Understanding model.predict()
predict() is used purely for inference, generating the model's output for new data where you don't have, or don't want to use, ground-truth labels — unlike evaluate(), which requires labels to compute loss/metrics, predict() only needs input data and simply returns the model's raw output, such as class probabilities for a classifier or continuous values for a regressor. Converting those raw outputs into a final answer, like the single most likely class, typically requires an extra step afterward, such as calling tf.argmax() on the output.
predict() returns raw output values, like a full array of class probabilities, not a final answer — for a classifier, you typically still need to call tf.argmax() on the result afterward to get the single predicted class index.
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(3, activation='softmax', input_shape=(4,))])
predictions = model.predict(np.random.rand(2, 4), verbose=0)
print(predictions.shape)2Practical Example
Here is a real-world application of model.predict() showing how it is used in production TensorFlow code.
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(3, activation='softmax', input_shape=(4,))])
predictions = model.predict(np.random.rand(2, 4), verbose=0)
predicted_classes = tf.argmax(predictions, axis=1)
print(predicted_classes.shape)3Best Practices
Follow these guidelines when working with model.predict():
1. Remember predict() only needs input data, no labels, distinguishing it clearly from evaluate(), which requires labels to compute loss/metrics
2. Apply tf.argmax(), or a threshold for binary classification, to predict()'s raw output when you need a final discrete class decision rather than raw probabilities
3. Batch large prediction workloads using the batch_size argument to control memory usage, rather than passing an enormous array all at once
Tip: predict() returns raw output values, like a full array of class probabilities, not a final answer — for a classifier, you typically still need to call tf.argmax() on the result afterward to get the single predicted class index.
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(3, activation='softmax', input_shape=(4,))])
predictions = model.predict(np.random.rand(2, 4), verbose=0)
print(predictions.shape)