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

TensorFlow · Module

model.predict

Generates predictions for input samples using batch processing. For small inputs that fit in one batch, directly calling the model is faster.

How it works

Call predict with batched input samples to obtain model outputs as NumPy arrays. TensorFlow processes the input in batches, while small inputs that already fit in one batch can usually be passed directly to the model for faster inference.

Syntax

predict(x, batch_size=None, verbose='auto', steps=None, callbacks=None)

Parameters

NameTypeDescription
xarray-like, tensor, dataset, generator, or sequenceInput samples: NumPy arrays or array-like values, tensors, tf.data.Dataset objects, PyDataset instances, generators, or sequences; lists represent multiple inputs.
batch_sizeint or NoneNumber of samples in each batch; defaults to 32 when omitted for array-like inputs.
verbose"auto", 0, 1, or 2Reporting mode: "auto", 0 for silent, 1 for a progress bar, or 2 for one line.
stepsint or NoneMaximum number of batches to process before prediction finishes; with None, a dataset runs until exhaustion.
callbackslist of keras.callbacks.CallbackCallbacks applied during prediction.

Returns

NumPy array(s) of predictions.

Key points

  • Use predict to obtain the model's outputs for supplied samples.
  • Prediction is batch-oriented, so inputs are processed in groups rather than necessarily all at once.
  • It is a suitable API for running inference over substantial input collections.
  • For a small batch, calling the model directly is faster than using predict.
  • Array-like inputs use 32 samples per batch unless another batch size is supplied.
  • The callbacks argument accepts callback objects that run during prediction.

Common mistakes

  • Avoid repeatedly calling predict for small inputs inside a loop; invoke the model directly instead.
  • Leave batch_size unset for dataset, generator, and PyDataset inputs because those inputs produce their own batches.

Example: Predict a batch of samples

import numpy as np
import tensorflow as tf

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

samples = np.array([[1.0, 2.0], [3.0, 4.0]])
predictions = model.predict(samples, verbose=0)
print(predictions.shape)

Creates a two-input model, predicts a two-sample batch, and prints the prediction shape.

When to use it

Batch inference

Run inference over a large NumPy array or tensor collection.

Dataset prediction

Generate outputs from a tf.data.Dataset until it is exhausted or for a fixed number of batches.

Prediction callbacks

Attach prediction lifecycle behavior such as logging or monitoring.

Interview questions

QWhat does Model.predict do, and when is it appropriate?

predict generates output predictions for input samples and performs computation in batches. It is intended for batch processing of large inputs, and its result is a NumPy array or collection of arrays.

Follow-up: What should you use for a small input batch? Call the model directly, such as model(x) or model(x, training=False), which avoids predict's extra batch-processing path.

QWhat input forms and batching rules does predict support?

The x argument may be a NumPy array or array-like value, a tensor, a tf.data.Dataset, or a PyDataset; multiple inputs can be represented by lists. Dataset-like sources should not receive an explicit batch_size.

Follow-up: How does an exhausted dataset differ from a bounded prediction run? With steps=None, prediction continues until a tf.data.Dataset is exhausted; setting steps limits the number of batches.

QHow do verbose, steps, and callbacks affect prediction?

verbose controls reporting: 0 is silent, 1 displays a progress bar, and 2 displays one line per epoch-like run. steps specifies how many batches to process, while callbacks supplies prediction callbacks.

Follow-up: When is steps unnecessary? For a tf.data.Dataset, leaving steps as None runs prediction until the dataset is exhausted.

  • tf.keras.Model.__call__
  • tf.keras.Model.fit
  • tf.keras.Model.evaluate
  • tf.data.Dataset
  • keras.utils.PyDataset
  • keras.callbacks.Callback

Source: tf.keras.Model. Checked 2026-10-15.