Batch inference
Run inference over a large NumPy array or tensor collection.
TensorFlow · Module
Generates predictions for input samples using batch processing. For small inputs that fit in one batch, directly calling the model is faster.
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.
predict(x, batch_size=None, verbose='auto', steps=None, callbacks=None)| Name | Type | Description |
|---|---|---|
x | array-like, tensor, dataset, generator, or sequence | Input samples: NumPy arrays or array-like values, tensors, tf.data.Dataset objects, PyDataset instances, generators, or sequences; lists represent multiple inputs. |
batch_size | int or None | Number of samples in each batch; defaults to 32 when omitted for array-like inputs. |
verbose | "auto", 0, 1, or 2 | Reporting mode: "auto", 0 for silent, 1 for a progress bar, or 2 for one line. |
steps | int or None | Maximum number of batches to process before prediction finishes; with None, a dataset runs until exhaustion. |
callbacks | list of keras.callbacks.Callback | Callbacks applied during prediction. |
NumPy array(s) of predictions.
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.
Run inference over a large NumPy array or tensor collection.
Generate outputs from a tf.data.Dataset until it is exhausted or for a fixed number of batches.
Attach prediction lifecycle behavior such as logging or monitoring.
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.
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.
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.fittf.keras.Model.evaluatetf.data.Datasetkeras.utils.PyDatasetkeras.callbacks.Callback