Test-set assessment
After training, measure generalization on held-out inputs and targets.
Keras · Module
Evaluates a compiled Keras model in test mode and returns its loss and configured metric values. It accepts arrays, tensors, named-input mappings, and supported data pipelines, with optional batching, weighting, callbacks, and named results.
After a model is compiled, evaluate() runs it in test mode over input and target data, processing samples in batches. It returns the computed loss together with configured metric values, either as an ordered list or, when requested, as a dictionary keyed by metric names.
Model.evaluate(x=None, y=None, batch_size=None, verbose="auto", sample_weight=None, steps=None, callbacks=None, return_dict=False, **kwargs)| Name | Type | Description |
|---|---|---|
x | array-like, tensor, dict, PyDataset, Dataset, DataLoader, or generator | Input samples or a supported data source yielding inputs, targets, and optionally sample weights. |
y | array-like, tensor, or None | Target values; omit when x supplies targets. |
batch_size | int or None | Number of samples in each computation batch. |
verbose | "auto", 0, or 1 | Display mode for evaluation progress. |
sample_weight | array-like or None | Per-sample weighting values. |
steps | int or None | Number of batches to process before stopping. |
callbacks | list or None | Callbacks invoked during evaluation. |
return_dict | bool | Whether to return named results rather than a list. |
Scalar test loss when there is one output and no metrics, otherwise scalar results for the loss and metrics in list or dictionary form.
batch_size to control how many samples are processed per evaluation batch.return_dict=True returns results under metric-name keys.x is a supported dataset or generator, omit y; the targets must come from the data source.return_dict=True when names are safer than relying on positional result ordering.steps is set, evaluation stops after that many batches rather than necessarily consuming the complete input source.import keras
import numpy as np
model = keras.Sequential([
keras.layers.Input(shape=(4,)),
keras.layers.Dense(1),
])
model.compile(optimizer="adam", loss="mse", metrics=["mae"])
model.fit(np.zeros((8, 4)), np.zeros((8, 1)), epochs=1, verbose=0)
results = model.evaluate(
np.zeros((2, 4)), np.zeros((2, 1)), return_dict=True, verbose=0
)
print(results)Compiles, briefly trains, and evaluates a model while returning named loss and metric values.
After training, measure generalization on held-out inputs and targets.
Evaluate a model directly from a Keras, TensorFlow, or PyTorch input pipeline.
Retrieve metric values by stable names for logging or downstream checks.
Model.evaluate() return?evaluate() measures a compiled model in test mode and returns its loss and metric values. It processes the data in batches, controlled by batch_size.
Follow-up: How would you make result consumption robust when metrics change? Set return_dict=True, then read values by metric name instead of assuming fixed list positions.
evaluate() consume?x accepts arrays, backend tensors, named-input dictionaries, supported datasets, data loaders, and generator functions. y is supplied separately for array or tensor inputs, but should be omitted when the input source already yields targets.
Follow-up: How can evaluation include per-sample weighting? Provide sample_weight, or have the dataset yield inputs, targets, and sample weights.
return_dict change the result?return_dict=False produces a list of scalar results, while return_dict=True produces a dictionary whose keys are metric names. The output contains the scalar test loss and configured metric results.
Follow-up: How do you determine labels for positional results? Use the model's metric-name labels when consuming the list form.
Model.compileModel.fitModel.predictmodel.metrics_nameskeras.utils.PyDataset