3.2 — Exponential moving averages and bias correction
Goal: Define the EMA, derive its bias when initialised at zero, and recover the v_t/(1−β^t) correction used by Adam.
1. Why this session exists
Every adaptive optimiser we'll meet — momentum, RMSProp, Adam, AdamW, Lion — runs an exponential moving average (EMA) over some quantity that changes from step to step (the gradient itself, or the squared gradient). The EMA gives a smooth, low-variance running estimate without storing the entire history.
This session does the EMA in isolation: definition, recurrence, what it converges to, and why the standard initialisation creates a bias that must be corrected. The correction from the Adam paper is not a heuristic — it falls out of one line of algebra.
2. Definition
Given a sequence of vectors (the "raw" signal) and a decay rate , define the EMA by the recurrence
For , each new gradient contributes to and the previous average is discounted by . For the average is much smoother — it remembers many more steps.
3. Unrolling: the EMA as a weighted sum
Iterate the recurrence:
So is a weighted sum of past gradients with weights . Most recent: weight . One step back: . Geometric decay.
The total weight is
Two regimes:
- For large (), the total weight tends to : is a proper convex combination of past 's.
- For small (early in training), the total weight is less than , and is biased toward zero.
This second observation is the entire reason for the correction in §5.[1]
4. Effective window size
A useful sanity check: what is the "effective" number of past gradients an EMA with decay averages over? Heuristic argument — define the effective window as the number of recent samples that carry, say, half the total weight.
The weights are for . The cumulative weight after most recent samples is
Set this equal to : . For , . For , . For , .
The convenient rule of thumb is
(Same order as the half-life, off by a small constant.) Adam's defaults and mean: average gradients over the last steps; average squared gradients over the last steps.
5. The bias and its correction
Assume is i.i.d. from some distribution with mean (a "true" gradient we're trying to estimate). Take the expectation of the unrolled :
So underestimates by exactly the factor . The estimator is biased — toward zero, the value was initialised to.
The fix is the obvious one. Define the bias-corrected EMA
Then . is the unbiased moving average; is the raw one.
The correction matters most in the early steps. At with , , so — the first gradient, as it should be. At with , again. After many steps (), and the correction is irrelevant.
6. Why not just initialise ?
You could. Setting would make a valid weighted average from step 1 onward. The reason every implementation initialises to zero anyway:
- You don't know at . Code is cleaner if is set before the first gradient is computed.
- The correction is one line of arithmetic. . Cheap.
- For they coincide. Long training runs barely see the bias; we pay for the correction only when it matters (warmup / short runs).
Zero-init is the convention. Bias correction lives in the optimiser's update.
7. Two EMAs we will use
In the next three sessions, two specific EMAs appear:
First moment (momentum, Adam's ):
This estimates — the mean gradient. Used as a velocity in momentum and Adam.
Second moment (RMSProp, Adam's ):
with understood element-wise. This estimates — the second moment of the gradient (variance + mean). Used as a per-parameter scale to divide the update by .
Both follow the same EMA pattern. The bias correction is the same line of algebra in both cases.
8. Takeaways
- An EMA is a weighted sum of past inputs with geometric weights .
- The total weight after steps is . For small, this is well below — hence the bias toward .
- The bias-corrected EMA is unbiased for when the 's are i.i.d.
- Effective averaging window: .
- Adam's averages gradients over steps; averages squared gradients over steps.
(a) Starting from and the recurrence , prove the closed form by induction.
(b) Show that the bias-corrected EMA is also expressible as a weighted average with and .
(c) With and the constant sequence for all , compute and for . Confirm for all (no bias) while approaches only as grows.
(d) A practitioner uses and trains for only steps. Without bias correction, by what factor is the EMA at the final step smaller than the true mean of the gradients (assuming stationary )?
References
- [1]
Patarasuk, P., & Yuan, X. (2009). "Bandwidth optimal all-reduce algorithms for clusters of workstations". J. Parallel and Distributed Computing. sciencedirect.com — Ring all-reduce algorithm (7.8 §3.2).
↩