Training Loop and Optimisers

P6.dl-architectures.03 · Audience: guest, language-pro, it-ml · Prerequisites: Backpropagation Intuition

A trained network is the product of millions of tiny weight updates. This module walks through the machinery that produces them: epochs and mini-batches organise the data, SGD turns each gradient into a weight update, Adam adapts the step size per parameter, and the loss curve shows whether the whole process is working — including the tell-tale signature of overfitting.

Step 1 / 4Epochs and Batches

The training loop in plain language. Training a neural network means repeatedly:

  1. Show the model some examples (a batch of data)
  2. Compute the loss (how wrong is the prediction?)
  3. Backpropagate (compute gradients — who is to blame?)
  4. Update weights (nudge them in the direction that reduces loss)
  5. Repeat until the loss is acceptably small

One complete pass through all the training data is called an epoch. Within each epoch, data is processed in small groups called batches — usually 32, 64, or 256 examples at a time.

🗣️ From a linguist's perspective: Epochs and batches as multiple passes through a training corpus
Corpus linguists build frequency lists by scanning the entire corpus once. Machine learning goes further: the model scans the training data many times (epochs), gradually refining its parameters with each pass. Within each epoch, the data is processed in small groups (batches) rather than all at once — analogous to a language learner who studies one chapter at a time and reviews multiple times rather than trying to memorise the entire textbook in one sitting. The key insight: repeated exposure to the same examples, each time adjusting weights slightly, is how the model learns stable, generalisable patterns.
ⓘ Concept: Epochs and Mini-Batches

Epoch — one full pass through the training dataset.

Mini-batch — a random subset of training examples processed together.

Why mini-batches instead of one example at a time?

  • Matrix operations on batches are faster on GPUs (parallelism)
  • Noisy gradient estimates from small batches can help escape local minima
  • Too large a batch (full dataset) is slow and can converge to sharp minima

Typical schedule: 100–300 epochs × 1,000–50,000 batches/epoch = millions of weight updates.

Why it matters — BERT-base was trained for 1 million steps on batches of 256 sequences. GPT-3 was trained on 300 billion tokens. The training loop ran continuously for weeks on thousands of GPUs.

ConceptTypical sizeOne iteration covers
Dataset10,000 – 1B examplesAll examples (once per epoch)
Mini-batch32 – 256 examplesOne batch
Step1 gradient updateOne batch forward+backward+update
EpochAll batchesFull dataset once

How epochs, batches, and steps nest:

LevelContainsTriggers
Training runall epochsstarts learning
Epochall mini-batches (= dataset size ÷ batch size)one full data pass
Mini-batchB examples (e.g. B = 32)1 forward pass + 1 backward pass + 1 weight update
Example1 input → label paircontributes to the batch gradient

Example: 10,000 training sentences, batch size 32 → 313 steps per epoch. At 100 epochs → 31,300 weight updates in total.

Check your understanding

Q1 — If the dataset has 100 samples, batch size = 10, and we train for 3 epochs, how many gradient update steps are there in total?

Q2 — Given w = 1.0, ∂L/∂w = 0.4, η = 0.1, what is w after one SGD step? (round to 2 d.p. — recall w_new = w − η × ∂L/∂w)

Q3 — In Adam, what quantity does the second moment (v) track that vanilla SGD ignores?

Q4 — If the learning rate is too large, what risk does the optimiser face?

Module review — what is the main advantage of Adam over SGD with a fixed learning rate?

📚 Go Further

Curated resources to explore training loops and optimisers further.

TypeResource
Video3Blue1Brown — Gradient descent, how neural networks learn (Deep Learning, Chapter 2, YouTube)
VideoStatQuest with Josh Starmer — Stochastic Gradient Descent, Clearly Explained! (YouTube)
CourseCoursera — Deep Learning Specialization, Course 2 Week 2: Optimisation Algorithms, Andrew Ng (free audit)
ReferenceSebastian Ruder — An overview of gradient descent optimisation algorithms (ruder.io/optimizing-gradient-descent)
Coursefast.ai — Practical Deep Learning for Coders Lesson 4–5: training loop and optimisers (free)
Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Ctrl/Cmd + Enter to send

Try it yourself

A scratch console for this page's ideas — ungraded, nothing you run here is recorded.

Scratch console

A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.

Output appears here.