Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Friday, 3 May 2024

Display Deep Learning Model Training History in Keras

 You can learn a lot about neural networks and deep learning models by observing their performance over time during training.

Keras is a powerful library in Python that provides a clean interface for creating deep learning models and wraps the more technical TensorFlow and Theano backends.

In this post, you will discover how you can review and visualize the performance of deep learning models over time during training in Python with Keras.

Access Model Training History in Keras

Keras provides the capability to register callbacks when training a deep learning model.

One of the default callbacks registered when training all deep learning models is the History callback. It records training metrics for each epoch. This includes the loss and the accuracy (for classification problems) and the loss and accuracy for the validation dataset if one is set.

The history object is returned from calls to the fit() function used to train the model. Metrics are stored in a dictionary in the history member of the object returned.

For example, you can list the metrics collected in a history object using the following snippet of code after a model is trained:

For example, for a model trained on a classification problem with a validation dataset, this might produce the following listing:

You can use the data collected in the history object to create plots.

The plots can provide an indication of useful things about the training of the model, such as:

  • Its speed of convergence over epochs (slope)
  • Whether the model may have already converged (plateau of the line)
  • Whether the mode may be over-learning the training data (inflection for validation line)
  • And more

Need help with Deep Learning in Python?

Take my free 2-week email course and discover MLPs, CNNs and LSTMs (with code).

Click to sign-up now and also get a free PDF Ebook version of the course.

Visualize Model Training History in Keras

You can create plots from the collected history data.

In the example below, a small network to model the Pima Indians onset of diabetes binary classification problem is created. This is a small dataset available from the UCI Machine Learning Repository. You can download the dataset and save it as pima-indians-diabetes.csv in your current working directory (update: download from here).

The example collects the history returned from training the model and creates two charts:

  1. A plot of accuracy on the training and validation datasets over training epochs
  2. A plot of loss on the training and validation datasets over training epochs

The plots are provided below. The history for the validation dataset is labeled test by convention as it is indeed a test dataset for the model.

From the plot of the accuracy, you can see that the model could probably be trained a little more as the trend for accuracy on both datasets is still rising for the last few epochs. You can also see that the model has not yet over-learned the training dataset, showing comparable skill on both datasets.

Plot of Model Accuracy on Train and Validation Datasets

       Plot of model accuracy on train and validation datasets

From the plot of the loss, you can see that the model has comparable performance on both train and validation datasets (labeled test). If these parallel plots start to depart consistently, it might be a sign to stop training at an earlier epoch.

Plot of Model Loss on Training and Validation Datasets

Summary

In this post, you discovered the importance of collecting and reviewing metrics while training your deep learning models.

You learned about the History callback in Keras and how it is always returned from calls to the fit() function to train your models. You learned how to create plots from the history data collected during training.

Do you have any questions about model training history or this post? Ask your question in the comments, and I will do my best to answer.

No comments:

Post a Comment

Connect broadband