Intermediate level · Module 1: PyTorch in Depth · Lesson 3 of 3
Structuring Training Code
Organise training into reusable functions with configuration, logging and checkpoints.
In this lesson you will
- Separate training, evaluation and configuration code
- Save and resume checkpoints
Key concepts
train and evaluate functionsconfiguration filelogging metricscheckpoint (model + optimiser + epoch)
The lesson
Move code out of single notebooks into functions — train_one_epoch, evaluate — and a script driven by a configuration file of learning rate, batch size and epochs. Experiments become repeatable and comparable.
Log metrics every epoch to a file or a tracking tool so runs can be compared later.
Save checkpoints containing the model weights, the optimiser state and the epoch. If a session disconnects, training resumes where it stopped instead of starting again.
Step by step
- Write train_one_epoch and evaluate functions.
- Move settings into a configuration file.
- Log metrics per epoch.
- Save a checkpoint, stop training and resume it.
Practice activity
Complete the refactoring practical activity.
Expected result: A small, configurable project that resumes correctly from a checkpoint.
Check your understanding
-
What should a checkpoint contain to resume training?
Show answer
Model weights, optimiser state and the epoch number.
-
Why use a configuration file?
Show answer
To make experiments repeatable and easy to compare.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.