Keras
Model Evaluation
Model Prediction
Machine Learning
Deep Learning

Getting different results from Keras model.evaluate and model.predict

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Understanding Discrepancies Between `model.evaluate` and `model.predict` in Keras

Keras is a powerful and widely-used library for constructing deep learning models. It offers several utilities for model training and evaluation, among them `model.evaluate` and `model.predict`. Both functions serve critical, yet fundamentally different purposes. Understanding the discrepancies in results between `model.evaluate` and `model.predict` requires an exploration of their functionalities, inputs, and outputs.

`model.evaluate` vs `model.predict`

  • `model.evaluate`: This function computes the loss and any specified metrics for the model based on given input and target data. It is typically used after training the model to assess its performance on test data. The primary output is a score representing model loss, along with other user-defined metrics.
  • `model.predict`: This function generates predictions from input data, utilizing only the model's learned weights. It serves to output raw prediction scores, probabilities, or classes derived from the input features.

Technical Explanation of Differences

  1. Purpose and Outputs:
    • `model.evaluate` is meant primarily for performance assessment. It calculates and returns the loss value along with any additional metrics such as accuracy, precision, etc.
    • `model.predict` generates predictions, without computing a loss function or any metric unless manually implemented afterward.
  2. Thresholds and Activation Functions:
    • Often, models use activation functions like sigmoid or softmax in their output layer, which convert logits into probabilities.
    • `model.evaluate` applies these activation functions naturally as part of its calculations, but any thresholding for classification (e.g., deciding classes from probabilities) is embedded in metrics (like accuracy).
    • When using `model.predict`, it provides raw predictions or probabilities, requiring a manual threshold application (e.g., 0.5 for sigmoid output) to define class labels.
  3. Batch Processing and Memory:
    • Both `model.evaluate` and `model.predict` can use batching, but their default batch sizes and internal implementations determine performance and potential memory usage.
    • Discrepancies in performance can also stem from variations in how data is split and processed in batches during evaluation and prediction phases.
  4. Loss Function Involvement:
    • In contrast to prediction, evaluation involves the computation of a loss function. A loss quantifies the difference between predicted values and actual target values, which is not calculated by `model.predict`.

Examples Showing Differences

Consider a trained binary classification model:

  • Data Spread: Ensure data pre-processing steps are consistent across training, evaluation, and prediction stages.
  • Batch Size: The choice of batch size could influence results, especially with metrics that aggregate performance over batches.
  • Metric Calculation: Custom metrics defined during `model.evaluate` might not straightforwardly translate to post-prediction evaluations.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.