From Zero to Model: A Complete Course in AI Development
Module 2 of 7

The Training Loop

In Module 1 you learned that a model has weights, and that a loss score measures how wrong it is. But how does the model actually get better? This module explains the engine behind all of it: gradient descent, backpropagation, and the learning rate. No prior math knowledge needed.

By the end of this module you will be able to

The Problem: Millions of Dials, One Score

At the end of Module 1, we left the model with a loss score: a number measuring how wrong its prediction was. The question is: what do you do with that number? You need to adjust the weights to make the loss smaller. But a large model has hundreds of millions of weights. You cannot manually adjust each one. You need a systematic method for figuring out which direction to nudge each weight, and by how much.

That method is called gradient descent. And despite the mathematical-sounding name, the intuition behind it is something you understand from everyday life.

Gradient Descent: Finding the Valley in the Dark

Imagine you are dropped blindfolded onto a hilly landscape, and your goal is to find the lowest valley. You cannot see anything. What do you do? You feel the ground under your feet. If it slopes downward to your left, you step left. If it slopes downward in front of you, you step forward. You always step in the direction the ground is going down. Step by step, you work your way toward lower ground.

Gradient descent is exactly this. The "landscape" is the loss landscape from Module 1: a surface where the height at any point represents how wrong the model is with that combination of weights. The model cannot see the whole landscape. But at any given point, it can feel which direction is downhill by calculating the slope of the loss. It then takes a small step in that direction. After millions of such steps, the model has descended to a valley: a set of weights where the loss is low and the predictions are good.

The analogy in full

You are lost in foggy mountains. Your goal: reach the lowest valley. Your only tool: you can feel the slope of the ground right where you are standing. You step downhill. Then you feel the slope again and step downhill again. You cannot see the whole mountain. You just keep stepping downhill, one small step at a time.

Gradient descent is a blindfolded hiker. The mountain is the loss landscape. Each step is one weight update. The valley is a set of weights that makes good predictions.

What "Gradient" Actually Means

The word "gradient" is just a technical term for slope. When you calculate the gradient of the loss with respect to a weight, you are asking: "if I increase this weight by a tiny amount, does the loss go up or down, and by how much?" The answer tells you which direction to nudge that weight.

If increasing a weight makes the loss go up, you should decrease it. If increasing it makes the loss go down, you should increase it more. The gradient gives you both the direction (up or down) and the steepness (a small nudge or a large one). Every weight in the model gets its own gradient, telling it exactly how to change.

Plain English summary Gradient descent is the process of repeatedly calculating how wrong the model is, then nudging every single weight slightly in the direction that would make the prediction a little better. Do this millions of times and the model learns.

Backpropagation: How the Blame Gets Shared

Here is a subtle question. The model makes a prediction, we calculate the loss at the output, and we want to update the weights. But in a deep network, there might be 50 layers between the input and the output. How does a weight deep inside the network know that its tiny contribution to the final answer was slightly off, and in which direction to change?

The answer is backpropagation. After calculating the loss at the output, the model works backwards through the network, layer by layer, passing blame back toward the input. Each layer receives information about how much its output contributed to the final mistake, and passes a portion of that blame back to the layer before it.

Think of a factory assembly line. A finished product comes out broken. You start at the end and ask: which step in the assembly line caused this? The inspector traces back: the problem was introduced at station 4, because station 3 fed it a slightly warped part, which itself came from station 2 using the wrong material. Each station learns its share of the blame. Each one adjusts its process a little. The next product comes out better.

Backpropagation is the mathematical version of this blame-tracing. It is efficient: instead of testing every weight individually to see how it affects the loss, backpropagation calculates every gradient in a single backwards pass through the network. One forward pass to get the prediction, one backward pass to get the gradients for every weight at once.

The Learning Rate: How Big a Step to Take

Once the model knows which direction to nudge each weight, it still has to decide how far to nudge it. This is the learning rate: a small number (typically something like 0.001 or 0.0001) that controls the size of each step.

Too large a learning rate and the model takes giant steps. It might step right over the valley and land on the other side of the hill, then step back over again, oscillating without ever settling. The loss bounces around instead of descending. Training fails.

Too small a learning rate and the model shuffles forward in tiny steps. It will eventually find the valley, but it will take an impractically long time to get there. Training works but is painfully slow.

The right learning rate sits in between: large enough to make meaningful progress, small enough to step carefully and not overshoot. Finding it is part science, part experience, and a major focus of modern research. Many practical systems use a learning rate schedule: start with a larger rate to make fast early progress, then gradually reduce it as training continues, like slowing down as you approach the valley.

Interactive — Live Training Visualizer Try it

This visualizer trains a tiny model in your browser in real time. The model is trying to learn a simple pattern from data. Set the learning rate, click Train, and watch the loss curve descend. Try a very high learning rate and see it fail. Try a very low one and see it crawl. Find the sweet spot.

0.050
80
Epoch
0
Current loss
Status
Ready
Set a learning rate and click Train to begin.

Epochs: How Many Times to Loop Through the Data

The training loop does not run once on one example. It runs over the entire dataset, many times. One complete pass through all the training data is called an epoch. A model might train for 10 epochs, 100 epochs, or thousands, depending on the task and the dataset size.

Why multiple passes? Because one look at an example is rarely enough to learn from it. Think about learning a new language. Seeing a word once gives you a vague impression. Seeing it in ten different sentences, on ten different days, builds understanding. Each pass through the data refines the weights a little further.

But there is a limit. If you run too many epochs, the model starts to memorise the training data instead of learning the underlying pattern. It becomes like a student who memorises the practice exam answers perfectly but cannot handle any question phrased differently. This problem is called overfitting, and it is one of the central challenges of machine learning. You will learn how to detect and prevent it in Module 5.

Putting the Full Loop Together

The training loop is four steps, repeated for every example in the dataset, for every epoch:

01
Forward pass
Feed an input through the model, layer by layer, to produce a prediction.
02
Calculate loss
Compare the prediction to the correct answer. Compute the loss score: how wrong was the model?
03
Backward pass (backpropagation)
Work backwards through the network, calculating how much each weight contributed to the mistake.
04
Update weights
Nudge every weight in the direction that reduces the loss, by an amount controlled by the learning rate.

That cycle: forward, loss, backward, update. Repeat millions of times. That is how a model learns. Everything else in training, batches, learning rate schedules, optimisers, is a refinement of this basic loop. The loop itself is the whole story.

The most important thing to remember Training is not magic. It is arithmetic, repeated. The model does not "understand" anything. It adjusts numbers to make a score go down. The intelligence that emerges comes from doing this billions of times, on billions of examples, until the weights happen to encode genuinely useful patterns. Scale is the ingredient that turns arithmetic into apparent understanding.

What Comes Next

You now know how a model learns: gradient descent to find the direction, backpropagation to assign blame across layers, and the learning rate to control step size. What you have not yet seen is the fuel for all of this: data. How you prepare your data, tokenise it, and organise it into batches is what Module 3 covers. Bad data is the most common reason models fail, even when everything else is working perfectly.

Module 2 Quiz · 4 Questions
1. What does gradient descent do, in plain English?
2. What is the job of backpropagation?
3. You set the learning rate very high and notice the loss bounces up and down instead of going down steadily. What is happening?
4. A model trains for 500 epochs and achieves near-perfect results on the training data, but performs poorly on new examples it has never seen. What most likely happened?
← Module 1: What a Model Is Module 3: Data and Tokenization →