Test-set evaluation
Assess generalization on held-out data after training.
TensorFlow · Module
Evaluates a compiled Keras model in test mode on supplied data. It returns the configured loss and metric results, either as a list or as a name-keyed dictionary.
After compilation, evaluate() runs the model in test mode over supplied inputs and targets, computes the configured loss and metrics batch by batch, and returns either an ordered list or a dictionary keyed by metric names.
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, Dataset, generator, or Sequence | Input data: array-like values, tensors, named-input mappings, a Dataset, generator, or Sequence. |
y | array-like or tensor | Target data corresponding to x. |
batch_size | int or None | Number of samples per computation batch. |
verbose | "auto", 0, 1, or 2 | Verbosity mode. |
sample_weight | array-like or None | Sample weights for weighting evaluation results. |
steps | int or None | Number of batches to evaluate. |
callbacks | list or None | Callbacks applied during evaluation. |
return_dict | bool | Whether to return results as a dictionary keyed by metric name. |
**kwargs | keyword arguments | Additional arguments accepted by the model implementation. |
The loss value and metric values for the model in test mode, returned as a list by default or as a dictionary when return_dict=True.
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Input(shape=(4,)),
tf.keras.layers.Dense(1)
])
model.compile(optimizer="adam", loss="mse", metrics=["mae"])
x = tf.random.normal((8, 4))
y = tf.random.normal((8, 1))
model.evaluate(x, y, batch_size=4, verbose=0, return_dict=True)Evaluates the compiled model on batches and returns named loss and metric results.
Assess generalization on held-out data after training.
Measure performance on a tf.data pipeline.
Apply per-example weighting during metric computation.
Consume results in monitoring or reporting code by metric name.
evaluate() measures a compiled model in test mode and returns its loss and metric values. It processes the input in batches, controlled by batch_size.
Follow-up: How can callers make the result self-describing? Set return_dict=True, which returns values keyed by metric names.
x may be array-like data, tensors, a named-input mapping, a Dataset, or a generator/Sequence. Dataset or generator elements should contain inputs and targets, optionally with sample weights.
Follow-up: What additional tuple form is supported? A three-part element can provide inputs, targets, and sample weights.
steps limits the number of batches evaluated. If it is omitted for a Dataset, evaluation runs until that Dataset is exhausted; array inputs do not support steps.
Follow-up: How would you evaluate a fixed portion of a Dataset? Provide an explicit positive steps value to stop after that many batches.
tf.keras.Model.compiletf.keras.Model.fittf.keras.Model.predicttf.keras.metrics.Metric