How to use Callbacks in Keras to Visualize, Monitor and Improve your Deep Learning Model

Yashwanth M
IITG.ai
Published in
6 min readNov 10, 2019

Often, when training a very deep neural network, we want to stop training once the training accuracy reaches a certain desired threshold. Thus, we can achieve what we want (optimal model weights) and avoid wastage of resources (time and computation power). In this brief tutorial, let’s learn how to achieve this in Tensorflow and Keras.

Training Deep Learning models without callbacks is like driving an airplane with no control over speed and altitude — you have little to no control over the whole process that is very likely to result in a disaster. In this article, you will learn how to monitor and improve your Deep Learning models using Keras callbacks like ModelCheckpoint and EarlyStopping.

What is callback in Keras

A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training.

Ok Let’s get started

First I will say how to stop training a neural-network using callback

First, set the accuracy threshold to which you want to train your model.

acc_thresh = 0.96

For implementing the callback first you have to create class and function.

Now, let us implement it to stop training when accuracy reaches acc_thresh.

Now you may wonder what is going on here?

We are creating a new class by extending tf.keras.callbacks.Callback and implementing the on_epoch_end() method which will invoke at the end of an epoch.

Next, we are fetching the value of accuracy at the end of that epoch, and if it is greater than our threshold, we are setting the stop_training of model to True using the “self” keyword to access the attributes and methods of the class in python.

Now, let us create the instance of an object of myCallback class.

callbacks = myCallback()

Next, build a Neural Network or Conv-Net or any model following the normal steps of TensorFlow or Keras. You can pass callbacks (as the keyword argument callbacks) to any of tf.keras.Model.fit(), tf.keras.Model.evaluate(), and tf.keras.Model.predict() methods. The methods of the callbacks will then be called at different stages of training/evaluating/inference.

callbacks=[the newly instantiated object of myCallback class]

model.fit(x_train, y_train, epochs=20, callbacks=[callbacks])

And that’s all! While training, as soon as accuracy reaches the value set in acc_thresh, training will be stopped. In the above example, you can also use on_epoch_begin() which is called at the beginning of an epoch during training.

Saving the model automatically in training.

Let’s say you want to save your model when validation accuracy reaches a minimum in between training before overfitting this can be implemented using the ModelCheckpoint in a callback which can be done as shown below.

Here we are monitoring the validation accuracy say you want training accuracy then just change the monitor value as ‘acc’ as shown below you can also put ‘loss’ for training loss and ‘val_loss’ for validation accuracy and mode can be min or max depending wheater it is accuracy or loss.

LearningRateScheduler

This one is pretty straightforward: it adjusts the learning rate over time using a schedule that you already write beforehand. This function returns the desired learning rate (output) based on the current epoch (epoch index as input).

from keras.callbacks import LearningRateSchedulerlrs = LearningRateScheduler(schedule, verbose=0) # schedule is a function

Early stopping at minimum loss

Overfitting is a nightmare for Machine Learning practitioners. One way to avoid overfitting is to terminate the process early. The EarlyStoppingfunction has various metrics/arguments that you can modify to set up when the training process should stop.

The code example below will define an EarlyStopping function that tracks the val_loss value, stops the training if there are no changes towards val_loss after 3 epochs, and keeps the best weights once the training stops.

monitor is the value being monitored, i.e: val_loss, min_delta is the minimum change in the monitored value and patience is the number of epochs with no improvement after which training will be stopped, restore_best_weights is the set this metric to True if you want to keep the best weights once stopped.

TensorBoard

This is the best of all callbacks. By using a TensorBoard callback, logs will be written to a directory that you can then examine with TensorFlow’s excellent TensorBoard visualization tool.

This line creates a Callback Tensorboard object, you should capture that object and give it to the fit function of your model.

tbCallBack = keras.callbacks.TensorBoard(log_dir=path_to_your_logs, histogram_freq=0, write_graph=True, write_images=False)`
...
model.fit(...inputs and parameters..., callbacks=[tbCallBack])
  • histogram_freq: frequency (in epochs) at which to compute activation and weight histograms for the layers of the model. If set to 0, histograms won't be computed. Validation data (or split) must be specified for histogram visualizations.
  • write_graph: whether to visualize the graph in TensorBoard. The log file can become quite large when write_graph is set to True.
  • write_images: whether to write model weights to visualize as image in TensorBoard.

This way you gave your callback object to the function. It will be run during the training and will output files that can be used with tensorboard.

If you want to visualize the files created during training, run in your terminal.

tensorboard --logdir=path_to_your_logs

LambdaCallback

If you want to create your own callback then you can use LambdaCallback. This allows you to trigger events when an epoch, batch, or training process begins or ends.

Here is an example of it.

This is the image of the code. The code is at the link given below.

As you can see above I have created a custom callback using LambdaCallback to show samples at the epoch end. And I have also illustrated the use of multiple callbacks in the above example. You can also see multiple examples of custom callbacks implemented in the main code at the link given below.

It’s a good idea to use more than one of these callbacks. I use ModelCheckpoint and RemoteMonitor and TensorBoard mainly. Callbacks like EarlyStopping and LearningRateScheduler are great for optimization. This is not the complete list but I have covered almost most of it.

I would like to hear about how you implemented and how it helped you to train your model in the comments. Also, don’t forget to clap if you found the article helpful and follow IITG.ai( the first student run AI and ML society of IITG) for more articles and updates.

Links and References:

--

--