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.
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
- 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'. - 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 aKeyError. - 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
Historyobject returned bymodel.fit()usinghistory.history.keys()to confirm the available keys before plotting.
Related reading
- Poetry fails to install tensorflow
- Poor results with tensorflow DNNClassifier and cross_val_score
- Possible to virtualize NVIDIA GeForce GTX 1070 Graphics Card for Distributed Tensorflow?
- Pre-trained checkpoints .chkpt Vs GraphDef .pb
- Pointers to some good SVM Tutorial
- Pointers to some good SVM Tutorial
- Predict single Image after training model in tensorflow
- Predicting a probability of a sentence using tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.