Lesson 5: Optimisers, Batches and Hyperparameters
Plain gradient descent works, but it is only the starting point. Real training uses an optimiser: a refined rule for turning gradients into weight updates. The best-known optimiser, Adam, keeps a running average of recent gradients and of their squares, and uses those averages to give each weight its own step size. A weight whose gradient keeps pointing the same way gets confident, steady steps; a weight whose gradient flips back and forth gets smaller, calmer ones. In practice Adam usually trains faster and needs less fiddling than plain gradient descent, which is why it is the default choice in most projects.
Optimisers also deal with a practical problem: computing the gradient over the whole training set before every step would be very slow when the set has millions of examples. Instead, training uses batches — small groups of examples, often 32 or 64 at a time. The gradient is estimated from one batch, a step is taken, and the next batch is used. Each pass through the full dataset is still called an epoch, but it now contains many small steps instead of one large one.
The Noise Is a Feature
A batch gradient is only an estimate, so each step is a little noisy: the true downhill direction and the batch's downhill direction differ slightly. Surprisingly, that noise is often helpful. A perfectly smooth descent can settle into a narrow valley that fits the training data exactly but generalises poorly; the jitter from small batches can bounce the weights out of such valleys and into broader ones that hold up better on new data. That is one reason very small batches sometimes generalise better than very large ones, even though large batches give a cleaner gradient.
Batch size also has a practical side. Larger batches use the computer's parallel hardware better, so each epoch runs faster, but they need more memory. Smaller batches are slower per epoch but often need fewer epochs. There is no single right answer: 32 and 64 are common starting points, and the choice is tuned along with everything else.
Hyperparameters: The Dials You Set
The learning rate, the batch size, the number of layers, the number of neurons per layer and the optimiser itself are called hyperparameters — settings you choose before training, rather than values the network learns. Choosing them well is part art, part experiment. A sensible approach is to change one at a time, train briefly, and compare the loss on data the model has not trained on. Keeping careful notes of what you tried and what happened is not optional paperwork: it is how you avoid repeating failures and how you learn which dials matter for your problem.
A final habit to build now: never judge a model by its training loss alone. A model that has memorised its training data will show a beautifully low training loss and fail on anything new. From the next lesson on, you will keep a separate set of examples aside — the validation set — and let that be the judge of whether your choices are actually helping.
Tuning in Practice: A Sensible Routine
Hyperparameter tuning can feel like guesswork, so here is a routine that works. Start with the defaults that the community has settled on: Adam as the optimiser, a learning rate of 0.001, and a batch size of 32 or 64. Train briefly — a few epochs — and look at the curves. If the loss falls smoothly, you have a working starting point; if it barely moves, raise the learning rate by a factor of ten and try again; if it jumps wildly, lower it by a factor of ten. Only once training is stable should you tune finer details, such as the number of layers or the batch size, and always judge the result on the validation set, never the training loss.
Keep a simple log of every run: the learning rate, batch size, architecture, and the best validation loss, with one line of notes about what you observed. Over a project this log becomes your memory, and it stops you from repeating experiments or forgetting which change helped. It also teaches you something no textbook can: which dials matter for your kind of problem. Two students can run the same course and learn different lessons from their logs, because their data pushed them in different directions — and that is exactly how practical skill is built.
Key Terms
- Optimiser
- The rule that turns gradients into weight updates; Adam is the most common choice.
- Adam
- An optimiser that adapts each weight's step size using running averages of gradients.
- Batch
- A small group of training examples used to estimate the gradient for one step.
- Mini-batch gradient descent
- Gradient descent using one batch at a time instead of the whole dataset.
- Hyperparameter
- A setting chosen before training, such as the learning rate or batch size.
- Epoch
- One full pass through the entire training set, made of many batch steps.
- Validation set
- Examples kept aside from training, used to judge how well the model generalises.
Discussion Questions
- Adam adapts each weight's step size. Why might a weight whose gradient keeps changing direction deserve smaller steps?
- Batch noise can help generalisation. Can you think of another situation where a little randomness improves an outcome?
- If you could only tune one hyperparameter, which would you choose, and why?
Practice Questions
- What is the main difference between plain gradient descent and Adam?
- A training set has 1,000 examples and the batch size is 50. How many steps are there in one epoch?
- What is a hyperparameter? Give two examples.
- Why is a small amount of noise in batch gradients sometimes helpful?
- Why should you judge a model on a validation set rather than on its training loss?
Show answers
- Adam adapts the step size for each weight using running averages of past gradients, while plain gradient descent uses the same learning rate for every weight. Adam usually trains faster with less tuning.
- 20 steps. 1,000 ÷ 50 = 20 batches, and each batch gives one update step.
- A setting chosen before training, not learned by the network — for example the learning rate and the batch size. The number of layers is another.
- It can bounce the weights out of narrow valleys that fit the training data too exactly, into broader valleys that generalise better. A perfectly smooth descent can overfit more easily.
- Because a model can memorise its training data and show a low training loss while failing on new examples. The validation set measures what actually matters: performance on unseen data.