Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Optimizers for Neural Networks

1. The Optimization Problem

Neural network training is formulated as empirical risk minimization:

min⁡θL(θ)=1n∑i=1nℓi(θ),where ℓi(θ)=ℓ(fθ(xi),yi)\min_{\theta} L(\theta) = \frac{1}{n} \sum_{i=1}^{n} \ell_i(\theta), \quad \text{where } \ell_i(\theta) = \ell(f_\theta(x_i), y_i)

2. Gradient Descent and Stochastic Gradient Descent

Gradient Descent (GD)

θk+1=θk−αk∇L(θk)\theta_{k+1} = \theta_k - \alpha_k \nabla L(\theta_k)

Each iteration requires computing gradients for all nn training samples.

Stochastic Gradient Descent (SGD)

When nn is large, computing the full gradient is expensive. Instead, sample an index ik∈{1,…,n}i_k \in \{1, \dots, n\} uniformly and use the stochastic gradient ∇ℓik(θk)\nabla \ell_{i_k}(\theta_k):

θk+1=θk−αk∇ℓik(θk)\theta_{k+1} = \theta_k - \alpha_k \nabla \ell_{i_k}(\theta_k)

Since Eik[∇ℓik(θk)]=1n∑i=1n∇ℓi(θk)=∇L(θk)\mathbb{E}_{i_k}[\nabla \ell_{i_k}(\theta_k)] = \frac{1}{n} \sum_{i=1}^{n} \nabla \ell_i(\theta_k) = \nabla L(\theta_k), SGD uses an unbiased estimate of the full gradient.

Mini-batch SGD

In practice, we use mini-batches Bk⊆{1,…,n}B_k \subseteq \{1, \dots, n\} of size bb:

θk+1=θk−αk1b∑i∈Bk∇ℓi(θk)\theta_{k+1} = \theta_k - \alpha_k \frac{1}{b} \sum_{i \in B_k} \nabla \ell_i(\theta_k)

3. SGD with Momentum

Heavy-ball Momentum

mk+1=βmk+gk,θk+1=θk−αkmk+1m_{k+1} = \beta m_k + g_k, \quad \theta_{k+1} = \theta_k - \alpha_k m_{k+1}

where 0≤β<10 \le \beta < 1 is the momentum parameter (typically β=0.9\beta = 0.9).

Exponential Averaging Form

Sometimes written as:

vk+1=βvk+(1−β)gkv_{k+1} = \beta v_k + (1 - \beta) g_k

Since ∑t=0∞(1−β)βt=1\sum_{t=0}^{\infty} (1-\beta)\beta^t = 1, this is an exponentially weighted average of past gradients.

Nesterov Accelerated Gradient (NAG)

A variant that evaluates the gradient at a look-ahead point:

θ~k=θk−αkvk,vk+1=βvk+∇L(θ~k),θk+1=θk−αkvk+1\tilde{\theta}_k = \theta_k - \alpha_k v_k, \quad v_{k+1} = \beta v_k + \nabla L(\tilde{\theta}_k), \quad \theta_{k+1} = \theta_k - \alpha_k v_{k+1}

4. Gradient Normalization and Clipping

Why Normalize or Clip?

In deep networks, gradients can become very large (exploding gradients), causing unstable updates. A single large gradient can produce an excessively large step, slowing training or causing divergence.

Gradient Normalization

g~k=gk∥gk∥2,θk+1=θk−αkg~k\tilde{g}_k = \frac{g_k}{\|g_k\|_2}, \quad \theta_{k+1} = \theta_k - \alpha_k \tilde{g}_k

The update depends only on the direction of the gradient; step length is controlled by the learning rate.

Gradient Clipping

More commonly used than normalization:

