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.
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:
where: • : model parameters • : learning rate • : 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:
- Compute the moving averages:
- Bias correction:
- Parameter update:
Where: • is the gradient at time . • are exponential decay rates for moment estimates. • is the learning rate. • 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:
| Optimizer | Key Features | Advantages | Disadvantages |
| SGD | Basic form of gradient descent Uses fixed learning rate | Simplicity Clear convergence properties | Sensitive to learning rate Can get stuck in local minima |
| Adam | Combines AdaGrad and RMSProp Adaptive learning rate | Fast convergence Less parameter tuning | Can become inefficient with sparse data |
| RMSProp | Adjusts learning rate per-parameter Based on moving average of squared gradients | Effective for RNNs Resilient with large datasets | Requires tuning decay parameter |
| AdaGrad | Adapts learning rates per-parameter Accumulates past gradients | Performs well with sparse data | Learning rate may diminish too quickly |
| AdaDelta | Removes the need for initial learning rate Adapts learning over window of past gradients | No initial learning rate Scales well | Can struggle with large datasets |
Practical Example
Here is an example of how you can use the Adam optimizer in Keras:
Related reading

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 courseTrack 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.