Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Module

model.predict

model.predict() runs inference on input samples in batches and returns NumPy prediction array(s). It is intended for batch processing of large inputs, not repeated tiny calls inside loops.

How it works

Call model.predict() with input samples to run inference in batches and obtain NumPy predictions. It is intended for processing a dataset or another large collection of inputs; for a single small step inside a loop, calling the model directly is generally the more suitable pattern.

Syntax

Model.predict(x, batch_size=None, verbose="auto", steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)

Parameters

NameTypeDescription
xarray, tensor, dataset, or other supported input structureInput samples, supplied in a structure compatible with the model.
batch_sizeint or NoneNumber of samples processed in each prediction batch; defaults to automatic behavior when omitted.
verbose"auto", 0, or 1Controls progress reporting; the default is automatic.
stepsint or NoneNumber of batches to process before stopping; useful for iterator-like inputs.
callbackslist of callbacks or NoneCallbacks invoked during prediction.
max_queue_sizeintMaximum number of batches queued ahead of processing.
workersintNumber of worker processes used for input processing.
use_multiprocessingboolWhether multiprocessing is used for input processing.

Returns

NumPy array(s) of predictions.

Key points

  • The method maps supplied input samples to model outputs.
  • Prediction work is divided into batches rather than processed as one undifferentiated collection.
  • Use it for dataset-scale or otherwise large-batch inference.
  • The returned prediction value is a NumPy array or collection of NumPy arrays.
  • Input interpretation follows the same rules as the model's fitting and evaluation methods.
  • The documentation identifies wrapping the method in a TensorFlow function as an error condition.

Common mistakes

  • Do not use predict() for repeated small inputs inside a loop; invoke the model directly instead.
  • Wrapping this method in tf.function raises a RuntimeError.
  • For stateful models, the sample count must be divisible by the selected batch size.

Example: Batch prediction

import numpy as np
import keras

model = keras.Sequential([
    keras.layers.Input(shape=(4,)),
    keras.layers.Dense(1),
])

x = np.random.random((8, 4))
predictions = model.predict(x, batch_size=4, verbose=0)
print(predictions.shape)

Runs batched inference on eight samples and returns their predictions as a NumPy array.

When to use it

Evaluate many samples

Run inference over a test or validation collection after training.

Batch offline inference

Generate outputs for a large unlabeled dataset.

Iterator-based prediction

Use a dataset or iterator with a bounded number of batches.

Interview questions

QWhat is the intended use of model.predict()?

predict() performs inference over the input in batches and returns NumPy value(s) containing the predictions. Its design targets batch processing of large input collections rather than tiny repeated calls.

Follow-up: How would you handle one sample per iteration? Call the model directly instead of repeatedly invoking predict().

QWhat input compatibility rule does predict() share with training and evaluation?

The input must follow the same interpretation rules used by fit() and evaluate(). This means the structure and meaning of the supplied inputs must be unambiguous across all three APIs.

Follow-up: What failure can result from an ambiguous input? The supplied data may not match the model's expected input structure, producing a ValueError.

QWhich controls can change how prediction is executed?

The method accepts batching controls such as batch_size and can report progress through verbose. It also accepts a finite number of steps and prediction callbacks for input pipelines that expose those controls.

Follow-up: What special constraint applies to stateful models? The number of samples must be an exact multiple of the batch size.

  • Model.fit()
  • Model.evaluate()
  • Model.__call__()
  • keras.callbacks.Callback

Source: Model training APIs. Checked 2026-10-15.