tf.keras.optimizers
Adam optimizer
optimization techniques
machine learning
deep learning

tf.keras.optimizers.Adam and other optimizers with minimization

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

In the realm of machine learning, optimization algorithms play a pivotal role in training models by minimizing the loss function. They adjust the model parameters to reduce errors and improve performance. TensorFlow's Keras API provides several optimizers, each with unique features and applications. This article delves into `tf.keras.optimizers.Adam` and other popular optimizers, their mechanisms, advantages, and potential use cases.

Learning about Optimizers

Stochastic Gradient Descent (SGD)

Stochastic Gradient Descent is the foundation of many optimizers. It updates model parameters in the opposite direction of the gradient of the loss function with respect to the parameters. The size of these updates is determined by the learning rate.

Mathematical Formulation: θ=θη_θJ(θ)\theta = \theta - \eta \nabla\_\theta J(\theta)

where: • θ\theta: model parameters • η\eta: learning rate • J(θ)J(\theta): loss function

While SGD is straightforward, it can be inefficient due to challenges like slow convergence and getting trapped in local minima.

Adam Optimizer

The Adam (Adaptive Moment Estimation) optimizer is an improvement over SGD, offering efficient training for deep neural networks with minimal parameter tuning. It combines the advantages of the AdaGrad and RMSProp algorithms, utilizing both adaptive learning rates and momentum.

Features of Adam: • Maintains an exponentially decaying average of past gradients (momentum) and squared gradients. • Utilizes these averages to compute adaptive learning rates for each parameter.

Mathematical Formulation:

  1. Compute the moving averages: m_t=β_1m_t1+(1β_1)g_tm\_t = \beta\_1 m\_{t-1} + (1 - \beta\_1)g\_t v_t=β_2v_t1+(1β_2)g_t2v\_t = \beta\_2 v\_{t-1} + (1 - \beta\_2)g\_t^2
  2. Bias correction: m^_t=m_t1β_1t\hat{m}\_t = \frac{m\_t}{1 - \beta\_1^t} v^_t=v_t1β_2t\hat{v}\_t = \frac{v\_t}{1 - \beta\_2^t}
  3. Parameter update: θ_t=θ_t1αv^_t+ϵm^_t\theta\_t = \theta\_{t-1} - \frac{\alpha}{\sqrt{\hat{v}\_t} + \epsilon}\hat{m}\_t

Where: • gtg_t is the gradient at time tt. • β1,β2\beta_1, \beta_2 are exponential decay rates for moment estimates. • α\alpha is the learning rate. • ϵ\epsilon is a small constant to prevent division by zero.

Other Optimizers

RMSProp

RMSProp (Root Mean Square Propagation) optimizer, developed to address AdaGrad’s diminishing learning rates, uses the exponential average of squared gradients.

Characteristics: • Suitable for nonstationary objectives. • Maintains per-parameter learning rates, adapting based on the moving average of recent squared gradients.

AdaGrad

AdaGrad adapts learning rates for each parameter based on past gradients. Frequently used in sparse data and natural language processing.

Notable Attribute: • Learning rate diminishes over updates, potentially becoming excessively small in long training runs, which can stall further learning.

AdaDelta

AdaDelta is an improvement over AdaGrad, addressing its diminishing learning rate issue by calculating updates based on the moving window of gradients.

Features: • Eliminates the need for an initial learning rate. • Robustness, particularly in cases where exact learning rate tuning is infeasible.

Comparing Optimizers

Here's a comparative table illustrating key aspects of various optimizers:

OptimizerKey FeaturesAdvantagesDisadvantages
SGDBasic form of gradient descent Uses fixed learning rateSimplicity Clear convergence propertiesSensitive to learning rate Can get stuck in local minima
AdamCombines AdaGrad and RMSProp Adaptive learning rateFast convergence Less parameter tuningCan become inefficient with sparse data
RMSPropAdjusts learning rate per-parameter Based on moving average of squared gradientsEffective for RNNs Resilient with large datasetsRequires tuning decay parameter
AdaGradAdapts learning rates per-parameter Accumulates past gradientsPerforms well with sparse dataLearning rate may diminish too quickly
AdaDeltaRemoves the need for initial learning rate Adapts learning over window of past gradientsNo initial learning rate Scales wellCan struggle with large datasets

Practical Example

Here is an example of how you can use the Adam optimizer in Keras:


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.