Keras How come 'accuracy' is higher than '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.
Introduction
Keras, a powerful and user-friendly high-level neural networks API, has become a popular tool for developing deep learning models. It operates on top of frameworks such as TensorFlow, Theano, or CNTK, and simplifies the process of constructing complex machine learning models. A common confusion that arises during the training of deep learning models is the discrepancy between training accuracy (accuracy) and validation accuracy (val_acc). This article will delve into the technical reasons for this phenomenon, backed by examples, and provide essential insights into interpreting these metrics.
Understanding Accuracy and Validation Accuracy
Definitions
- Accuracy in the context of training is the measure of how well your model predicts the training data. It's calculated as the number of correct predictions divided by the total number of predictions made.
- Validation Accuracy (
val_acc), on the other hand, is the measure of your model's predictive performance on a separate dataset—the validation set—that was not used during the training of the model.
Accuracy > Validation Accuracy
Several reasons account for the situation where "accuracy" is greater than "val_acc":
- Overfitting: This is the most common reason. The model learns the training data so well that it starts memorizing it rather than generalizing from it. This leads to high training accuracy but poor generalization to the validation set.
- Data Imbalance: If the training set and validation set have different class distributions, it might lead to this discrepancy.
- Data Leakage: Unintended inclusion of validation data in the training dataset can show artificially inflated training accuracy.
- Insufficient Validation Data: A small validation dataset might not be representative of the actual data distribution.
- Hyperparameter Choices: Regularization, model complexity, learning rate, and other hyperparameters can lead to differences in how well the model generalizes.
Technical Explanation
Example of Overfitting
Consider a multi-layer neural network trained on the famous MNIST dataset. Suppose the architecture is significantly complex (e.g., many layers with numerous neurons), and a very high training accuracy is attained within a few epochs. However, if this complexity becomes excessive relative to the data amount and diversity, the model begins capturing the noise and patterns in the training data exclusive to that dataset.
- Training Evaluation: The accuracy on the training data increases with epochs:
- 1st Epoch: 82%
- 3rd Epoch: 95%
- 5th Epoch: 99%
- Validation Evaluation: The accuracy on unseen validation data:
- 1st Epoch: 80%
- 3rd Epoch: 92%
- 5th Epoch: 88%
This example demonstrates the training accuracy surpassing validation accuracy as the model starts overfitting at the 5th epoch, capturing irrelevant patterns in the training set.
Steps to Mitigate Overfitting
- Regularization Techniques: Use L1 or L2 regularization and dropout layers during training.
- Simplify the Model: Reduce the number of layers or neurons, thus reducing model complexity.
- Increase the Dataset Size: Sometimes, more data is needed to help the model generalize better.
- Data Augmentation: Apply transformations like rotation, flipping, or scaling to artificially enhance the dataset size and diversity.
Example Code
Here is a minimal Keras example illustrating training and validation accuracy:
Related reading
- Keras How is Accuracy Calculated for Multi-Label Classification?
- Keras How should I prepare input data for RNN?
- Keras How to feed input directly into other hidden layers of the neural net than the first?
- Keras How to get layer shapes in a Sequential model
- Keras, How to get the output of each layer?
- Keras, How to get the output of each layer?
- Keras how to get tensor dimensions inside custom loss?
- Keras How To Resume Training With Adam Optimizer
.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.