The patterns in this file show up in nearly every PyTorch project worth its salt.
- Device handling —
model.to(device)andxb.to(device)keep CPU/GPU code uniform. run_epochhelper — one function for both train and eval, controlled by atrain: boolflag andtorch.set_grad_enabled.- Metrics — track loss and accuracy per epoch on both splits.
- Early stopping — if val loss hasn't improved in
patienceepochs, stop. Saves time and prevents overfitting. - Checkpointing —
torch.save({"model": model.state_dict(), ...})writes weights + metadata.load_state_dictrestores them. TrainConfigdataclass — keep hyperparameters in one place; easy to log or sweep.
Save model.state_dict(), not model itself. The state dict is a plain {name: tensor} mapping — portable, version-agnostic, and doesn't depend on the exact class definition being importable. Pickled nn.Module objects break on refactors.
torch.load(..., weights_only=True) is the safe loader. The default unpickler can execute arbitrary code from a malicious checkpoint. Use weights_only=True for anything you don't trust 100%.
python 08_training_loop/main.py