Module 4 — How a machine learns
"The model learns from data": this phrase, used since the beginning of the course, describes a precise and surprisingly simple mechanism. This module takes it apart piece by piece, with no math prerequisites. By the end, "training a model" will refer, for you, to a concrete procedure — not an incantation.
A model is a function with adjustable knobs
Recall the definition from module 1: a model turns an input into a prediction. What makes this function adjustable are its parameters — internal numbers that weigh the influence of each piece of information.
Take a tiny example: estimating an apartment's price from its surface area.
estimated price = a × surface + b
Here, a and b are the two parameters. With a = 3,000 and b = 20,000, a 50 m² apartment is estimated at 170,000. With other values, the estimate changes. Learning means finding the values of a and b that give the least wrong estimates on the known examples.
A real estate model uses dozens of features; a large language model has billions of parameters. But the principle stays exactly this: numbers to tune so the output matches the examples.
The loss function: scoring the error
To tune the parameters, you first need to measure how wrong the model is. That is the job of the loss function: a single number summarizing the model's error on the training data. Large loss, bad model; small loss, good model — on that data, at least.
For our example, the classic loss is the mean squared error: for each apartment in the training set, compute the difference between estimated and actual price, square it (so positive and negative gaps do not cancel out, and large errors are penalized more), then average.
Learning then becomes an optimization problem, expressible in one sentence:
Find the parameter values that make the loss function as small as possible.
The loss function encodes what "being wrong" means to you. Should underestimating and overestimating be penalized equally? In fraud detection, does missing a fraud cost as much as blocking an honest customer? These trade-offs translate mathematically into the loss. A model optimizes exactly what it is asked to optimize — including when it is asked badly.
Gradient descent: finding the bottom of the valley
What remains is finding the minimum. Trying all combinations is impossible: with millions of parameters, the space is unimaginably vast. The field's central algorithm proceeds otherwise: gradient descent.
The standard image — and a faithful one: the loss draws a hilly landscape where every point is a combination of parameters and altitude is the error. You are dropped somewhere in this landscape, in fog. To get down, a simple strategy: feel the slope under your feet and take one step in the steepest downhill direction. Then repeat.
The gradient is the mathematical object that indicates the slope: for each parameter, it says whether increasing it would raise or lower the loss, and by how much. One descent step therefore adjusts all parameters simultaneously, each in the direction that reduces the error. Repeated massively, this procedure walks the model down to a good combination.
Two practical settings dominate practitioners' lives.
The learning rate: the step size. Too large, you leap across the valley and the loss oscillates or explodes. Too small, the descent takes unreasonable time. It is the most sensitive hyperparameter in the field.
Mini-batches: computing the slope on the whole dataset at every step would be too slow. It is computed on a small randomly drawn batch of examples — the direction is a bit noisy but sufficient, and you move much faster. One full pass over the data is called an epoch.
What this mechanism implies
Learning is a numerical process, not understanding. The model did not "grasp" that surface drives price; its parameters converged to values that minimize an average error. This nuance explains failures: if the data contains a spurious regularity (all the expensive apartments in the dataset happen to be in Paris), the model learns it with the same zeal as the true causes.
Training loss is not performance. A tiny loss on known examples can hide a model that is useless on new cases — that is overfitting, the exact subject of the next module.
The computing cost comes from repetition. Millions of steps, each over thousands of examples, each adjusting millions or billions of parameters: that is why training large models costs weeks of compute and millions of dollars, while inference — a single forward pass — stays cheap.
Key takeaways
- A model is a function with parameters; learning = tuning the parameters to minimize a loss function measured on examples.
- Gradient descent finds that minimum step by step: compute the slope, move, repeat.
- Learning rate and mini-batches are the two essential practical settings; choosing the loss is a business decision as much as a technical one.
- The model optimizes what you measure — including spurious regularities. Data quality is not a detail; it is the core.
Next module: the two ways this learning fails — overfitting and underfitting — and the standard remedies.