Moving Averages (EMA)

Smooth a noisy loss with simple and exponential moving averages, see how β sets the response, and when a zero-started EMA needs 1 − βᵗ bias correction.

Imagine watching a model's loss values arrive: 8, 4, 6, 2. The latest value moves around, making the underlying trend hard to see. A moving average smooths the sequence.

One option is to average the last three values. After 8, 4, 6, the average is 6. When 2 arrives, drop 8 and average 4, 6, 2 to get 4. This is a simple moving average with a window of three.

Blend the new value with the old estimate

An exponential moving average, or EMA, keeps a single running estimate. For this example, initialize it with the first value, 8. Each later estimate is half the old estimate plus half the new observation:

New observationCalculationNew estimate
8Initialize with the first observation8
40.5 × 8 + 0.5 × 46
60.5 × 6 + 0.5 × 66
20.5 × 6 + 0.5 × 24

Predict: If the next observation is 10, what is the new estimate? Does the old estimate disappear immediately?

The general rule uses β for the fraction of the old estimate we retain:

$$v_t=\beta v_{t-1}+(1-\beta)x_t,\qquad 0\leq\beta<1$$

xₜ is the new observation, vₜ₋₁ the previous estimate, and vₜ the updated estimate. The subscript t counts time steps. At β = 0.9, each update keeps 90% of the previous estimate and mixes in 10% of the new value. It reacts more slowly than β = 0.5.

Change the amount of smoothing

This ordinary Python example uses the same four observations:

Before adding 10.0 to the list, predict which estimate will move by a larger amount on that next update. Run it to compare.

The β = 0.5 estimate moves from 4 to 7, a change of 3. The β = 0.9 estimate moves from 6.896 to 7.2064, a change of 0.3104; the code prints that estimate rounded to 7.206.

Strong smoothing can make a training curve easier to read while also hiding a recent change for longer.

Initialization matters

We initialized with the first observation. Another convention starts with v₀ = 0. With a first observation of 8 and β = 0.5, that produces 4, biased downward by the initial zero.

Dividing by 1 − βᵗ corrects the missing total weight when using zero initialization. At t = 1, the correction is 4 / (1 − 0.5) = 8. Do not apply that correction to our first-observation initialization: it follows a different convention.

Paper inspection: Adam, Algorithm 1 tracks moving averages of gradients and of squared gradients. A gradient is a local rate of loss change with respect to a parameter. Find its zero initial values and the two bias-correction lines. The squared-gradient average is a raw second moment, not the variance; Adam combines these estimates to scale parameter updates.

  • A simple moving average uses a fixed recent window with equal weights.
  • An exponential moving average updates v = β × previous v + (1 − β) × new value.
  • Larger β gives more weight to the previous average and a slower response.
  • Initialization matters: starting from the first observation differs from starting at zero.
  • For the usual zero-initialized EMA, divide by 1 − βᵗ for startup bias correction. Do not apply this unchanged to a first-observation initialization.
  • Adam tracks averages of gradients and squared gradients; the latter is a raw second moment, not variance.