🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

model.predict()

AI & DATA SCIENCE // model-predict

model.predict() runs a trained model on new input data and returns its raw output predictions, without requiring corresponding labels.

Syntax

model.predict(x, batch_size=None)

Deep Dive Course

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.

editor.html
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)
localhost:3000

2Practical Example

Here is a real-world application of model.predict() showing how it is used in production TensorFlow code.

editor.html
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)
localhost:3000

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.

editor.html
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)
localhost:3000

Examples

Example 01Basic Usage
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)
Example 02Advanced Example
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)

Best Practices

  • Remember predict() only needs input data, no labels, distinguishing it clearly from evaluate(), which requires labels to compute loss/metrics
  • 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
  • Batch large prediction workloads using the batch_size argument to control memory usage, rather than passing an enormous array all at once

Interview Question

Why does model.predict() not require labels as an argument, while model.evaluate() does?

Hint: Think about what each function is actually computing, and whether that computation needs a ground-truth answer to compare against.

predict() only runs the forward pass of the model to generate its output for given inputs — it never needs to know the correct answer, since it isn't computing any measure of correctness, just producing the model's own output. evaluate(), on the other hand, computes loss and metric values, both of which are fundamentally comparisons between the model's predictions and the true, correct labels — without labels there is nothing to compare the predictions against, so evaluate() requires them as a necessary input, while predict() has no use for them at all, which is exactly why it's the function you use in production or real-world deployment, generating predictions for genuinely new data where the correct answer isn't known yet.

Exercises

MediumPractice using model.predict() in a real scenario.
View Solution
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)

Frequently Asked Questions

Why does model.predict() not require labels as an argument, while model.evaluate() does?

predict() only runs the forward pass of the model to generate its output for given inputs — it never needs to know the correct answer, since it isn't computing any measure of correctness, just producing the model's own output. evaluate(), on the other hand, computes loss and metric values, both of which are fundamentally comparisons between the model's predictions and the true, correct labels — without labels there is nothing to compare the predictions against, so evaluate() requires them as a necessary input, while predict() has no use for them at all, which is exactly why it's the function you use in production or real-world deployment, generating predictions for genuinely new data where the correct answer isn't known yet.

Related Functions

model-evaluatetf-argmaxmodel-fit