All courses

Momentum and per-parameter adaptive steps

Explain how momentum averages gradients over steps. Explain how dividing by a running RMS of gradients (RMSProp) gives each parameter its own effective step size.

Builds on: Stochastic minibatch gradients

▶ Watch this lesson Start the whole course

What the animation says

One step size, noisy path

Plain SGD steps along each minibatch gradient, with one learning rate for every parameter. Minibatch noise tilts each step a little differently, so the path zigzags down the valley. Worse, some parameters see gradients a hundred times bigger than others. One learning rate is then too big for some and too small for the rest. Can cheap running averages of past gradients fix both problems?

Momentum: steady gradients build up

Momentum keeps a running average of gradients, called m, and steps along that instead. Here each step keeps half of the old m and adds half of the new gradient. Feed it a steady gradient of one. m goes point five, point seven five, point eight seven five, point nine three seven five. It climbs toward one, the full gradient. A steady direction builds up. Now the gradients flip: plus one, minus one, plus one, minus one. What does m do?

Noise cancels, the trend survives

Here it is: point five, minus point two five, point three seven five, minus point three one two five. Opposite gradients cancel, so m stays small and keeps flipping sign. It never settles at zero. So momentum follows the steady direction at full strength and damps the back and forth. In the valley, the zigzag smooths into a path down the middle. A gradient's weight shrinks by a factor of beta for each step it ages. So memory lasts about one over one minus beta steps: two here, ten for beta point nine.

RMSProp: per-parameter scale

Now the second problem. Two parameters, with steady gradients of ten and point one. Plain gradient descent with learning rate point zero one steps point one and point zero zero one. A hundred times apart. RMSProp keeps, for each parameter, a running average of the squared gradient, called s. Its square root is that parameter's typical gradient size: here about ten, and about point one. Then each gradient is divided by its own square root of s. What happens to the two steps?

Same pace, then combine both

Both step about point zero one. This code checks it: these lines build s over five hundred steps. Plain steps print point one and point zero zero one. RMSProp steps print about point zero one for both. Each parameter gets its own step size: the learning rate divided by its running gradient size. Momentum averages direction. RMSProp normalizes size. The standard optimizer for large models combines both. Tap the part of this update that gives each parameter its own step size.

One step size, noisy gradients: can we do better?

Plain SGD steps along each minibatch gradient alone, with one learning rate $\eta$ for every parameter. This causes two problems. 1. The noise in each minibatch gradient makes the path zigzag. 2. Some parameters have gradients 100× larger than others. Any single $\eta$ is then too big for some parameters and too small for others. Can cheap running statistics of past gradients fix both?

Average the direction, normalize the size

**Momentum** steps along a running average of recent gradients, not just the current one. Directions that agree step after step add up. Directions that flip sign cancel, so noise shrinks much as it would if you averaged more minibatches. **RMSProp** keeps a running average of each parameter's *squared* gradient and divides that parameter's gradient by its square root. Big-gradient parameters get scaled down and small ones scaled up, so all of them move at a similar pace.

The two update rules

$g_t$ is the minibatch gradient at step $t$. All operations are elementwise, and $m_0=s_0=0$. $$m_t=\beta_1 m_{t-1}+(1-\beta_1)g_t,\quad \theta\leftarrow\theta-\eta\, m_t$$ $$s_t=\beta_2 s_{t-1}+(1-\beta_2)g_t^2,\quad \theta\leftarrow\theta-\eta\frac{g_t}{\sqrt{s_t}+\epsilon}$$ In either rule, a gradient from $k$ steps ago gets weight $(1-\beta)\beta^k$ ($\beta=\beta_1$ for $m$, $\beta_2$ for $s$). So the memory is about $1/(1-\beta)$ steps. $\sqrt{s_t}$ is that parameter's running RMS gradient. Its effective step size is $\eta/(\sqrt{s_t}+\epsilon)$, where $\epsilon$ is tiny and only prevents division by zero.

Momentum keeps the trend, kills the zigzag

Take $\beta_1=0.5$ and $m_0=0$, so $m_t=0.5\,m_{t-1}+0.5\,g_t$. **Steady** gradients $1,1,1,1$: $m = 0.5,\ 0.75,\ 0.875,\ 0.9375$. This climbs toward 1, the full gradient. **Flipping** gradients $+1,-1,+1,-1$: $m = 0.5,\ -0.25,\ 0.375,\ -0.3125$. This stays small and keeps alternating. The update follows the consistent direction at full strength and damps the back-and-forth.

RMSProp equalizes step sizes

Take two parameters whose gradients are steadily 10 and 0.1. - **Plain gradient descent:** their steps differ by 100×. - **RMSProp:** $\sqrt{s}$ tracks $|g|$, so $g/\sqrt{s}\approx\pm1$. Both parameters step by about $\eta = 0.01$, whatever their gradient's scale.
import numpy as np
eta, b2, eps = 0.01, 0.99, 1e-8
g = np.array([10.0, 0.1])   # steady gradients of two params
s = np.zeros(2)
for t in range(500):
    s = b2 * s + (1 - b2) * g**2
print("plain GD steps:", eta * g)               # [0.1, 0.001]
print("RMSProp steps: ", eta * g / (np.sqrt(s) + eps))  # ~[0.01, 0.01]

Remember this

**Momentum** steps along an exponentially weighted average of the last ~$1/(1-\beta_1)$ gradients. Steady directions build up, while noise and zigzags cancel. **RMSProp** divides each parameter's gradient by its running RMS. Each parameter gets its own step size $\eta/\text{RMS}$, and steps no longer depend on the gradient's raw scale. The standard optimizer for large models combines both ideas.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.