Adam Optimizer
Machine Learning
Deep Learning
Gradient Descent
Optimization Algorithms

Running Adam Optimizer

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

Running Adam Optimizer is one of the pivotal developments in the area of machine learning, particularly in the optimization of neural networks. The Adam optimizer, standing for Adaptive Moment Estimation, effectively combines the advantages of two other popular optimizers: AdaGrad and RMSProp. This optimizer is designed to work with noisy data and sparse gradients, making it highly relevant for training deep learning models.

Introduction to Adam Optimizer

The Adam optimizer was introduced by Diederik P. Kingma and Jimmy Lei Ba in 2014. It utilizes the power of adaptive learning rates per parameter, which helps in accelerating the convergence of the training process. Adam makes use of two key ideas:

  1. Momentum: Like SGD with momentum, Adam computes an exponentially weighted average of past gradients.
  2. Adaptive Learning Rate: It adjusts learning rates based on the average of past squared gradients.

Mechanisms of Adam Optimizer

Adam is an extension to stochastic gradient descent that computes adaptive learning rates for each parameter. Let's break down the key components of Adam:

Gradient, gtg_t: The gradient of the stochastic objective function at time step t.

Two Moment Estimates:First Moment (Mean) - mtm_t:

m_t=β_1m_t1+(1β_1)g_tm\_t = \beta\_1 \cdot m\_{t-1} + (1 - \beta\_1) \cdot g\_t

This is an exponentially decaying average of past gradients, similar to momentum.

Second Moment (Uncentered Variance) - vtv_t:

v_t=β_2v_t1+(1β_2)g_t2v\_t = \beta\_2 \cdot v\_{t-1} + (1 - \beta\_2) \cdot g\_t^2

This is an exponentially decaying average of the squares of past gradients.

Bias-Corrected Moment Estimates:First Moment:

m^_t=m_t1β_1t\hat{m}\_t = \frac{m\_t}{1 - \beta\_1^t}

Second Moment:

v^_t=v_t1β_2t\hat{v}\_t = \frac{v\_t}{1 - \beta\_2^t}

Parameter Update:

θ_t=θ_t1ηv^_t+ϵm^_t\theta\_t = \theta\_{t-1} - \frac{\eta}{\sqrt{\hat{v}\_t} + \epsilon} \cdot \hat{m}\_t

Here, η\eta is the learning rate and ϵ\epsilon is a small constant to prevent division by zero.

Hyperparameters of Adam

The Adam optimizer has several hyperparameters that can be fine-tuned:

Learning Rate (η\eta): Default is 0.001; this is a step size for moving toward the minima of the cost function.

Exponential Decay Rates: Typically, β1=0.9\beta_1 = 0.9 and β2=0.999\beta_2 = 0.999 are the default.

Epsilon (ϵ\epsilon): A small constant (e.g., 10710^{-7}) added to the denominator to improve numerical stability.

These hyperparameters are essential in diagnosing and troubleshooting training performance in deep neural networks.

Key Advantages of Adam

Adam is widely adopted in deep learning due to several advantages:

Efficient: Adam requires relatively little memory and is computationally efficient.

Invariance: Works well regardless of problem scaling due to its adaptive nature.

Robust: Performs well when dealing with non-stationary objectives, and sparse gradients.

Minimal Hyperparameter Tuning: Default settings are often good enough for most problems.

Comparison with Other Optimizers

OptimizerAdvantagesDisadvantages
SGDSimplicity, works well with large datasetsFixed learning rate, sensitive to initial settings
MomentumAccelerates convergence on steep pathsCan overshoot
AdaGradWell-suited for sparse dataLearning rates can become very small after many updates
RMSPropAdjustable learning rates for each of the parameters over time, good for online learningLearning rates need careful tuning
AdamCombines the advantages of AdaGrad and RMSProp, adapts learning ratesMay not work well with very noisy data

Implementation Example

Here is a basic implementation of the Adam optimizer in Python using TensorFlow:


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the 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

All Rights Reserved.