---
title: Training networks
heading: Gradient descent on a quadratic bowl, the largest stable learning rate and the best one, how the condition number sets the convergence rate, momentum, stochastic minibatch gradients and their noise, and Adam with its bias correction
eyebrow: S14 · Lesson 26 · Training networks
xp: 50
---

::: hook
Backpropagation gives the gradient; something still has to walk downhill. Plain gradient descent is simple and, near a minimum, its behavior can be predicted exactly: the error surface looks like a quadratic bowl, each direction converges at its own rate, and one badly shaped direction can slow everything or make the whole run explode. Momentum and adaptive methods like Adam exist to fix that, and minibatches make each step cheap at the cost of noise. These are the knobs every training run turns.
:::

::: play
- Gradient descent on $E = \frac{1}{2}(\lambda_1w_1^2 + \lambda_2w_2^2)$ with $\lambda_1$ = 1 and an adjustable $\lambda_2$. Each weight evolves as $w_k = (1 - \eta\lambda)^kw_0$.
- Raise η and the steep direction ($\lambda_2$) starts to oscillate, then diverges once $\eta > 2/\lambda_2$. Lower it and the shallow direction crawls.
- The best compromise is $\eta = 2/(\lambda_1 + \lambda_2)$, and even then the slow rate is $(\kappa - 1)/(\kappa + 1)$ for condition number $\kappa = \lambda_2/\lambda_1$.
:::

```sim plot
view: graph
x: n = 0..40
y: w = -1.5..1.5
slider: e = 0.005..0.25 step 0.005 = 0.05
slider: L = 2..100 step 1 = 20
plot: (1 - e)^n | stems | label: shallow direction (λ = 1)
plot: (1 - e*L)^n | stems | color: ch3 | label: steep direction (λ = λ₂)
readout: stability limit, 2/λ₂ = 2/L | digits: 4
readout: best η = 2/(1 + L) | digits: 4
readout: slowest factor per step = (abs(1 - e) + abs(1 - e*L) + abs(abs(1 - e) - abs(1 - e*L)))/2 | digits: 4
```

::: predict
## A quadratic bowl has curvatures 1 and 100. With the best fixed learning rate, roughly how many gradient-descent steps reduce the error in every direction by a factor of a million?

- [ ] About 14
- [ ] About 70
- [x] About 690
- [ ] About 100,000

::: reveal
The best rate, η = 2/101, gives both directions the factor (κ − 1)/(κ + 1) = 99/101 = 0.980 per step. Reducing by $10^{-6}$ takes ln(10⁻⁶)/ln(0.980) = 691 steps. Momentum improves the factor to (√κ − 1)/(√κ + 1) = 9/11, about 69 steps: ten times fewer.
:::
:::

::: nameit
## Gradient descent near a minimum

Near a minimum, $E \approx E_0 + \frac{1}{2}\Delta\mathbf{w}^TH\Delta\mathbf{w}$. In the Hessian's eigenvector directions, the update $\mathbf{w} \leftarrow \mathbf{w} - \eta\nabla E$ multiplies each component by $1 - \eta\lambda_i$ every step.

- **Stability** needs $|1 - \eta\lambda_i| < 1$ for all i: $\eta < 2/\lambda_\max$.
- The best fixed rate is $\eta = 2/(\lambda_\min + \lambda_\max)$, giving a factor $(\kappa - 1)/(\kappa + 1)$ per step with $\kappa = \lambda_\max/\lambda_\min$.

Badly conditioned problems (large κ) converge slowly, which is why inputs are normalized and why tricks like batch normalization help.

## Momentum

The heavy-ball update keeps a velocity: $\mathbf{v} \leftarrow \mu\mathbf{v} - \eta\nabla E$, $\mathbf{w} \leftarrow \mathbf{w} + \mathbf{v}$. Tuned on a quadratic, its factor improves to $(\sqrt{\kappa} - 1)/(\sqrt{\kappa} + 1)$.

## Stochastic gradients

**Stochastic gradient descent** estimates the gradient from a minibatch of B examples. If single-example gradients have standard deviation σ, the minibatch average has σ/√B: cheaper steps, more noise. A decaying learning rate tames the noise near the end.

## Adam

Adam keeps exponential averages of the gradient, $m \leftarrow \beta_1m + (1 - \beta_1)g$, and of its square, $v \leftarrow \beta_2v + (1 - \beta_2)g^2$, and steps by $\eta\hat{m}/(\sqrt{\hat{v}} + \epsilon)$. Both averages start at 0, so they're divided by $1 - \beta^t$ after t steps (**bias correction**). The step size is then about η in every direction, whatever the gradient's scale: a per-weight normalization that eases bad conditioning.
:::

