
The Ultimate Guide to Understanding Optimizers in Machine Learning
At its core, machine learning is a mathematical optimization problem. A model learns by minimizing a loss function—a numerical measure of how wrong its predictions are. The optimizer is the algorithm that guides this process, adjusting the model’s internal parameters (weights and biases) to reduce the loss. Without optimizers, training would be a blind, random walk.
This guide dissects the mechanics of optimizers, from foundational concepts to advanced state-of-the-art algorithms, covering their mathematical underpinnings, practical trade-offs, and when to deploy each variant.
The Foundations: Gradient Descent and the Learning Rate
All modern optimizers descend from Gradient Descent (GD) . The core idea is simple: compute the gradient (the derivative) of the loss function with respect to every parameter. The gradient points in the direction of steepest ascent. To minimize the loss, you move the parameters in the opposite direction.
The step size, known as the learning rate (α) , is the most critical hyperparameter. A large α can cause divergence (overshooting the minimum). A small α leads to excruciatingly slow convergence, potentially getting stuck in local minima or plateaus. Classical (batch) GD computes the gradient over the entire dataset. This is deterministic and precise, but computationally prohibitive for modern datasets with millions of examples.
Stochastic Gradient Descent (SGD) solved this by computing the gradient on a single random data point (or a small batch). This introduces noise, which helps escape shallow local minima but causes erratic updates. The mini-batch variant, typically using batch sizes of 32, 64, or 128, provides a compromise: computational efficiency with reduced gradient variance. However, vanilla SGD is fragile; it treats all parameters equally, leading to poor performance on ill-conditioned problems.
The Problem of Oscillation and the Momentum Family
A major flaw of SGD is its zig-zagging path through ravines (areas where the surface curves much more steeply in one direction than another). In such landscapes, SGD oscillates across the steep direction while making slow progress along the shallow one.
Momentum was introduced to dampen this. It acts like a ball rolling down a hill: it accumulates past gradients, gaining speed in consistent directions and reducing oscillations. Conceptually, the update at time t is a weighted average of the current gradient and the previous update vector:
v_t = β * v_{t-1} + α * ∇L(θ_t)θ_t = θ_{t-1} - v_t
The coefficient β (usually 0.9) controls the decay of the momentum buffer. Nesterov Accelerated Gradient (NAG) is a smarter variant. It computes the gradient not at the current position, but at the position anticipated by the momentum (θ_t - β * v_{t-1}). This “look-ahead” correction significantly improves convergence speed and stability, particularly near the optimum.
Adaptive Learning Rates: The Game-Changer
The assumption that one learning rate fits all parameters is fundamentally flawed. Parameters associated with infrequent features often need larger updates, while those for frequent features need smaller, finer-grained updates. Adaptive methods solve this by maintaining a per-parameter learning rate.
AdaGrad pioneered this by scaling the learning rate inversely with the square root of the sum of past squared gradients. Parameters with large gradients (steep slopes) get their learning rate reduced quickly; parameters with small gradients get larger updates. AdaGrad works well for sparse data (e.g., NLP problems) but has a critical flaw: the accumulated sum grows monotonically, causing the learning rate to shrink to near zero and effectively stopping training early.
RMSProp addressed AdaGrad’s death-by-learning-rate by using an exponentially decaying moving average of squared gradients instead of a cumulative sum:
E[g²]_t = γ * E[g²]_{t-1} + (1 - γ) * g_t²
Here, γ (typically 0.9) controls the window of the average. RMSProp prevents the learning rate from decaying to zero, making it suitable for non-stationary objectives like those encountered in Reinforcement Learning and online learning.
Adam: The Current Standard
Adam (Adaptive Moment Estimation) synthesizes the strengths of Momentum and RMSProp into a single, robust optimizer. It maintains two moving averages:
- First Moment (m_t) : The mean of past gradients (like Momentum).
- Second Moment (v_t) : The uncentered variance of past gradients (like RMSProp).
The update rule for Adam involves bias correction for both moments to account for their initialization at zero, especially important early in training:
m_t = β1 * m_{t-1} + (1 - β1) * g_tv_t = β2 * v_{t-1} + (1 - β2) * g_t²m_hat = m_t / (1 - β1^t)v_hat = v_t / (1 - β2^t)θ = θ - α * m_hat / (sqrt(v_hat) + ε)
Default values (β1=0.9, β2=0.999, ε=1e-8) work remarkably well across a staggering variety of tasks—from image classification to language modeling. Adam is the de facto default optimizer in deep learning today due to its fast convergence, high tolerance to hyperparameter settings, and low memory overhead (relative to second-order methods).
Adam’s Heirs and Variants
Despite its dominance, Adam is not perfect. It can fail to converge to the optimal generalization performance in some cases (e.g., certain image classification problems).
- AdaMax simplifies Adam by replacing the L2 norm of the second moment with an L-infinity norm, making it more stable in high-dimensional cases.
- Nadam combines Nesterov momentum with Adam, adding the “look-ahead” correction to the momentum component.
- AMSGrad (A Variant of Adam) addresses a theoretical flaw where Adam can forget past gradients by using the maximum of past second moments instead of the moving average. This ensures the learning rate does not increase. While theoretically important, empirical gains are often marginal.
Second-Order Methods and Their Trade-offs
The optimizers discussed above are first-order methods (they use only gradients). Second-order methods, like Newton’s Method, use the Hessian matrix (second derivative) to directly compute the curvature of the loss landscape. This allows them to take more informed, directionally optimal steps, converging in far fewer iterations than first-order methods.
L-BFGS (Limited-memory Broyden–Fletcher–Goldfarb–Shanno) is the most popular practical second-order method. It approximates the inverse Hessian using a limited history of gradients, avoiding the O(n²) memory cost of a full Hessian. L-BFGS is frequently the optimizer of choice for small- to medium-sized datasets where convergence speed and deterministic optimization are paramount (e.g., logistic regression, SVMs).
However, second-order methods struggle in deep learning. Computing or even approximating the Hessian for a 100-million-parameter model is prohibitive. They also operate poorly with mini-batches, as the curvature estimate from a small batch is often noisy and misleading. In practice, Adam or SGD with momentum remains superior for deep neural networks.
Regularization Through Optimization
Optimizers interact deeply with generalization. The choice of optimizer implicitly regularizes the model.
- SGD with Momentum tends to produce flatter minima compared to Adam. Flat minima are thought to generalize better because small perturbations (noise in data) do not drastically change the loss.
- Weight Decay is often conflated with L2 regularization, but it is distinct when using adaptive optimizers like Adam. L2 adds the penalty to the loss; weight decay directly subtracts a fraction of the weights from the update. AdamW (Adam with decoupled weight decay) separates the two, leading to better validation performance and dramatically improved hyperparameter transfer across tasks.
Practical Selection Guide
Choosing an optimizer depends on data volume, model architecture, and computational budget:
- For small datasets (e.g., <10k samples) with shallow models (e.g., logistic regression, small MLPs): L-BFGS often converges in fewer epochs and requires minimal hyperparameter tuning.
- For Computer Vision (CNNs): SGD with Momentum (0.9) remains a strong contender and is often preferred for reaching peak accuracy, despite requiring careful learning rate scheduling. AdamW is a powerful alternative that is faster to tune.
- For Natural Language Processing and Transformers: Adam (or AdamW) is virtually non-negotiable due to the sparse gradients and highly complex loss landscapes of attention-based models. Use β1=0.9, β2=0.999, and a weight decay of 0.01.
- For Reinforcement Learning: RMSProp (or Adam) is standard. The non-stationary nature of the RL objective (the data distribution changes as the policy evolves) makes AdaGrad unsuitable.
- For Transformers and Large Language Models: AdamW with a linear warmup and cosine decay learning rate schedule is the current industry standard. This combination prevents early training divergence and ensures stable convergence.
Hyperparameter Tuning Best Practices
The learning rate is always the most critical dial. Use a learning rate range test (Leslie Smith’s method): start at a very small α, increase it linearly each mini-batch, and plot the loss. The ideal learning rate is where the loss decreases most steeply. For Adam, a starting point of 0.001 (1e-3) is robust; for SGD with momentum, 0.01 to 0.1 is typical.
The batch size is often tied to optimizer performance. Larger batch sizes (512+) require careful tuning of the learning rate. The linear scaling rule suggests doubling the learning rate when doubling the batch size. Warmup (starting with a very small α for a few epochs) is necessary to prevent early model collapse when using large batches.
The Future: Gradient Compression and Few-Shot Optimizers
As models scale to trillions of parameters, communication becomes the bottleneck. Local SGD (allowing workers to perform multiple gradient steps before averaging) and gradient compression (e.g., quantization, top-k sparsification) are emerging as critical extensions to standard optimizers. Meta-learning optimizers (e.g., Learned Optimizers like VeLO) aim to replace hand-designed rules with neural networks that learn the optimization process itself, promising fewer hyperparameters to tune, though they are not yet universally superior to AdamW.