g~k={gk,∥gk∥2≤ττgk∥gk∥2,∥gk∥2>τ\tilde{g}_k = \begin{cases} g_k, & \|g_k\|_2 \le \tau \\ \tau \frac{g_k}{\|g_k\|_2}, & \|g_k\|_2 > \tau \end{cases}

If the gradient is small, the update is unchanged. If large, its magnitude is capped at τ\tau (typically τ=1\tau = 1 or 0.5).


5. Adaptive Optimizers

Different parameters may have gradients of very different magnitudes. Adaptive methods use a different effective step size for each coordinate based on historical gradients:

AdaGrad

Accumulates squared gradients:

vk+1=vk+gk⊙gkv_{k+1} = v_k + g_k \odot g_k
θk+1=θk−α gkvk+1+ϵ\theta_{k+1} = \theta_k - \frac{\alpha \, g_k}{\sqrt{v_{k+1}} + \epsilon}

where ⊙\odot denotes element-wise multiplication and operations are element-wise.

RMSProp

Improves AdaGrad by using an exponentially weighted average instead of a cumulative sum:

vk+1=ρvk+(1−ρ)(gk⊙gk)v_{k+1} = \rho v_k + (1 - \rho)(g_k \odot g_k)
θk+1=θk−α gkvk+1+ϵ\theta_{k+1} = \theta_k - \frac{\alpha \, g_k}{\sqrt{v_{k+1}} + \epsilon}

where 0≤ρ<10 \le \rho < 1 (typically ρ=0.9\rho = 0.9) and ϵ≈10−8\epsilon \approx 10^{-8} for numerical stability.

Adam (Adaptive Moment Estimation)

Combines momentum and RMSProp-style scaling:

First moment estimate: mk+1=β1mk+(1−β1)gkm_{k+1} = \beta_1 m_k + (1 - \beta_1) g_k

Second moment estimate: vk+1=β2vk+(1−β2)(gk⊙gk)v_{k+1} = \beta_2 v_k + (1 - \beta_2)(g_k \odot g_k)

Since m0=v0=0m_0 = v_0 = 0, the estimates are biased toward zero in early iterations. Apply bias correction:

m^k+1=mk+11−β1k+1,v^k+1=vk+11−β2k+1\hat{m}_{k+1} = \frac{m_{k+1}}{1 - \beta_1^{k+1}}, \quad \hat{v}_{k+1} = \frac{v_{k+1}}{1 - \beta_2^{k+1}}
θk+1=θk−α m^k+1v^k+1+ϵ\theta_{k+1} = \theta_k - \frac{\alpha \, \hat{m}_{k+1}}{\sqrt{\hat{v}_{k+1}} + \epsilon}

Typical defaults: β1=0.9\beta_1 = 0.9, β2=0.999\beta_2 = 0.999, ϵ=10−8\epsilon = 10^{-8}.

The correction mainly affects the first few iterations and becomes negligible later.


6. Weight Decay and AdamW

L2 Regularization (Weight Decay)

To reduce overfitting, add a regularization term:

min⁡θL(θ)+λ2∥θ∥22\min_{\theta} L(\theta) + \frac{\lambda}{2} \|\theta\|_2^2

The gradient becomes ∇L(θ)+λθ\nabla L(\theta) + \lambda \theta, so the SGD update is:

θk+1=θk−αk(gk+λθk)\theta_{k+1} = \theta_k - \alpha_k (g_k + \lambda \theta_k)

This is often called weight decay as it encourages smaller parameter values and can improve generalization.

AdamW: Decoupled Weight Decay

For SGD, L2 regularization and weight decay are equivalent. However, for Adam they are not, because the adaptive scaling affects the regularization term. AdamW applies weight decay separately from the gradient update:

θk+1=θk−α m^k+1v^k+1+ϵ−αλθk\theta_{k+1} = \theta_k - \frac{\alpha \, \hat{m}_{k+1}}{\sqrt{\hat{v}_{k+1}} + \epsilon} - \alpha \lambda \theta_k

7. Learning Rate Schedules

ScheduleFormula
Constantαk=α0\alpha_k = \alpha_0
Exponential decayαk=α0⋅rk\alpha_k = \alpha_0 \cdot r^k for some r∈(0,1)r \in (0, 1)
Linear decayαk=α0(1−kT)\alpha_k = \alpha_0 (1 - \frac{k}{T}) where TT is total epochs
Cosineαk=αmin⁡+12(αmax⁡−αmin⁡)(1+cos⁡(kTπ))\alpha_k = \alpha_{\min} + \frac{1}{2}(\alpha_{\max} - \alpha_{\min})(1 + \cos(\frac{k}{T}\pi))
Reduce on plateauDecrease α\alpha when validation loss improvement stalls
WarmupStart with small α\alpha, increase over a few epochs to αmax⁡\alpha_{\max}, then apply main schedule

8. Practical Recommendations