PythiloLesson 5 of 8 · progress kept in this browser

Capture a checkpoint that cannot drift with live weights

A state dictionary contains tensor references, not a frozen historical snapshot. Continuing training or mutating a parameter can therefore change an alleged best checkpoint in memory. Copy tensor state when capturing the checkpoint, and keep training progress metadata tied to that capture.

The exercise records actual model and optimizer state with a deep copy. It tests aliasing by modifying the live model after capture. Persisting and resuming a whole experiment additionally needs RNG, sampler, preprocessing and data-version state. A model-only snapshot is enough for inference recovery but not exact training continuation.

import copy,torch
from torch import nn
model=nn.Linear(1,1)
snapshot=copy.deepcopy(model.state_dict())
with torch.no_grad():model.weight.fill_(9)
print(torch.equal(snapshot["weight"],model.weight))
This example uses torch. Run it in the Pythilo app, where these features are available.
Your task

Implement capture_training(model,optimizer,step) returning a deep-copied dict model=model.state_dict(), optimizer=optimizer.state_dict(), step=step. Later live tensor or optimizer-state mutation must not change the snapshot.

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.