::: example
## For curvatures 1 and 100, find the stability limit and the best rate, and what happens at η = 0.015.

::: solution
1. **Stability limit.**
   $$\begin{aligned} \eta &< 2/100 \\ &= 0.02 \end{aligned}$$
2. **Best rate.**
   $$\begin{aligned} \eta &= 2/101 \\ &= 0.0198 \end{aligned}$$
3. **Factors at η = 0.015.** The steep direction's factor (fast, oscillating decay), then the shallow one's:
   $$\begin{aligned} 1 - 1.5 &= -0.5 \\ 1 - 0.015 &= 0.985 \end{aligned}$$
4. **Steps needed.** The shallow direction dominates: 915 steps for $10^{-6}$.

**Answer:** the stability limit is η = 0.02 and the best rate 0.0198; at η = 0.015 the shallow direction sets the pace, taking 915 steps to reach $10^{-6}$.
:::
:::

::: example
## With Adam's β₁ = 0.9 and β₂ = 0.999, what are m and v after one step with gradient g, and what step results?

::: solution
1. **Moments after one step.**
   $$m = 0.1g, \quad v = 0.001g^2$$
2. **Step without correction.**
   $$\begin{aligned} &\eta \times \frac{0.1g}{0.0316|g|} \\ &= 3.16\eta\,\text{sign}(g) \end{aligned}$$
3. **Bias correction.** It divides m by 0.1 and v by 0.001, giving m̂ = g and v̂ = g², and a step of exactly
   $$\eta\,\text{sign}(g)$$
   (for small ε).

**Answer:** m = 0.1g and v = 0.001g², and the corrected step is exactly η sign(g), against 3.16η sign(g) without correction.
:::
:::

::: example
## Gradient descent on a quadratic starts from zero with η = 0.01 and stops after 1000 steps. What fraction of the solution has it learned along directions of curvature 1 and 0.01? Compare with ridge regression at λ = 1/(kη) = 0.1.

::: solution
1. **Gradient descent.** Along an eigendirection of curvature c, each step closes a fraction ηc of the remaining gap, so after k steps the fraction learned is
   $$1 - (1 - \eta c)^k$$
2. **The two directions.** For c = 1 and c = 0.01:
   $$\begin{aligned} 1 - 0.99^{1000} &= 0.99996 \\ 1 - 0.9999^{1000} &= 0.095 \end{aligned}$$
3. **Ridge.** Ridge keeps $c/(c + \lambda)$:
   $$\begin{aligned} 1/1.1 &= 0.909 \\ 0.01/0.11 &= 0.091 \end{aligned}$$
4. **Compare.** Both learn the well-determined direction and suppress the poorly determined one by about the same amount, so stopping early acts as a regularizer with $\lambda \approx 1/(k\eta)$, and the number of steps is a hyperparameter to choose on validation data.

**Answer:** gradient descent has learned 0.99996 of the solution along curvature 1 and 0.095 along curvature 0.01, against ridge's 0.909 and 0.091.
:::
:::

::: check
given: a = 4 | 50 | 1000
answer: 2/a
tolerance: 0.5%

## The Hessian's largest eigenvalue is {a}. What's the largest learning rate for which gradient descent is stable?

::: right
Right. $2/\lambda_\max$ = {answer}.
:::

::: misses
- [1/a] That's the rate that removes the steep direction in one step; stability allows up to twice that.
- [any] Not yet. Need $|1 - \eta\lambda| < 1$.
:::
:::

::: check
given: a = 2 | 5; b = 20 | 200
answer: 2/(a + b)
tolerance: 0.5%

## The Hessian's eigenvalues run from {a} to {b}. What fixed learning rate converges fastest?

::: right
Right. $2/(\lambda_\min + \lambda_\max)$ = {answer}.
:::

::: misses
- [2/b] That's the edge of stability; the best rate balances both ends.
- [any] Not yet. Make the extreme factors equal and opposite.
:::
:::

::: check
given: k = 10 | 100 | 1000; e = 1e-3 | 1e-6
answer: ceil(ln(e)/ln((k - 1)/(k + 1)))
tolerance: 0.5%

## A problem has condition number {k}. With the best fixed learning rate, how many gradient-descent steps reduce the error by a factor of {e}?

::: right
Right. $\lceil\ln\epsilon/\ln((\kappa - 1)/(\kappa + 1))\rceil$ = {answer}.
:::

::: misses
- [any] Not yet. Each step multiplies by $(\kappa - 1)/(\kappa + 1)$.
:::
:::

