The Adam optimizer
Compute Adam's update from the first and second moment estimates with bias correction. Explain its approximately sign-like, scale-invariant steps, and that it stores 2 extra numbers per parameter.
Builds on: Momentum and per-parameter adaptive steps
What the animation says
Two averages, one ratio
Adam runs two old ideas at once, separately for every parameter. First, a running average of the gradient. That's momentum, and it smooths the direction. Second, a running average of the squared gradient, as in RMSProp. Its square root is the gradient's typical size. Each step is the learning rate times the momentum divided by that typical size. The units cancel. When gradients keep agreeing in sign, the ratio sits near one. Now suppose the gradient flips sign every single step. What happens to the step size?
The zero-start trap
Near zero: the flips cancel in the momentum while the typical size stays large, so Adam barely moves. Now a catch. Both averages start at zero. Use the usual settings: point nine for momentum, point nine nine nine for the squares. After step one, momentum holds only a tenth of the gradient. The squared average holds only a thousandth of the gradient squared. Take its square root, and the typical size reads about three hundredths of the true size. So without a fix, how big is the very first step?
Bias correction, by hand
About three point two times too big. The squared average's square root is shrunk far less than the momentum is. The fix: divide each average by one minus its beta, raised to the step number. Step one, gradient 0.02. Momentum is 0.002. Divided by 0.1, that gives back 0.02. Corrected, the typical size is 0.02 too. The ratio is one, so the step is exactly the learning rate, 0.001. Step two: the gradient halves to 0.01. Drag the marker to where step two lands.
Check it in code
Step two landed at 0.000932. Adam responds to agreement, not size. Here's the same update in Python. These two lines keep the running averages. This line applies bias correction, and this one takes the ratio and scales it by the learning rate. On our two gradients it prints 0.001, then 0.000932. Now multiply every gradient by a thousand: twenty, then ten. What do the steps become?
What a step really is
Identical steps. The momentum and the typical size both grow a thousandfold, and the ratio doesn't change. So every Adam step is the learning rate times a number usually between minus one and one. It moves against the averaged gradient, like any descent. Its size reflects how consistent the gradient has been, whatever the gradient's scale. The price is memory: two extra stored numbers per parameter. A billion-parameter model carries two billion more.
A ratio that ignores gradient scale
The Adam update, with bias correction
Two steps by hand
Check it, then scale the gradients by 1000
import numpy as np
def adam_steps(grads, b1=0.9, b2=0.999, eta=1e-3, eps=1e-8):
m = v = 0.0; out = []
for s, g in enumerate(grads, start=1):
m = b1*m + (1-b1)*g
v = b2*v + (1-b2)*g**2
mh, vh = m/(1-b1**s), v/(1-b2**s)
out.append(eta*mh/(np.sqrt(vh)+eps))
return np.round(out, 6)
print(adam_steps([0.02, 0.01])) # [0.001 0.000932]
print(adam_steps([20.0, 10.0])) # same steps
What to remember
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