The Loop

An interactive primer  ·  three live demos

It learns by repeating.

Nobody programs the answers. A model starts with random numbers, guesses, measures how wrong it was, nudges the numbers, and goes again — thousands of times. Everything we call AI is that loop, run at a scale where it stops feeling like a loop.

Start from the idea
3 live demos 0 libraries 100% in your browser
01 / THE IDEA

Nobody writes the rules.

A normal program is a list of instructions a person worked out. A model is a pile of adjustable numbers that finds its own instructions from examples. That difference is the whole subject.

The old way — rules written by hand

A program

// a human decided all of this
if (subject.includes("FREE MONEY")) spam++;
else if (sender.endsWith(".ru"))      spam++;
else if (links > 12)                    spam++;
// …and 10,000 more rules

Every rule is something a person had to think of. Spam changes, and the rules quietly rot. Nobody can write down the rule for “this face is Isaac” at all.

The learning way — rules found from data

A learner

for each (example, answer) in data:
    guess  = model(example)
    error  = guess − answer
    numbers -= step × error      // repeat

Nobody wrote “FREE MONEY”. The model searches for whatever separates the examples — including patterns you would never think to write down, and some you would rather it hadn’t.

Training is not teaching. It is search. The model has numbers — weights — and training keeps nudging them in whichever direction makes the error smaller. No understanding is inserted. The numbers just get less wrong.

01

Data

Examples. For supervised learning, each one carries the answer you want: this email is spam, this photo is a cat, this house sold for $410k.

02

Model

A function with adjustable numbers. Start simple — a straight line is a model. A language model is the same thing with hundreds of billions of numbers.

03

Loss

One number that says how wrong the model is right now. Training exists to make this number go down. Everything else is detail.

02 / THE LOOP

Watch a model learn to separate two things.

This is a real classifier — logistic regression, trained by gradient descent, in this page. Every dot is an example with two measurements. The model starts knowing nothing. Each repeat: guess every dot, measure the error, adjust the line.

Classifier  /  2 inputs  /  3 numbers idle — press run
dataset
class A class B decision boundary
repeats0
loss0.693
accuracy—
weights—
loss over repeats
The shaded areas are the model’s current opinion. A straight boundary can only ever draw a straight line — try “Non-linear” and watch it fail honestly.
Watch the boundary

It starts in a random orientation and rotates toward a position that puts class A on one side and class B on the other. The line is not drawn by hand; it is what the three numbers currently mean.

Watch the loss

Loss is the only thing training minimises. It falls fast at first, then flattens — the model is getting the easy examples right and grinding on the hard ones. A flat curve is not failure; it is “this model has learned what it can”.

Watch the repeats

One repeat = one pass through the data = one update. Real systems do this millions of times. Nothing accumulates except the numbers — which is why training can stop and resume without losing the model.

03 / INSIDE

Three numbers did that.

The classifier you just trained has exactly three adjustable numbers: two weights and a bias. That is the entire “brain”. It works because the problem is simple — not because the maths is deep.

x₁x₂x₃ w₁w₂w₃ Σ + b weighted sum σ squash ŷ 0…1
One neuron. A network is this, stacked and wired differently.

The neuron does one thing: it multiplies each input by a weight, adds them up, adds a bias, and squashes the result into a probability.

ŷ = σ( w₁x₁ + w₂x₂ + w₃x₃ + b )

A weight says how much an input matters and in which direction. A negative weight flips the evidence. A bias shifts the whole decision, which is how a model can be biased toward saying yes even when the inputs are weak.

A modern network is this same unit repeated in layers — millions or billions of weights. No layer does anything a single neuron cannot do; there are just enough of them, wired together, to represent extremely complicated boundaries. Gradient descent still trains every one of those numbers the same way it trained the three in the lab above.

What the numbers cannot do: they cannot know anything the data did not contain, and they cannot tell you when they are guessing. A confident output is still just a number between 0 and 1.

04 / OVERFIT

More learning is not always better.

A model can get every training example right and still be useless, because it learned the noise instead of the pattern. This is overfitting — the most important failure in machine learning, and the one that looks most like success.

Curve fit  /  14 training points  /  90 unseen points drag the flexibility slider
training data model the real pattern
train error—
test error—
flexibility3
verdictbalanced
A straight line underfits; a wild curve memorises. Somewhere between them the model captures the shape and ignores the noise.
Both errors are measured on data the model did not train on. That distinction is the entire reason we can tell learning from memorising.

The two failures

Underfitting — the model is too simple to represent the pattern. Training error is high, test error is high, and more data will not save it. The fix is a more flexible model or better inputs.

Overfitting — the model is flexible enough to memorise each training point, including the noise. Training error falls toward zero while test error climbs. The fix is more data, a simpler model, or stopping early.

The gap between training and test performance is the honest measure. A model that scores 99% on data it has seen and 61% on data it has not has learned almost nothing useful.

Three defences

Hold data back. Split examples into train and test before you touch anything. The test set is the exam; peeking at it turns the exam into more training.

Punish complexity. Add a cost for large weights (regularisation) so the model prefers a smooth answer unless the data really demands a wiggle.

Stop early. Watch the validation error while training. When it starts rising, the useful learning is over — even though the training loss is still falling.

05 / REWARD

Sometimes nobody tells you the answer.

Supervised learning needs labelled examples. Reinforcement learning gets only a reward — and has to work out which of its many actions earned it. This agent starts moving at random. Every episode is a repeat; over repeats, it finds the short path.

