Keras
Adam optimizer
learning rate
epochs
machine learning

How can I print the Learning Rate at each epoch with Adam optimizer in Keras?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The Adam optimizer is a popular choice for training neural networks due to its efficiency and effectiveness in adjusting learning rates on a per-parameter basis. However, analyzing how the learning rate changes during training can provide insights to improve model performance. In Keras, you can easily access and print the learning rate at each epoch even when using the Adam optimizer. This article will guide you through the process of doing this, including technical explanations and a practical example.

Understanding the Adam Optimizer

The Adam optimizer combines the advantages of two other extensions of stochastic gradient descent (SGD): AdaGrad and RMSProp. Specifically, Adam uses adaptive learning rates and momentum, resulting in faster convergence and better optimization of the stochastic objectives.

In Adam:

  • Learning rates are computed individually for each parameter at each time step.
  • The algorithm keeps track of an exponentially decaying average of past gradients, similarly to momentum.
  • It also maintains an exponentially decaying average of past squared gradients, which is akin to AdaGrad.

The learning rate in Adam is dynamically adjusted, and understanding its adaptivity can be helpful in tuning hyperparameters or diagnosing training issues.

How to Print the Learning Rate in Keras

In order to print the learning rate at each epoch, you can use a custom callback in Keras. But first, ensure you correctly understand how to initialize and modify the learning rate parameter within the Adam optimizer setup.

Using Callbacks in Keras

Keras callbacks are powerful tools that can be applied at different stages during training to receive updates, inspect models, and modify parameters. Here, we'll create a custom callback to print the learning rate after each epoch.

Step-by-Step Guide

  1. Initialize the Adam Optimizer: When you initialize the Adam optimizer, you can specify the learning rate. This will serve as the starting point for all adjustments. For instance:

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design