PythiloLesson 5 of 9 · progress kept in this browser

Practice in Lab: Own EMA weights and copy non-parameter buffers →

Own EMA weights and copy non-parameter buffers

An exponential moving average is another owned model state, not a shallow alias of the current model. Averaging parameter tensors can stabilize evaluation, but buffers need a declared policy too. Here running statistics and counters are copied rather than averaged. This is an explicit exercise contract; other EMA implementations can make different choices. The update disables gradient recording, verifies compatible state before mutation and refuses shared storage so a supposed shadow model cannot silently modify its source.

import copy, torch
m = torch.nn.Linear(1, 1)
shadow = copy.deepcopy(m)
print(shadow is m)
This example uses torch. Run it in the Pythilo app, where these features are available.
Your task

Update an independent average module in place and return it. Floating named parameters use decay*old+(1-decay)*live under no_grad. All other state entries, including running-statistic and integer buffers, copy the live value. Require matching state keys/shapes/dtypes and decay in [0,1]; reject shared tensor storage before changing anything. Live model state must remain unchanged. Update exact tied aliases once; reject inconsistent shadow aliases before mutation.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.