By the end
- Write the five stages of a PyTorch training loop.
- Keep training, validation, and test roles separate.
- Read learning curves for underfitting and overfitting.
- Use early stopping without tuning on the test set.
Week 4 · Hands-on foundations
When does a falling training loss become a useful model?
The 2025 refined regression notebooks add early stopping, learning curves, nonlinear comparisons, and cross-validation. They are valuable but dense for one class. This week isolates the reusable loop—split, train, validate, stop, and compare—so later architectures can reuse the same evidence discipline.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Repeat with three seeds and compare the best validation epoch and loss.
Core ideas
Each iteration calculates predictions and loss, clears old gradients, runs backward, and updates parameters. Evaluation mode omits gradient recording and parameter updates.
A model generalizes when its behavior transfers to unseen examples from the intended use distribution. Training fit is necessary but not sufficient evidence.
Validation performance can identify when additional fitting stops transferring. The test set remains untouched until the model and stopping rule are fixed.
Mathematical intuition
A growing gap can indicate overfitting, but both losses must come from a valid split. A small gap between two bad losses still represents underfitting.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
train_loss = 0.18
validation_loss = 0.31
print(round(validation_loss - train_loss, 2))Predict the result, then click Run.
validation = [0.62, 0.44, 0.36, 0.35, 0.39]
best_epoch = min(range(len(validation)), key=validation.__getitem__) + 1
print(best_epoch)Predict the result, then click Run.
scores = [0.78, 0.81, 0.79, 0.80]
print(round(sum(scores)/len(scores), 3), round(max(scores)-min(scores), 3))Predict the result, then click Run.
Failure checks
What goes wrong: Repeated test checks influence model choices.
Check: Use validation for iteration and reserve the test set.
What goes wrong: Dropout or batch normalization behaves differently during evaluation.
Check: Use `model.train()` and `model.eval()` deliberately.
What goes wrong: A claim depends on one random initialization or split.
Check: Repeat the small experiment with several seeds.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. This week isolates the reusable loop—split, train, validate, stop, and compare—so later architectures can reuse the same evidence discipline.
Standard answer: Each iteration calculates predictions and loss, clears old gradients, runs backward, and updates parameters. Evaluation mode omits gradient recording and parameter updates.
Standard answer: A model generalizes when its behavior transfers to unseen examples from the intended use distribution. Training fit is necessary but not sufficient evidence.
Standard answer: Validation performance can identify when additional fitting stops transferring. The test set remains untouched until the model and stopping rule are fixed.
Standard answer: A growing gap can indicate overfitting, but both losses must come from a valid split. A small gap between two bad losses still represents underfitting. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Freeze a reproducible split and preprocessing rule.
Standard answer: Use validation for iteration and reserve the test set.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Weeks 5 and 6 reuse the same split/train/validate pattern for classification, adding probabilities, thresholds, and class-specific errors.
Terminology
Go further
Next: Weeks 5 and 6 reuse the same split/train/validate pattern for classification, adding probabilities, thresholds, and class-specific errors.