::: check
given: k = 10 | 100 | 1e4
answer: (sqrt(k) - 1)/(sqrt(k) + 1)
tolerance: 0.5%

## With well-tuned momentum, what factor per step does gradient descent achieve on a quadratic with condition number {k}?

::: right
Right. $(\sqrt{\kappa} - 1)/(\sqrt{\kappa} + 1)$ = {answer}.
:::

::: misses
- [(k - 1)/(k + 1)] That's plain gradient descent; momentum takes the square root of κ.
- [any] Not yet. Heavy-ball rate.
:::
:::

::: check
given: g = 0.2 | 3; b = 0.9 | 0.99; t = 1 | 5 | 20
answer: g*(1 - b^t)
tolerance: 0.5%

## Adam's first-moment average starts at 0 with β₁ = {b}. After {t} steps with constant gradient {g}, what's the uncorrected m?

::: right
Right. $g(1 - \beta_1^t)$ = {answer}, which bias correction divides back out.
:::

::: misses
- [g] That's the corrected value.
- [any] Not yet. Sum the geometric series.
:::
:::

::: check
given: s = 0.5 | 2 | 8; B = 16 | 64 | 512
answer: s/sqrt(B)
tolerance: 0.5%

## Single-example gradients have standard deviation {s}. What's the standard deviation of a minibatch gradient over {B} examples?

::: right
Right. $\sigma/\sqrt{B}$ = {answer}.
:::

::: misses
- [s/B] Averaging B independent values divides the variance by B, the standard deviation by √B.
- [any] Not yet. Standard error of a mean.
:::
:::

::: check
kind: parts
given: c = 0.01 | 0.1 | 0.5; e = 0.01 | 0.05; k = 300 | 1000

## Gradient descent with rate {e} runs {k} steps from zero on a quadratic.

::: part
answer: 1 - (1 - e*c)^k
tolerance: 0.5%

### What fraction of the solution has it learned along a direction of curvature {c}?

::: misses
- [e*c*k] That's the first-order approximation; the gap shrinks geometrically.
- [any] $1 - (1 - \eta c)^k$.
:::
:::

::: part
answer: c/(c + 1/(k*e))
tolerance: 0.5%

### What fraction would ridge regression with λ = 1/(kη) keep along that direction?

::: misses
- [any] $c/(c + \lambda)$.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.001..1, b = 2..1000

## A quadratic's Hessian eigenvalues run from a to b; gradient descent uses heavy-ball momentum.

::: step
answer: 4/(sqrt(a) + sqrt(b))^2

### What learning rate is optimal?
:::

::: step
answer: ((sqrt(b) - sqrt(a))/(sqrt(b) + sqrt(a)))^2

### What momentum coefficient goes with it?
:::

::: right
Right. The error then shrinks by $(\sqrt\kappa - 1)/(\sqrt\kappa + 1)$ per step, against $(\kappa - 1)/(\kappa + 1)$ without momentum.
:::
:::

::: explain
## Why does a badly conditioned error surface slow gradient descent, and how do momentum and Adam help?

::: model
Gradient descent uses one learning rate for every direction, and that rate must stay below 2/λ for the steepest direction or the run diverges. Directions with small curvature then move by only a small fraction of their distance each step, so convergence is set by the ratio of the largest to the smallest curvature. Momentum accumulates velocity along directions where the gradient keeps pointing the same way, so shallow directions speed up while oscillations in steep ones partly cancel, improving the rate from depending on κ to depending on √κ. Adam rescales each weight's step by its own gradient magnitude, which acts like a per-weight learning rate and compensates for curvatures that differ along the coordinate axes.
:::
:::

::: practice

::: check
given: a = 0.5 | 5; b = 10 | 5000
answer: 2/(a + b)
tolerance: 0.5%

## Best rate: eigenvalues {a} to {b}?

::: misses
- [any] $2/(\lambda_\min + \lambda_\max)$.
:::
:::

::: check
given: k = 3 | 10 | 100 | 1000 | 1e4 | 1e6
answer: (sqrt(k) - 1)/(sqrt(k) + 1)
tolerance: 0.5%

## Momentum rate, κ = {k}?

::: misses
- [any] $(\sqrt{\kappa} - 1)/(\sqrt{\kappa} + 1)$.
:::
:::

::: check
given: s = 0.1 | 20; B = 1 | 4096
answer: s/sqrt(B)
tolerance: 0.5%

## Minibatch std: σ = {s}, B = {B}?

::: misses
- [any] $\sigma/\sqrt{B}$.
:::
:::
:::
