evaluate() runs the model over the provided data in inference mode, computing the same loss and metrics configured in compile(), but without updating any weights — it's the standard way to check how a model performs on data it was never trained on, most commonly a test set kept completely separate from both training and validation data. Unlike fit()'s validation_data, which is checked repeatedly during training to monitor progress, evaluate() is typically called once, after training is fully complete, for a final, unbiased performance measurement.
1Understanding model.evaluate()
evaluate() runs the model over the provided data in inference mode, computing the same loss and metrics configured in compile(), but without updating any weights — it's the standard way to check how a model performs on data it was never trained on, most commonly a test set kept completely separate from both training and validation data. Unlike fit()'s validation_data, which is checked repeatedly during training to monitor progress, evaluate() is typically called once, after training is fully complete, for a final, unbiased performance measurement.
Keep your test set completely separate from both training and validation data, and only run evaluate() on it once training and all hyperparameter tuning are fully finished — checking it repeatedly during development risks unconsciously tuning choices to fit the test set too, defeating its purpose as an unbiased final check.
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse', metrics=['mae'])
model.fit(np.array([1, 2, 3, 4]), np.array([2, 4, 6, 8]), epochs=50, verbose=0)
results = model.evaluate(np.array([5, 6]), np.array([10, 12]), verbose=0)
print(len(results))2Practical Example
Here is a real-world application of model.evaluate() showing how it is used in production TensorFlow code.
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse', metrics=['mae'])
model.fit(np.array([1, 2, 3, 4]), np.array([2, 4, 6, 8]), epochs=50, verbose=0)
loss, mae = model.evaluate(np.array([5, 6]), np.array([10, 12]), verbose=0)
print(loss >= 0 and mae >= 0)3Best Practices
Follow these guidelines when working with model.evaluate():
1. Reserve a genuinely separate test set that's never used during training or validation-based tuning, and only call evaluate() on it once, at the very end
2. Match the batch_size used in evaluate() to a size that fits comfortably in memory, since it doesn't affect the computed loss/metric values themselves, only computation speed
3. Read evaluate()'s returned list in the same order as the metrics configured in compile(), with loss always first
Tip: Keep your test set completely separate from both training and validation data, and only run evaluate() on it once training and all hyperparameter tuning are fully finished — checking it repeatedly during development risks unconsciously tuning choices to fit the test set too, defeating its purpose as an unbiased final check.
import tensorflow as tf
import numpy as np
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer='sgd', loss='mse', metrics=['mae'])
model.fit(np.array([1, 2, 3, 4]), np.array([2, 4, 6, 8]), epochs=50, verbose=0)
results = model.evaluate(np.array([5, 6]), np.array([10, 12]), verbose=0)
print(len(results))