The Goal: Minimise the Loss
A neural network starts with random weights. It makes predictions — mostly wrong at first. The training process is simply: measure how wrong the predictions are, then adjust the weights to make them less wrong. Repeat thousands of times.
The "measure of wrongness" is the loss function. It takes the network's prediction and the correct answer, and returns a single number — higher means more wrong, lower means closer to correct. Training is the process of minimising this number.
Loss Functions
Categorical Cross-Entropy (Classification)
Used when the output is a probability distribution over multiple classes (like Softmax outputs). It measures how different the predicted probability distribution is from the true label.
If the correct class is "5" and the model outputs 95% confidence for "5", the loss is very low. If it outputs 5% for "5", the loss is very high (log of a small number is a large negative number — the minus sign makes it positive).
Mean Squared Error (Regression)
Used when predicting a continuous value. Squares the difference between prediction and truth, then averages across all examples. Not used in the SVHN project (classification, not regression), but fundamental to understand.
Gradient Descent
The loss function creates a landscape in weight space — a high-dimensional surface where lower points mean better predictions. Training is navigating this landscape downhill toward a minimum.
Gradient descent computes the direction of steepest ascent at the current position (the gradient), then moves in the opposite direction (downhill) by a small step.
Where η (eta) is the learning rate — how large a step to take. ∂L/∂w is the gradient of the loss with respect to each weight.
The Learning Rate Dilemma
Too low: Training converges, but extremely slowly. You need 10× more epochs to reach the same result.
Typical starting values: 0.001 (Adam) or 0.01 (SGD). Always validate via training curves.
Mini-Batch Gradient Descent
Computing the gradient over the entire dataset before each update (batch gradient descent) is accurate but slow. Computing it on a single example (stochastic gradient descent) is fast but noisy. The standard compromise: mini-batches — compute the gradient on a random subset (typically 32 or 64 examples), update, repeat.
Backpropagation
After the forward pass (computing the prediction and loss), the network needs to know: which weights contributed most to the error, and in which direction should they move to reduce it?
Backpropagation is the algorithm that computes these gradients efficiently using the chain rule of calculus. It starts at the output layer (where the loss is measured), computes each layer's gradient, and "propagates" the signal backwards through the network to the input.
The Full Training Loop
- 1Forward pass. Feed a mini-batch of examples through the network. Each layer computes its output. The final layer produces predictions.
- 2Compute loss. Compare predictions to true labels using the loss function. One number summarises how wrong the batch was.
- 3Backward pass (backpropagation). Compute the gradient of the loss with respect to every weight in the network.
- 4Update weights. Move each weight slightly in the direction that reduces the loss (gradient descent step).
- 5Repeat for every mini-batch. One full pass through all training data = one epoch. Train for dozens to hundreds of epochs.
The Adam Optimiser
Plain gradient descent uses the same learning rate for every weight. Adam (Adaptive Moment Estimation) adapts the learning rate individually for each weight based on the history of its gradients. Weights that have been updated consistently get smaller steps (they're likely near a minimum); weights with erratic gradients get larger steps.
Adam combines two ideas: momentum (remembering recent gradient directions to smooth out noise) and adaptive learning rates (per-weight step sizes). In practice, it converges faster and more reliably than plain SGD for most architectures.
Interactive: Training Curves
Watch loss decrease and accuracy climb over 50 epochs. The gap between training and validation reveals how much the model is overfitting. Toggle between Loss and Accuracy views.
All four models (ANN1, ANN2, CNN1, CNN2) used the Adam optimiser with categorical cross-entropy loss. Training curves showed classic learning behaviour: rapid improvement in early epochs, then gradual convergence. Early stopping (patience=5) halted training when validation loss stopped improving, preventing wasted compute and overfitting. The jump from ANN2 (77%) to CNN1 (87%) demonstrated that architecture change — not just more training — was responsible for the accuracy gain. See Chapter 4 — Training →