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

Keras · Module

model.evaluate

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.

How it works

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.

Syntax

Model.evaluate(x=None, y=None, batch_size=None, verbose="auto", sample_weight=None, steps=None, callbacks=None, return_dict=False, **kwargs)

Parameters

NameTypeDescription
xarray-like, tensor, dict, PyDataset, Dataset, DataLoader, or generatorInput samples or a supported data source yielding inputs, targets, and optionally sample weights.
yarray-like, tensor, or NoneTarget values; omit when x supplies targets.
batch_sizeint or NoneNumber of samples in each computation batch.
verbose"auto", 0, or 1Display mode for evaluation progress.
sample_weightarray-like or NonePer-sample weighting values.
stepsint or NoneNumber of batches to process before stopping.
callbackslist or NoneCallbacks invoked during evaluation.
return_dictboolWhether to return named results rather than a list.

Returns

Scalar test loss when there is one output and no metrics, otherwise scalar results for the loss and metrics in list or dictionary form.

Key points

  • Evaluation runs the compiled model in test mode and reports its loss plus configured metrics.
  • Use batch_size to control how many samples are processed per evaluation batch.
  • For models with named inputs, pass a dictionary keyed by input name.
  • A Keras dataset can yield inputs and targets, optionally adding sample weights.
  • The default result format is a list of scalar outputs.
  • Setting return_dict=True returns results under metric-name keys.

Common mistakes

  • When x is a supported dataset or generator, omit y; the targets must come from the data source.
  • Use return_dict=True when names are safer than relying on positional result ordering.
  • If steps is set, evaluation stops after that many batches rather than necessarily consuming the complete input source.

Example: Evaluate with named results

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.

When to use it

Test-set assessment

After training, measure generalization on held-out inputs and targets.

Pipeline evaluation

Evaluate a model directly from a Keras, TensorFlow, or PyTorch input pipeline.

Structured result reporting

Retrieve metric values by stable names for logging or downstream checks.

Interview questions

QWhat does 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.

QWhat forms of input data can 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.

QHow does 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.compile
  • Model.fit
  • Model.predict
  • model.metrics_names
  • keras.utils.PyDataset

Source: Keras documentation: Model training APIs. Checked 2026-10-15.