Skip to content

Keras - Model Evaluation and Prediction

After training a model using model.fit(), the next crucial steps are to evaluate its performance on unseen data and use it to make predictions on new inputs. tf.keras provides dedicated methods for these tasks.

Evaluation assesses how well the trained model generalizes to new data it hasn’t seen during training. This is typically done using a separate test dataset (e.g., x_test, y_test) that was held out from the training process.

The model.evaluate() method computes the loss and any configured metrics (defined during model.compile()) for the provided data.

Syntax:

model.evaluate(
x=None, # Input data (NumPy array, tf.data.Dataset, etc.)
y=None, # Target data (labels)
batch_size=None, # Number of samples per batch of computation
verbose='auto', # 0, 1, or 'auto'. Verbosity mode.
sample_weight=None, # Optional weights for test samples
steps=None, # Total number of steps (batches) before declaring evaluation finished
callbacks=None, # List of tf.keras.callbacks.Callback instances
return_dict=False, # If True, return a dict mapping metric names to values
**kwargs
)

Key Arguments:

  • x: Input data (features) for evaluation.
  • y: True labels or target values corresponding to x.
  • batch_size: How many samples to process at once. Affects memory usage and computation speed.
  • verbose: Controls the logging output (0 = silent, 1 = progress bar).
  • return_dict: If True, returns a dictionary mapping metric names (including ‘loss’) to their calculated values. If False (default), returns a list of scalar values (loss first, then metrics in the order they were specified during compile).

Example (using the MLP model from the ‘Model Compilation’ chapter):

# Assume 'model' is the trained MLP model
# Assume 'x_test', 'y_test' are the preprocessed test data and labels
results = model.evaluate(x_test, y_test, batch_size=128, verbose=1)
print("Test Loss:", results[0])
print("Test Accuracy:", results[1])
# --- Or using return_dict=True ---
results_dict = model.evaluate(x_test, y_test, batch_size=128, return_dict=True)
print("\nEvaluation Results (Dictionary):")
print(results_dict) # e.g., {'loss': 0.075, 'accuracy': 0.982}

The output provides a quantitative measure of the model’s performance on the test set. A high accuracy (for classification) or low loss/MAE (for regression) indicates good generalization.

Once you have a trained and evaluated model that meets your performance criteria, you can use it to make predictions on new, unseen data for which you don’t have labels.

The model.predict() method generates output predictions for the input samples.

Syntax:

model.predict(
x, # Input data (NumPy array, tf.data.Dataset, etc.)
batch_size=None, # Number of samples per batch
verbose='auto', # Verbosity mode
steps=None, # Total number of steps (batches) before stopping
callbacks=None, # List of tf.keras.callbacks.Callback instances
**kwargs
)

Key Arguments:

  • x: Input data for which to generate predictions. Ensure it has the same preprocessing applied as the training data.
  • batch_size: Number of samples to process simultaneously.
  • verbose: Controls logging output.

The output format depends on the model’s final layer:

  • Classification (softmax output): Returns an array of probability distributions, where each row corresponds to an input sample and each column corresponds to a class probability. The shape is typically (num_samples, num_classes).
  • Binary Classification (sigmoid output): Returns an array of probabilities (values between 0 and 1), often with shape (num_samples, 1).
  • Regression (linear output): Returns an array of predicted continuous values, often with shape (num_samples, num_output_features).

Example (using the MLP model for MNIST classification):

import numpy as np
# Assume 'model' is the trained MNIST MLP model
# Assume 'x_test' is the preprocessed test data
# Get predictions for the first 5 test images
predictions_probabilities = model.predict(x_test[:5])
print("Raw Predictions (Probabilities):\n", predictions_probabilities)
# To get the predicted class label, find the index with the highest probability
predicted_labels = np.argmax(predictions_probabilities, axis=1)
print("\nPredicted Labels:", predicted_labels)
# You can compare with the actual labels (if available)
# actual_labels = np.argmax(y_test[:5], axis=1) # Assuming y_test is one-hot encoded
# print("Actual Labels:", actual_labels)

The predict() method is the core function for deploying your model to generate outputs for new, real-world data.