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

TensorFlow · Module

model.evaluate

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.

How it works

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.

Syntax

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, Dataset, generator, or SequenceInput data: array-like values, tensors, named-input mappings, a Dataset, generator, or Sequence.
yarray-like or tensorTarget data corresponding to x.
batch_sizeint or NoneNumber of samples per computation batch.
verbose"auto", 0, 1, or 2Verbosity mode.
sample_weightarray-like or NoneSample weights for weighting evaluation results.
stepsint or NoneNumber of batches to evaluate.
callbackslist or NoneCallbacks applied during evaluation.
return_dictboolWhether to return results as a dictionary keyed by metric name.
**kwargskeyword argumentsAdditional arguments accepted by the model implementation.

Returns

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.

Key points

  • The method evaluates the compiled model in test mode and reports the configured loss together with metric results.
  • Evaluation is batch-oriented, and batch_size controls how many samples participate in each computation batch.
  • Array-like inputs are accepted, including lists of arrays for models with multiple inputs.
  • Named-input models can receive a dictionary mapping each input name to its data.
  • Dataset, generator, and Sequence inputs can provide either inputs and targets or those values plus sample weights.
  • steps specifies the maximum number of evaluation batches before the evaluation round ends.

Common mistakes

  • The default return format is a list, so use return_dict=True when consumers should access results by metric name.

Example: Evaluate a compiled model

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.

When to use it

Test-set evaluation

Assess generalization on held-out data after training.

Dataset evaluation

Measure performance on a tf.data pipeline.

Weighted evaluation

Apply per-example weighting during metric computation.

Structured metric output

Consume results in monitoring or reporting code by metric name.

Interview questions

QWhat does Model.evaluate do?

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.

QWhat kinds of input can evaluate accept?

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.

QHow does steps affect evaluation?

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.compile
  • tf.keras.Model.fit
  • tf.keras.Model.predict
  • tf.keras.metrics.Metric

Source: tf.keras.Model. Checked 2026-10-15.