Part 10 / Topic 7 of 7
Saving and loading
Save the state_dict (the weights), not the model object:
torch.save(model.state_dict(), "model.pt")
clone.load_state_dict(torch.load("model.pt", weights_only=True))
# verified: identical outputs after reload
weights_only=True is the safe way to load: checkpoint files are pickle-based, and unrestricted unpickling of an untrusted file can execute code. For resumable training, save the optimizer's state_dict and the epoch number alongside — the stage project does.
Done here? The practice set for Part 10 — 4 exercises with solutions — is on the part page.