Basis Lab

Answers · The mathematics underneath

Why does Adam need bias correction at the start of training?

In shortAdam needs bias correction because its two running averages start at zero, and without correction the early steps are several times larger than the learning rate asks for.

Adam needs bias correction because both running averages start at zero, so at early steps they are too small. Correcting each by its exact shortfall makes the first update equal to the learning rate; without correction the update is not cautious but several times too large.

The two averages are not symmetric. The gradient average with β1=0.9\beta_1=0.9 looks back about ten steps. The squared-gradient average with β2=0.999\beta_2=0.999 looks back about a thousand. At step one the first is at a tenth of its final size and the second is at a thousandth. The numerator therefore warms up quickly and the denominator warms up slowly, and that mismatch is why the uncorrected ratio overshoots rather than undershoots.

The exact shortfall

Expand the recursion for the gradient average. After tt steps the weights on the observed gradients are (1−β1)β1t−i(1-\beta_1)\beta_1^{t-i}, and they sum to 1−β1t1-\beta_1^t rather than to one. The missing weight was spent on the zero with which the average started. The same calculation applies to the squared-gradient average with β2\beta_2. That is why the correction divides by 1−βt1-\beta^t rather than adding a constant.

mt=(1−β1)∑i=1tβ1 t−igi,E[mt]=(1−β1 t) E[g]vt=(1−β2)∑i=1tβ2 t−igi2,E[vt]=(1−β2 t) E[g2]m_t=(1-\beta_1)\sum_{i=1}^{t}\beta_1^{\,t-i}g_i,\qquad \mathbb{E}[m_t]=\big(1-\beta_1^{\,t}\big)\,\mathbb{E}[g]\\[2pt] v_t=(1-\beta_2)\sum_{i=1}^{t}\beta_2^{\,t-i}g_i^2,\qquad \mathbb{E}[v_t]=\big(1-\beta_2^{\,t}\big)\,\mathbb{E}[g^2]
m^t=mt1−β1 t,v^t=vt1−β2 t\hat m_t=\frac{m_t}{1-\beta_1^{\,t}},\qquad \hat v_t=\frac{v_t}{1-\beta_2^{\,t}}

At t=1t=1 with β1=0.9\beta_1=0.9 and β2=0.999\beta_2=0.999, take a gradient gg that is the same at every step for the calculation. Then m1=0.1gm_1=0.1g and v1=0.001g2v_1=0.001g^2. The square root is v1=0.001 g≈0.0316g\sqrt{v_1}=\sqrt{0.001}\,g\approx0.0316g, so the uncorrected ratio is 0.1/0.0316≈3.160.1/0.0316\approx3.16. The corrected quantities are m^1=0.1g/0.1=g\hat m_1=0.1g/0.1=g and v^1=0.001g2/0.001=g2\hat v_1=0.001g^2/0.001=g^2, so their ratio is one. The corrected first step is α g/(∣g∣+ϵ)\alpha\,g/(|g|+\epsilon), approximately α\alpha.

FIG 1Two averages warming up at different speeds
0.000.300.600.901.201.0500.81000.51500.32000.0step number
the gradient average, beta 0.9the squared-gradient average, beta 0.999
The gradient average has essentially finished warming up by step ten. The squared-gradient average needs hundreds of steps. Correcting neither, or correcting only one, leaves their ratio wrong throughout that stretch.

Why both being small does not cancel

The natural guess is that if mtm_t and vtv_t are both short, the ratio mt/vtm_t/\sqrt{v_t} is roughly right. It is not, because the two averages are short by different factors. The uncorrected update is inflated relative to the corrected one by (1−β1t)/1−β2t(1-\beta_1^t)/\sqrt{1-\beta_2^t}. At t=1t=1 that factor is 0.1/0.001≈3.160.1/\sqrt{0.001}\approx3.16, not one. At t=2t=2 the uncorrected ratio is 0.19/0.001999≈4.250.19/\sqrt{0.001999}\approx4.25.

The gap gets worse before it gets better. At t=10t=10 the uncorrected ratio is 0.6513/0.009955≈6.530.6513/\sqrt{0.009955}\approx6.53. By t=1000t=1000 the squared-gradient average has warmed up and the uncorrected ratio is 1.000/0.6323≈1.261.000/\sqrt{0.6323}\approx1.26. A learning rate tuned for corrected Adam can therefore be exceeded by a large factor in the first few dozen steps. A ramped learning rate does a similar job, but bias correction does it exactly for a zero initialisation.

Correcting only one average is worse than correcting neither in one direction and not enough in the other. If only mtm_t is corrected at t=1t=1, the update is g/0.001g2≈31.6g/\sqrt{0.001g^2}\approx31.6, still far too large. If only vtv_t is corrected, the update is 0.1g/∣g∣=0.10.1g/|g|=0.1, too small. Both factors are needed because the ratio contains both.

The correction is exact for the zero start. If the gradients were stationary, m^t\hat m_t would be an unbiased estimate of E[g]\mathbb{E}[g] and v^t\hat v_t an unbiased estimate of E[g2]\mathbb{E}[g^2]. If the distribution changes, both corrected and uncorrected quantities estimate different things, but the initialisation bias is still removed. The ϵ\epsilon of about 10−810^{-8} only stops a coordinate with no history from dividing by zero; it is not what fixes the bias.

What it does not fix

Bias correction repairs the start, not the diagonal approximation. If a valley runs at forty-five degrees to both coordinates, both coordinates have gradients of the same typical size, so the adaptive denominators are equal and the method behaves like plain momentum. Only a full matrix preconditioner would notice that curvature. Weight decay placed inside the loss has a related problem: its gradient is divided by v^t\sqrt{\hat v_t}, so the decay applied to a parameter is scaled by that parameter's gradient history. Applying the decay directly to the parameters is the difference between AdamW and Adam.

Worth keeping

  • At step one with the usual settings, uncorrected Adam takes a step about 3.16 times the learning rate; by step ten the factor is about 6.53.
  • Bias correction divides each average by one minus beta to the power t, which is the exact factor by which a zero-started geometric average is short.
  • It fixes the initialisation, not the diagonal limitation: a valley at forty-five degrees still looks like plain momentum.

Where this goes next

This question is one step of The Mathematics of Gradient Descent, a course that works through its subject from the beginning. The page that teaches this part of it is A Step Size Per Coordinate, and it reads the whole way through whether or not you have an account.

What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.

Read that pageThe whole course

Other questions people ask about this

  1. Why is the negative gradient the direction of steepest descent?A unit step goes downhill fastest when it points directly opposite the gradient, because the dot product with the gradient is then as negative as it can be.
  2. How does momentum stop the steps from cancelling each other out?Momentum keeps a running total of past gradients, so directions that keep agreeing build up while directions that keep flipping sign cancel out.
  3. Why does computing the gradient cost about the same no matter how many parameters the network has?Because one backward sweep shares intermediate derivatives across all parameters, so the whole gradient costs a few forward passes in total rather than one per parameter.
  4. Why does gradient descent still work when each step uses only one minibatch's gradient?A minibatch gradient is wrong at every step but correct on average, so the errors cancel over many steps while the true downhill pull keeps adding up.
  5. What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
  6. How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.

Read alongside