PyTorch lifecycle field guide
The compact training, evaluation, checkpoint, and device loop.
A compact lookup for the baseline lab.
Training batch
model.train()once per training phase.- Move model, features, and labels to one device.
optimizer.zero_grad().logits = model(features).loss = loss_fn(logits, labels).loss.backward().optimizer.step().
Evaluation batch
Use model.eval() and with torch.no_grad():. The first changes layer behavior; the second disables gradient recording.
Reproducible checkpoint
Save model and optimizer state dictionaries, epoch, seed, config, ordered labels, dataset checksum, preprocessing/weights identity, and best validation metric. Load with an explicit map_location. Recreate the model and optimizer before restoring state.
Device triage
Start with a CPU one-batch shape test. On GPU, record PyTorch/CUDA/device versions and reduce batch size before changing the model. Reproduction means matching inputs and configuration within a declared tolerance, not promising bitwise identity across hardware.
Your local study notes mount here when JavaScript is available.
Teaching modes mount here when JavaScript is available. The readable course remains available without them.