1. One update — and the loss increases
We now have a network, a loss function and class probabilities. How do we change the parameters to reduce the loss?
Lesson 5 established that the gradient points towards steepest local ascent. Its negative gives a downhill direction. We might try subtracting the gradient directly, but a direction is only local information: taking a large step can carry us past the valley floor.
To see the arithmetic, isolate one weight and use an illustrative loss:
This is a toy quadratic, not the exact loss of our fruit network. Its minimum is at . Starting at :
Subtract the whole gradient:
Now the loss is:
The loss rose from 24 to 96. The initial direction was correct, but the step was too large.
2. Learning rate: how large should the step be?
For a parameter vector , ordinary gradient descent uses:
The learning rate scales the update. It is not the distance moved on its own: the gradient magnitude also matters.
- Too large a rate can cause overshooting, oscillation or divergence.
- Too small a rate can make progress unnecessarily slow.
- A suitable rate depends on the curvature of the loss and the parameterisation, among other factors.
For our quadratic, the distance from the minimum obeys:
It shrinks when , so a constant rate converges here when:
At , the weight alternates across the minimum with equal distance and constant loss. Above this value it diverges. These thresholds belong to this quadratic, not to neural networks in general.
Change the learning rate during training
A schedule changes the learning rate over time. A decay schedule, such as cosine decay, lowers it during part of training. Warmup is different: it starts with a small rate and increases it towards the intended rate before a later schedule may decay it.
There is no universal numerical range. A value that works for one optimiser and data scale may fail for another.
Train the fruit machine yourself
Simplify the machine to one feature: colour, scored from zero to one. We use eight labelled teaching examples, four apples and four non-apples. The classifier has only two learnable parameters:
Binary Sigmoid can also be written as a two-class Softmax on logits . These are equivalent views of one output calculation, not two activations that must be applied in sequence.
The objective is mean binary cross-entropy:
Using Lesson 10's score derivative , the two gradients are:
Calculate both at the current parameters, then update both together. Start with a few manual steps; compare the learning rates after resetting.
Step: / 100
Mean loss:
Weight:
Bias:
Current weight gradient:
Current bias gradient:
Inspect one true apple: colour 0.92 → score → apple probability .
Its loss is . The total above averages all eight examples.
Inspect the eight training examples
Colour 0.12 · Not apple
Colour 0.24 · Not apple
Colour 0.30 · Not apple
Colour 0.42 · Not apple
Colour 0.72 · Apple
Colour 0.80 · Apple
Colour 0.86 · Apple
Colour 0.92 · Apple
| Step | Weight | Bias | Mean loss |
|---|---|---|---|
| 0 | 0.0000 | 0.0000 | 0.6931 |
At , every example has probability and loss . This is an initial baseline, not the maximum possible cross-entropy: confident wrong predictions can have arbitrarily large loss.
The experiment displays current gradients and appends each update to the record table. It uses all eight examples per update. The loss is computed directly from logits with a stable expression, avoiding logarithms of rounded probabilities.
The learning-rate range belongs to this tiny example. A rate of six is not guaranteed to diverge here just because it is a large number; the quadratic above gives a clearer controlled demonstration of overshooting. Neither decreasing training loss nor separating these eight examples establishes good performance on unseen fruit.
3. Which examples should determine each gradient?
So far we have computed a gradient from the entire training dataset before each update:
For eight examples this is convenient. For a large dataset, processing every example before every update becomes expensive.
As a deliberately simplified serial estimate, a billion examples at one millisecond each would take about 278 hours for one pass, even before additional gradient work. Real systems batch and parallelise computation, so this is an illustration of scale rather than a training-time prediction.
Full-batch training can still be practical for small datasets. The problem is the per-update cost as the dataset grows.
4. Mini-batch SGD: trade some precision for cheaper updates
For a batch of examples, estimate the gradient as:
This is mini-batch stochastic gradient descent. The term SGD is often used for mini-batch updates, although its strict single-example form uses .
A common arrangement is to shuffle the data and split it into batches. Independently sampling batches is another possibility. With appropriate unbiased sampling, the batch gradient estimates the full-data gradient, but individual updates still fluctuate.
Noise can change how optimisation explores a loss surface and may affect generalisation. It is not a guarantee of escaping local minima or finding a better solution. Full-batch gradients are exact for the chosen dataset objective; mini-batch gradients are cheaper estimates of that objective.
An epoch is one pass through the dataset. With batches of size and the final partial batch included, it has approximately:
updates. If that last batch is dropped, the count uses the floor instead. Batch sizes and epoch counts depend on the task, memory, hardware and stopping criterion rather than a universal rule.
5. Narrow valleys, momentum and Adam
In a narrow valley, the loss changes sharply across one direction but gently along another. Plain gradient descent may zigzag across the steep direction while progressing slowly along the valley.
Momentum remembers recent gradients
One convention uses an exponential moving average:
The coefficient controls how much history is retained. Alternating gradient components can partially cancel, while consistent components persist. This can smooth updates, but does not guarantee faster convergence in every setting. Another common momentum convention omits the factor , which changes the effective scale of the update.
Adam tracks first and second moments
Adam combines a moving average of gradients with a moving average of their elementwise squares:
Both averages normally start at zero. Bias correction compensates for their initial bias towards zero:
The update is applied elementwise:
The denominator scales each parameter's update using recent squared gradient magnitudes. It does not simply count how often a parameter was updated. The small positive stabilises division.
Common starting coefficients are and , but the learning rate and other choices still need tuning. Adam is widely used; SGD with momentum remains useful too. AdamW separates weight decay from the adaptive gradient update, and is common in Transformer training. Neither name implies a universally best optimiser.
6. What you have discovered
A gradient supplies local information; an optimiser turns it into a parameter update.
- Learning rate scales the update. An excessive step can increase loss even when the initial direction is downhill.
- Full-batch gradients use every training example; mini-batch gradients reduce per-update work by using a subset.
- Batch noise is a trade-off, not a promise of a better solution.
- Momentum incorporates gradient history.
- Adam combines first and second moment estimates with bias correction and elementwise scaling.
- Schedules change the learning rate over time; warmup and decay have different roles.
Our tiny training machine calculated its two gradients explicitly. Next, we will learn how backpropagation calculates gradients efficiently throughout a much larger network.