How to plot a learning curve for a keras experiment?
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
A learning curve shows how your model behaves over training epochs, usually by plotting loss and one or more metrics for both training and validation data. In Keras, the data you need is already returned by model.fit, so plotting a useful curve is mostly about saving the History object and charting the right keys.
Training a Model and Capturing History
model.fit returns a History instance whose history attribute is a dictionary. Each key stores the per-epoch values for a metric such as loss, val_loss, accuracy, or val_accuracy.
The example below trains a small binary classifier on synthetic data and records the training history:
That final print is useful because it tells you the exact metric names available for plotting.
Plotting Loss and Accuracy
Once you have the history object, use Matplotlib to draw the curves:
This gives you the standard learning-curve view most teams use during experimentation.
Reading the Curve Correctly
A plot is only useful if you interpret it well:
- If training loss keeps dropping but validation loss starts rising, the model is likely overfitting.
- If both training and validation metrics stay poor, the model may be underfitting.
- If both curves improve and remain relatively close, the model is usually learning something that generalizes.
Learning curves also help you spot unstable training. Large oscillations can mean the learning rate is too high, the batch size is poorly chosen, or the data pipeline is noisy.
Making the Plot More Useful
For real experiments, you often want more than a raw plot. Early stopping is a common addition because it marks the best validation point:
If you log custom metrics, they will appear in history.history too. For example, regression experiments may include mae, while multiclass models may report sparse categorical accuracy.
Common Pitfalls
- Forgetting to pass
validation_datameans you can only plot training curves, which hides overfitting. - Hard-coding metric names can fail because some tasks use
sparse_categorical_accuracy,mae, or other names instead of plainaccuracy. - Comparing curves across experiments without matching batch size, learning rate, and data split can be misleading.
- Judging a model from a single noisy run often leads to the wrong conclusion about convergence.
- Treating a nice-looking training curve as proof of real-world performance ignores the need for a separate test set.
Summary
- Keras already records per-epoch loss and metric values in the
Historyobject returned bymodel.fit. - Plot both training and validation curves to understand generalization, not just optimization.
- Use the exact keys in
history.historywhen building the chart. - Interpret divergence between training and validation curves as a signal about overfitting or underfitting.
- Add callbacks such as early stopping when you want the plot to reflect real experiment control decisions.
Related reading
- How to Plot and save a tensor as an image in Tensorflow
- How to plot grid of images in tensorboard?
- how to plot the tensorflow neural network object
- How to predict a function/table using Keras?
- How to plot gradient descent using plotly
- How to Plot PR-Curve Over 10 folds of Cross Validation in Scikit-Learn
- How to plot in multiple subplots
- How to plot multiple dataframes in subplots
.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.