Member-only story
Keras KeyError: ‘acc’ and KeyError: ‘val_acc’ Errors in Keras
Fixing the Fixing the KeyError: ‘acc’ and KeyError: ‘val_acc’ Errors in Keras
Have you been using the ‘History’ object returned by the fit() functions of Keras to graph or visualize the training history of your models? And have you been getting a ‘KeyError’ type error such as the following since recent Keras upgrade and wondering why?
Traceback (most recent call last):
File "lenet_mnist_keras.py", line 163, in <module>
graph_training_history(history)
File "lenet_mnist_keras.py", line 87, in graph_training_history
plt.plot(history.history['acc'])
KeyError: 'acc'
Well, this is due to a breaking change introduced in Keras release 2.3.0.
According to the 2.3.0 Release Notes:
“Metrics and losses are now reported under the exact name specified by the user (e.g. if you pass metrics=[‘acc’], your metric will be reported under the string “acc”, not “accuracy”, and inversely metrics=[‘accuracy’] will be reported under the string “accuracy”.”
You can read the official release notes here: https://github.com/keras-team/keras/releases/tag/2.3.0
What this means is that if you specify metrics=[“accuracy”] in the model.compile(), then the history object will have the keys as ‘accuracy’ and ‘val_accuracy’. While if you specify it as metrics=[“acc”] then they will be reported with the keys ‘acc’ and ‘val_acc’.