Skip to content
AI360XpertBeta
Core ML

Visual explainer

Momentum

A decaying gradient buffer carries history into the next update. Follow computed steps through a narrow valley, overshoot and instability.

Momentum carries a decaying buffer of past gradients into each parameter update. That history can reinforce a consistent direction, but it can also keep weights moving after the current gradient reaches zero.

Plain gradient steps zigzag across a narrow quadratic valley.
Plain gradient steps zigzag across a narrow quadratic valley.

Our synthetic loss is L(x,y)=12x2+5y2L(x,y)=\tfrac12x^2+5y^2, so the gradient is (x,10y)(x,10y). The contour ellipses show equal losses 7 and 2 using equal spatial units on both axes. Start at (2,1)(2,1) with learning rate η=0.18\eta=0.18 and no momentum. Four gradient evaluations produce (1.64,−0.8)(1.64,-0.8), (1.3448,0.64)(1.3448,0.64), (1.102736,−0.512)(1.102736,-0.512) and (0.90424352,0.4096)(0.90424352,0.4096).

The steep y direction keeps reversing while x contracts by 0.82 per update. These are full gradients of a known quadratic; no sampling noise is needed to create the zigzag. The white dot is the start and the ring is the fourth endpoint.

Weight Older Gradients Less

Older gradients contribute with exponentially smaller weights.
Older gradients contribute with exponentially smaller weights.

Use the gradient-buffer convention, with buffer v0=0v_0=0:

vt+1=βvt+g(wt),wt+1=wt−ηvt+1.v_{t+1}=\beta v_t+g(w_t),\qquad w_{t+1}=w_t-\eta v_{t+1}.

The buffer has gradient units; the learning rate acts after accumulation. This matches PyTorch SGD with momentum, no dampening, no weight decay and no Nesterov lookahead. Other conventions absorb the learning rate or a factor 1−β1-\beta into their state, so compare definitions before copying settings.

At β=0.5\beta=0.5, gradients of ages 0, 1, 2 and 3 have coefficients 1, 0.5, 0.25 and 0.125. The bars show those coefficients, not the actual signed gradients. Older history continues decaying beyond the four displayed ages; there is no hard four-step cutoff.

Combine Memory And New Slope

A retained buffer of 0.9 and a new gradient of 0.4 combine to 1.3.
A retained buffer of 0.9 and a new gradient of 0.4 combine to 1.3.

For an isolated scalar example, change to β=0.9\beta=0.9 and an existing buffer of 1. Retain 0.9×1=0.90.9\times1=0.9, add the new gradient 0.4 and obtain 1.3. The first two arrows join head to tail; dashed projections align the total below. All three use the same length scale.

The buffer is not the displacement. At rate 0.1, the parameter moves by −0.1(1.3)=−0.13-0.1(1.3)=-0.13, rather than by −1.3. This warm-buffer example is separate from the zero-start valley trajectory.

Opposite Pushes Leave A Residual

Decay prevents equal opposite gradients from cancelling exactly in the buffer.
Decay prevents equal opposite gradients from cancelling exactly in the buffer.

Return to β=0.5\beta=0.5. Gradients +0.8 then −0.8, starting from an empty buffer, produce buffers +0.8 then 0.5(0.8)−0.8=−0.40.5(0.8)-0.8=-0.4. The drawing adds the retained +0.4 to the fresh −0.8. Because the older push decays first, their contributions are unequal.

Consistent gradients +0.3 then +0.3 instead produce 0.3 then 0.45. Momentum can reduce the relative effect of alternating signals while accumulating persistent ones; exact sideways cancellation and universal smoothing do not follow from this example.

Compare Equal Gradient Budgets

Four gradient evaluations reach different endpoints with momentum.
Four gradient evaluations reach different endpoints with momentum.

Return to the original two-dimensional quadratic, start (2,1)(2,1), rate 0.18 and zero buffer. Plain descent is the β=0\beta=0 case. With β=0.5\beta=0.5, the four endpoints are (1.64,−0.8)(1.64,-0.8), (1.1648,−0.26)(1.1648,-0.26), (0.717536,0.478)(0.717536,0.478) and (0.36474752,−0.0134)(0.36474752,-0.0134). The two panels share parameter scales; circles identify plain descent and the diamond identifies momentum's fourth endpoint.

