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
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.
Average the direction, normalize the size
The two update rules
Momentum keeps the trend, kills the zigzag
RMSProp equalizes step sizes
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
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.
Bytewise
All courses