Modern Deep Learning Optimizers
While standard Gradient Descent provides the mathematical foundation, training modern deep neural networks requires more sophisticated algorithms that can adapt to the complex, high-dimensional loss landscapes of billion-parameter models.
1. Stochastic Gradient Descent (SGD) with Momentum
Standard SGD often oscillates in ravines where the surface curves much more steeply in one dimension than in another. Momentum helps accelerate SGD in the relevant direction and dampens oscillations.
The Physics Intuition
Think of a ball rolling down a hill. It gains momentum as it rolls, making it harder to change direction abruptly and helping it push through small local bumps (minima).
- (Momentum coefficient): Usually set to 0.9. It determines how much of the previous velocity to keep.
2. RMSProp (Root Mean Square Propagation)
Developed by Geoffrey Hinton, RMSProp addresses the problem of learning rates that are too large or too small for different parameters. It scales the learning rate by a moving average of the squared gradients.
- Effect: If a gradient is consistently large, the learning rate for that parameter is reduced. If the gradient is small, the learning rate is increased.
3. Adam (Adaptive Moment Estimation)
Adam is the "Swiss Army Knife" of optimizers. it combines the benefits of Momentum (first moment) and RMSProp (second moment).
The Update Rule
Adam calculates an exponential moving average of both the gradients () and the squared gradients ():
-
Estimate Moments:
-
Bias Correction: (Crucial for the early steps of training)
-
Update Parameters:
- Standard Hyperparameters: , , .
4. AdamW: The Modern Standard
A common mistake in early implementations was combining L2 regularization (weight decay) directly into the gradient. AdamW decouples the weight decay from the optimization step, which significantly improves generalization.
- Adam/AdamW: Default choice for almost all deep learning tasks (Transformers, CNNs).
- SGD with Momentum: Often preferred in Computer Vision (ResNets) for final "fine-tuning" as it can sometimes find flatter, better-generalizing minima than Adam.
Summary Comparison
| Optimizer | Main Advantage | Key Hyperparameter |
|---|---|---|
| SGD + Momentum | Reduces oscillations | |
| RMSProp | Adapts per-parameter LR | |
| Adam | Fast, robust, widely applicable | |
| AdamW | Better regularization/generalization | Weight Decay |
Python Implementation
import numpy as np
class AdamOptimizer:
def __init__(self, lr=0.001, b1=0.9, b2=0.999, eps=1e-8):
self.lr = lr
self.b1 = b1
self.b2 = b2
self.eps = eps
self.m = None # 1st moment
self.v = None # 2nd moment
self.t = 0
def update(self, w, dw):
if self.m is None:
self.m = np.zeros_like(dw)
self.v = np.zeros_like(dw)
self.t += 1
# Update moments
self.m = self.b1 * self.m + (1 - self.b1) * dw
self.v = self.b2 * self.v + (1 - self.b2) * np.square(dw)
# Bias correction
m_hat = self.m / (1 - self.b1**self.t)
v_hat = self.v / (1 - self.b2**self.t)
# Update weights
return w - self.lr * m_hat / (np.sqrt(v_hat) + self.eps)