After four gradient evaluations, plain loss is about 1.24769 and momentum loss about 0.06742. This is a result of these stated settings, not a general speedup claim. Momentum's loss actually rises from about 1.01638 at update two to 1.39985 at update three. A better eventual endpoint does not imply every intermediate update improves loss.

Compare Memory Decay

A larger decay factor retains more of a past gradient contribution.
A larger decay factor retains more of a past gradient contribution.

Isolate one unit gradient followed by zero gradients. Its remaining contribution after age kk is βk\beta^k. After four updates, β=0.5\beta=0.5 retains 0.0625 while β=0.9\beta=0.9 retains 0.6561. The dashed ring trace and solid diamond trace share a zero-to-one scale.

A larger factor preserves history longer. It also changes accumulated magnitude under persistent gradients: the unnormalized buffer approaches g/(1−β)g/(1-\beta) for a constant gradient gg. Rate and momentum therefore interact. No factor is a universal optimum.

Zero Slope Can Still Move

Stored momentum can move weights away from a zero-gradient minimum.
Stored momentum can move weights away from a zero-gradient minimum.

Switch explicitly to L(w)=12w2L(w)=\tfrac12w^2, rate 0.5, β=0.5\beta=0.5, w0=2w_0=2 and empty buffer. Updates give (w1,v1)=(1,2)(w_1,v_1)=(1,2) and (w2,v2)=(0,2)(w_2,v_2)=(0,2). The weight reaches the minimum, but its buffer is still 2.

Now the gradient is zero, so the next buffer is 1 and the next weight is −0.5. The arrow joins the minimum to a point at loss 0.125. The curve shows loss against weight, not the parameter-space motion of a physical ball. Zero gradient alone does not stop this optimizer.

Preserve The Optimizer State

Resetting the momentum buffer changes the next update from the same weight.
Resetting the momentum buffer changes the next update from the same weight.

At that same weight zero, retaining buffer 2 produces the −0.5 endpoint. Resetting the buffer to zero instead leaves the weight at zero; the concentric circles show no displacement. Both lanes use the same weight ruler.

This explains why restoring only model weights need not resume the same training trajectory. Restore optimizer state when continuity is intended. The illustration is a controlled state comparison, not advice to discard momentum at every apparent minimum.

Scale The Next Displacement

The learning rate scales a displacement computed from the same momentum buffer.
The learning rate scales a displacement computed from the same momentum buffer.

For another scalar state on 12w2\tfrac12w^2, hold w=1w=1, old buffer 2 and β=0.5\beta=0.5. Its gradient is 1 and its new buffer is 2 for either rate. Rates 0.1 and 0.2 produce endpoints 0.8 and 0.6. The longer arrow doubles the parameter displacement while keeping the newly computed buffer fixed.

Future gradients generally differ after those endpoints. The comparison concerns one update, not a claim that entire trajectories scale linearly with the learning rate.

Where It Breaks

An oversized learning rate can produce growing oscillations despite momentum.
An oversized learning rate can produce growing oscillations despite momentum.

Keep the one-dimensional loss 12w2\tfrac12w^2 and β=0.5\beta=0.5, but restart at w0=1w_0=1, buffer zero and rate 3.5. The weights become 1→−2.5→4.5→−7.751\to-2.5\to4.5\to-7.75. The vertical axis is weight, and the horizontal axis is update count. The growing alternating excursions produce losses 0.5→3.125→10.125→30.031250.5\to3.125\to10.125\to30.03125.

This is instability, beyond the temporary overshoot shown earlier. Momentum cannot rescue an arbitrarily oversized step. Choosing rate and decay factor requires evidence for the actual objective; a smooth-looking path or a remembered direction does not guarantee convergence.

The Quick Version

  • Curvature differences can cause plain descent to zigzag.
  • Older gradients enter with exponentially decaying coefficients.
  • Buffer magnitude differs from parameter displacement.
  • Opposite gradients need not cancel under decay.
  • Compare optimizers using the same gradient budget.
  • Larger decay factors preserve contributions longer.
  • Zero gradient can coexist with continuing motion.
  • Buffer resets alter the resumed trajectory.
  • Learning rate scales the next buffered displacement.
  • Oversized rates can cause growing oscillations.