We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Production ML — Training Stack step 6 of 12
Exponential Moving Average
An exponential moving average (EMA) keeps a smoothed copy of a model's parameters. Each update blends the previous EMA with the latest parameter values. A larger decay gives the previous EMA more weight, so it changes more slowly.
Your task
Implement update_ema(ema_weights, current_weights, decay).
ema_weightsis the previous EMA vector, with shape(d,).current_weightsis the current parameter vector, also(d,).- Both vectors contain finite floating-point values and have matching dtype and device.
decayis a scalar in[0, 1].
Compute one elementwise update:
new_ema = decay * ema_weights + (1 - decay) * current_weights
Return a vector of shape (d,) with the same dtype, leaving both inputs
unchanged. There is no optimizer step, averaging across vector coordinates,
bias correction or parameter initialization to perform.
For ema_weights=[2, -4], current_weights=[10, 4] and decay=0.75, return
[4, -2]: keep three quarters of the old vector and add one quarter of the
current vector. The endpoint rules are:
- decay=0 returns the current values.
- decay=1 returns the old EMA values.
- decay=0.5 gives equal weight to the two supplied vectors.
What makes it exponential?
When the returned vector is used as the old EMA for the next update, each older contribution is multiplied by decay again. For a fixed decay between 0 and 1, its influence shrinks geometrically with age. This is not an average over a fixed window: old contributions do not disappear at a particular step.
For example, starting from scalar EMA 0 with decay=0.5, a current value of 10 gives EMA 5. A subsequent current value of 6 gives EMA 5.5. The second call uses 5 as its old EMA, not the original 0.
This function only computes that update. Choosing how to initialize the EMA, how often to update it and when to use the smoothed parameters belongs to the surrounding training loop.
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Set the learning rate and control gradient size
A learning-rate schedule changes the step size during training. Check its exact indexing: a warmup may start at zero or at a first positive fraction. Test the initial value, the warmup boundary, the decay endpoint, and any later steps. The schedule's name alone does not specify those choices.
Global norm clipping scales all gradients by the same factor:
$$\min\left(1,\frac{c}{\|g\|}\right),$$
where g represents all parameter gradients together and c is the norm limit.
For scalar gradients 3 and 4, the global norm is 5. Clipping to 2.5
multiplies both by 0.5, giving [1.5, 2]. Clipping each tensor separately can
change their relative direction. Handle zero norm without dividing by zero.
Keep a smoothed copy of the parameters
An exponential moving average (EMA) updates a separate parameter copy:
$$\bar\theta_t=\beta\bar\theta_{t-1}+(1-\beta)\theta_t.$$
For old average 2, new parameter 4, and beta = 0.9, the new average is
0.9*2 + 0.1*4 = 2.2. A larger beta retains more of the past. Initialization and
whether EMA runs before or after the optimizer step affect the result.
Use validation to decide when to stop
Evaluate using the intended inference behavior without changing model parameters. For mean per-example loss, weight each batch's contribution by its example count. Giving unequal batches equal weight changes the overall mean.
Early stopping monitors a validation metric and stops after a specified number of observations without improvement. Define ties, the minimum improvement, and patience precisely. Save an independent copy of the best parameters: the final step may not be the best one. Keep the test set out of the stopping decision so it remains an independent evaluation.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
JAX reference solution
Sign in to track your attempts toward unlocking the reference solution.