keras
keyerror
learning-curve
val_acc
machine-learning

Plotting learning curve in keras gives KeyError 'val_acc'

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

Plotting learning curves is a common practice in machine learning to evaluate the performance of a model during training. It provides visibility into metrics such as accuracy or loss over time, both for the training and validation datasets. A common issue that arises when plotting learning curves in Keras is the KeyError: 'val_acc' , which can often stem from a misunderstanding of the key naming conventions used by Keras.

Understanding KeyError: 'val_acc'

In Keras, when you train a model using the model.fit() function, a History object is returned. This object contains the logs of all metrics calculated on both training and validation datasets at each epoch. To access this data, the keys corresponding to these metrics are used:

  • 'acc' or 'accuracy' for training accuracy
  • 'val_acc' or 'val_accuracy' for validation accuracy
  • Similarly named keys exist for other metrics like 'loss' , 'val_loss' , etc.

Common Causes for KeyError

  1. Version Compatibility: The key naming conventions have changed between different versions of Keras. In the earlier versions of Keras (before 2.1.0), 'acc' and 'val_acc' were used as keys for accuracy and validation accuracy, respectively. With newer versions, these keys have been changed to 'accuracy' and 'val_accuracy' .
  2. Custom Metrics: If you have defined custom metrics without proper naming or have overridden metric names, these would not appear under 'val_acc' and will cause a KeyError .
  3. Lack of Validation Data: If validation data is not supplied to model.fit() , then the History object will not contain validation-specific keys like 'val_accuracy' .

Example of Plotting Learning Curves

Let's assume you are using a version of Keras that uses the newer key naming conventions and you have a simple binary classification model:

  • Custom Callbacks: If you are using Keras Callbacks, you can add custom functionality for capturing metrics or modifying the defaul.t_history behavior. This can be useful if the default settings do not meet your needs.
  • Error Handling: Wrap your plotting code in try-except blocks to handle this error gracefully:
  • Inspecting the History Object: It is helpful to explore the History object returned by model.fit() using history.history.keys() to confirm the available keys before plotting.

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.