Q-learning  /  6×6 grid  /  reward at the far corner untrained — 0 episodes
agent goal wall learned policy
episodes0
avg steps—
success—
best—
steps per episode
Each arrow is the action the agent currently believes is best in that square. Early on they point nowhere useful. The reward is only at the goal, so credit for arriving has to travel backwards, one square at a time.
The agent is exploring: it moves mostly at random, so it finds the goal by accident and slowly remembers which moves came before it.
Reward, not instruction

Nobody says “go right, then up”. The environment only says +1 at the goal and −0.02 per step. The agent’s job is to maximise the total, which quietly implies “get there fast”.

Explore vs exploit

It has to keep trying random moves (exploration) while using what it already knows (exploitation). Stop exploring too early and it locks into a mediocre route forever.

Credit assignment

The reward arrives at the end. Which of the forty moves deserves the credit? Q-learning answers by backing the value up one step at a time — repeat enough times and the whole path becomes valuable.

06 / THE TYPES

The shape of the feedback.

The loop is always the same. What changes is what the model is given: the answer, no answer, or a score.

Supervised

Learn from answers

Every example comes with the label you want. The model compares its guess to the truth and adjusts. This is the workhorse of industry.

  • Spam / not spam
  • Does this scan show a fracture?
  • Predict tomorrow’s demand

Needs: labelled data, which is expensive and sometimes impossible.
Fails when: the labels are wrong, or the world shifts under it.

Demo 02 — you watched the labels do the teaching.
Unsupervised

Find structure

No labels at all. The model looks for grouping, compression or oddity in the data. You judge the result, because there is no answer key.

  • Customer segments
  • Compressing an image
  • Spotting an unusual transaction

Needs: a good notion of “similar”.
Fails when: the clusters it finds are real but meaningless to you.

Used inside recommendation and fraud systems.
Reinforcement

Learn from consequences

An agent acts, the world responds, and a reward arrives — often delayed. It learns a policy: what to do in each situation.

  • Game-playing agents
  • Robot locomotion
  • Traffic-light control

Needs: a simulator or a very safe real world.
Fails when: the reward can be gamed — the agent will game it.

Demo 05 — 300 episodes of trial and error.

And a fourth, hiding in plain sight. Large language models are trained mostly self-supervised: take real text, hide the next word, and let the model predict it. The label is free because it was already in the sentence. Do that across enough text and the model is forced to absorb grammar, facts and style as a side effect of getting the next token right.

07 / THE LIMITS

Impressive is not the same as correct.

The loop explains the mechanism. It does not make the output trustworthy. These are the limits worth carrying away.

A language model predicts text

When you chat with an AI, it is doing the same thing as the classifier above: given everything so far, produce the most plausible next piece of text. It was trained on an enormous amount of writing and adjusted until its predictions were good.

That is genuinely useful, and it is also why it can be fluently wrong. A plausible sentence and a true sentence are not the same target. Nothing in the loop checks facts — only whether the next token was likely.

The data is the argument

A model inherits the gaps and biases of its examples. Train on hiring decisions that favoured one group and the model learns to repeat them — with the authority of arithmetic. Train on scraped text and it absorbs whatever that text assumed.

“The model decided” is never a full explanation. Ask what it was trained on, what it was optimised for, and who checked.

Ask

What was the training data? Whose behaviour is being reproduced, and who is missing from the examples?

Ask

What is the loss? Whatever the number rewards is what the model will get good at — including shortcuts you did not intend.

Ask

How was it tested? On data it had never seen? On people like the people it will be used on? Compared against what baseline?

08 / QUIZ

Seven questions, no tricks.

If you can answer these, you understand more about machine learning than most of the marketing does.

Not started 0 / 7

Ready when you are.

Answer in any order you like. Every answer gets an explanation, right or wrong.

09 / GLOSSARY

The words, plainly.

Fifteen terms that cover most conversations about machine learning.

Model

A function with adjustable numbers that turns inputs into a prediction. Nothing more mystical than that.

Weight

A number saying how strongly one input affects the output, and in which direction. Training changes weights.

Bias

An extra adjustable number added to the weighted sum. It shifts the decision threshold, not the model’s fairness — that is a different, also important, kind of bias.

Feature

One measurable input: a pixel, a word count, a temperature. Good features make hard problems easy; bad ones hide the pattern.

Label

The answer attached to a training example — “spam”, “cat”, “£410,000”. Only supervised learning needs them.

Loss

A single number measuring how wrong the model is. Training’s only goal is to reduce it. Choosing the loss chooses what the model cares about.

Gradient

The direction and steepness of the loss with respect to each weight. Following it downhill is gradient descent.

Learning rate

How far each update moves the weights. Too small and training crawls; too large and it overshoots and can diverge. Try 6.0 in the first demo.

Epoch

One full pass over the training data — one “repeat”. The counter in the first demo.

Overfitting

Learning the training examples, including their noise, so well that performance on new data gets worse.

Generalisation

Doing well on examples the model has never seen. The only result that matters in practice.

Inference

Using a trained model to make a prediction. No learning happens here — the numbers are frozen.

Supervised / unsupervised

Learning with answers versus learning without them, from structure alone.

Reinforcement learning

Learning a policy from rewards and consequences rather than labelled examples.

Token

A chunk of text — roughly a word-piece — that a language model predicts one at a time. “Predict the next token” is the whole training task.