Each batch triggers one optimizer step. An epoch is one complete pass through the entire training dataset (all batches once, with shuffling typically enabled).
Epoch count, learning rate schedules, and early stopping are defined in epochs—the time unit trainers use when reading learning curves.
Learning Objectives
By the end of this lesson, students should be able to:
- Define epoch, iteration (step), and their relationship to batch size.
- Implement a multi-epoch training loop in PyTorch.
- Log per-epoch train and validation metrics.
- Apply early stopping when validation loss stops improving.
- Estimate total optimizer steps from epochs × batches per epoch.
Epoch vs Step
One epoch means every training example has been used exactly once (in expectation, if sampling with replacement in large-scale systems; exactly once per shuffle in standard DataLoader loops).
One step (iteration) = one batch processed + one optimizer update.
| Term | Formula / Meaning |
|---|---|
| Steps per epoch | ⌈N / batch_size⌉ |
| Total steps | epochs × steps per epoch |
| Examples seen | epochs × N (with replacement in streaming) |
Multi-Epoch Training Loop
How Many Epochs?
There is no universal answer. Small datasets may need 100+ epochs; ImageNet often uses 90–300 with strong augmentation. Watch validation curves—training loss still falling while validation rises means too many epochs without regularization.
Reality: Extra epochs after validation peaks waste compute and worsen overfitting unless regularized or early-stopped.
Reality: One epoch contains many iterations. Learning rate schedulers may step per epoch or per batch—read the API.
Knowledge Check
- Short Answer: Define one epoch. Answer: One full pass through the training dataset.
- True/False: One epoch always equals one optimizer step. Answer: False—many steps per epoch.
- Multiple Choice: N=8000, B=200 → steps/epoch: (a) 40, (b) 200, (c) 8000, (d) 1. Answer: (a).
- Short Answer: Purpose of early stopping? Answer: Halt training when validation stops improving to limit overfitting.
- True/False: model.eval() should run during validation each epoch. Answer: True.
- Multiple Choice: 10 epochs × 100 steps/epoch = total steps: (a) 10, (b) 100, (c) 1000, (d) 10000. Answer: (c).
- Short Answer: Why shuffle each epoch? Answer: Reduces order bias and changes batch composition.
- True/False: Training loss ↓ and validation loss ↑ suggests overfitting. Answer: True.
- Multiple Choice: Next hyperparameter on step size: (a) Learning Rate, (b) Input Layer, (c) Softmax, (d) Bias. Answer: (a).
- Short Answer: What to save when val loss improves? Answer: Model checkpoint (state_dict).
Key Takeaways
- Epoch = full dataset pass; step = one batch update.
- Log train and validation metrics every epoch.
- Early stopping saves the best checkpoint, not the last.
- Total steps = epochs × ⌈N/B⌉.
- Next: Learning Rate — the step size behind every epoch of progress.
Visualization: Plot train/val loss vs epoch for a small net; students mark the early-stop point.
Discussion: Why do transfer-learning fine-tunes sometimes use only 3–10 epochs?