Evaluate many samples
Run inference over a test or validation collection after training.
Keras · Module
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.
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.
Model.predict(x, batch_size=None, verbose="auto", steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False)| Name | Type | Description |
|---|---|---|
x | array, tensor, dataset, or other supported input structure | Input samples, supplied in a structure compatible with the model. |
batch_size | int or None | Number of samples processed in each prediction batch; defaults to automatic behavior when omitted. |
verbose | "auto", 0, or 1 | Controls progress reporting; the default is automatic. |
steps | int or None | Number of batches to process before stopping; useful for iterator-like inputs. |
callbacks | list of callbacks or None | Callbacks invoked during prediction. |
max_queue_size | int | Maximum number of batches queued ahead of processing. |
workers | int | Number of worker processes used for input processing. |
use_multiprocessing | bool | Whether multiprocessing is used for input processing. |
NumPy array(s) of predictions.
predict() for repeated small inputs inside a loop; invoke the model directly instead.tf.function raises a RuntimeError.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.
Run inference over a test or validation collection after training.
Generate outputs for a large unlabeled dataset.
Use a dataset or iterator with a bounded number of batches.
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().
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.
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