◆ AI All-in-One

Level 1 Use AI · Getting Started

How Machines Learn

The one loop behind almost all of modern AI. Show the model examples, measure how wrong it is, nudge its adjustable knobs to be a little less wrong, and repeat.

Level
●●●●● Beginner
Reading
11 min
To learn it well
40 minutes to read, 20 minutes to play
Builds on
1 concept →
↗ Plan a path here

Read first

What Is AI?

This leads to

Large Language Models, Explained SimplySupervised, Unsupervised and Reinforcement Learning
The full path from zero — 1 concept before this one
  1. What Is AI?
  2. How Machines Learn

Read them roughly in this order. See it on the concept map →

Why it matters

When people say an AI system "learned" something, it sounds mysterious, as if the computer sat down and studied. It is much more mechanical than that, and much more understandable. Almost every AI system you have heard of — the spam filter, the face recogniser, the chess engine AlphaZero, ChatGPT — was produced by the same basic loop:

  1. Show the model some examples.
  2. Measure how wrong its answers are.
  3. Adjust the model a little so it would have been less wrong.
  4. Repeat, many times.

That is it. The differences between a spam filter and a chatbot are in what the model is, what examples it sees and how many adjustments are made. A spam filter might have a few thousand adjustable numbers. GPT-3, released in 2020, had 175 billion. The loop is the same. If you understand this chapter, every later chapter is a variation on it.

The intuition

Rules versus learning

Imagine you want a computer to estimate monthly rent for flats in your city.

The old approach is a rule-based system: you, the expert, write the rules. "Start at 300 euros. Add 15 euros per square metre. Add 100 if there's a balcony. Subtract 50 if it's above the fourth floor with no lift." This works if you know the rules and they don't change. But you probably don't know the exact numbers. Is it 15 euros per square metre or 18? And the market moves every year.

The machine learning approach turns this around. You collect a table of real flats and their actual rents. You tell the computer the shape of the rule — "rent is some number of euros per square metre, times the size" — but leave the number blank. The computer then finds the number that best matches the real rents. You supply the examples. The computer supplies the details.

The four ingredients

Every learning system has the same four ingredients.

1. Data examples, each with its true answer size 30 m² → 620 € 2. Model a recipe with knobs (parameters) Predictions e.g. 300, 500, 700, 900 € 3. Loss compare predictions with true answers: average error 600 € 4. Nudge the knobs turn each knob in the direction that lowers the loss true answers: 620, 980, 1,410, 1,790 € …then repeat with the new knob settings, thousands or millions of times new settings
Fig. 1 The learning loop. Examples go into the model, which makes predictions. The predictions are compared with the true answers to measure the error, and the error is used to nudge the model's knobs. Then the loop repeats.
  1. Data. A collection of examples, each pairing an input (the flat's size) with the right answer (its rent). The examples used for learning are the training data.
  2. A model with knobs. A model is a recipe that turns an input into an output, a prediction. The recipe contains adjustable numbers called parameters. Think of them as knobs on a mixing desk. Turning a knob changes every prediction the model makes. In our rent example there is one knob: euros per square metre.
  3. A measure of error. A single number saying how wrong the model's predictions are, across all the training examples. It is called the loss (or error, or cost). Big loss means bad predictions, zero loss means perfect ones.
  4. A way to nudge the knobs. A procedure that turns the knobs a little in whichever direction makes the loss smaller.

Run the loop of predict, measure, nudge over and over and the loss falls. When it stops falling, the knobs are set as well as they can be, and the model is trained.

Key idea Machine learning = a model with adjustable knobs + examples + a score for how wrong it is + a procedure that turns the knobs to lower that score, repeated until the score stops improving.

How it works: a toy example you can follow by hand

Here are four flats from an imaginary city.

Flat Size (m²) Real rent (€/month)
A 30 620
B 50 980
C 70 1,410
D 90 1,790

Our model is the simplest one possible:

predicted rent = w × size

where w is our single knob, "euros per square metre". Learning means finding a good value for w.

Step 1: guess. We know nothing, so start with w = 10. The model predicts 300, 500, 700 and 900 euros. Those are all far too low.

Step 2: measure the error. For each flat, how far off was the prediction? 320, 480, 710 and 890 euros. The average is 600 euros. That is our loss.

Step 3: nudge. Try turning the knob up a little, to w = 11. The average error drops to 540. Try turning it down, to w = 9. The error rises to 660. So up is the right direction. Move to 11 and repeat.

Step 4: repeat. Keep nudging. Here is how the loss changes as the knob turns:

Knob w Predictions (A, B, C, D) Average error
10 300, 500, 700, 900 600 €
15 450, 750, 1,050, 1,350 300 €
20 600, 1,000, 1,400, 1,800 15 €
25 750, 1,250, 1,750, 2,250 300 €

At w = 20 the predictions are close. Turning the knob further up makes things worse again, because now the predictions are too high. The best setting is around 19.9, where the average error bottoms out at about 14 euros. No single value is perfect, because real rents don't follow an exact rule. Flat C is a bit dear for its size, perhaps it has a balcony.

0 30 50 70 90 0 500 1,000 1,500 2,000 10 15 20 25 30 0 300 600 w = 10 w = 15 w = 20 rent (€) size (m²) average error (€) knob w (€ per m²) 600 € 300 € best: about 14 € bottom of the valley
Fig. 2 Left: the four flats and the model's predictions for three knob settings. At w = 10 the line is far too low; at w = 20 it passes almost through every point. Right: the average error for every setting of the knob. Learning means finding the bottom of this curve.

The right-hand picture is the key one. For each possible setting of the knob there is a loss, and together they form a valley. Learning is walking downhill in that valley to the bottom. We did it by trial nudges. Real systems use a smarter method called gradient descent, which calculates the downhill direction directly instead of trying both ways. That matters when there are billions of knobs, because trying each one in both directions would take forever.

From one knob to billions

Our model has one knob. A slightly better rent model might have five: a base price, euros per square metre, a balcony bonus, a floor adjustment and a city-centre premium. That is called linear regression, and the loop is identical.

A model that recognises photos has millions of knobs, arranged in a neural network, where each knob controls how strongly one artificial neuron listens to another. A large language model has billions. GPT-3 had 175 billion parameters. Some open models of 2025, such as Moonshot AI's Kimi K2, have about a trillion, and most big labs no longer publish the number for their own models. Nobody turns these knobs by hand or even looks at most of them. The loop does all the work, running on thousands of specialised chips (GPUs) for weeks or months.

What changes as models grow is not the loop but three things:

  • The model's shape. Different arrangements of knobs suit different data: convolutional networks for images, Transformers for text.
  • The data. Rent models learn from thousands of flats. Language models learn from trillions of words.
  • The measure of error. For rents it is "how many euros off". For a language model it is "how surprised were you by the actual next word". Loss Functions covers the common choices.

The real test: new examples

Here is a trap. Suppose our model had one knob per flat: "flat A costs 620, flat B costs 980…". It would have zero error on the training data. It would also be useless, because it has learned nothing about flats it hasn't seen.

The point of learning is to do well on new examples, ones that weren't in the training data. This ability is called generalisation. To check it, you keep some examples aside and never let the model train on them. These held-back examples form the test set. If the model does well on the training data but badly on the test set, it has memorised rather than learned. This failure is called Overfitting and the Bias–Variance Tradeoff, and it is one of the central problems of machine learning. See Training, Validation and Test Sets.

Watch out A model is only as good as its examples. If every flat in the training data is in the city centre, the model will overprice flats in the suburbs, and it won't warn you. Learned models quietly absorb whatever patterns — and gaps and biases — their data contains.

In code (optional)

You don't need to code to follow this guide's first part, but if you want to see the loop run, here is the toy example in about 20 lines of plain Python. You can paste it into a free online notebook such as Google Colab.

# Flats: size in square metres, monthly rent in euros
sizes = [30, 50, 70, 90]
rents = [620, 980, 1410, 1790]

def average_error(w):
    # how far off, on average, is "rent = w x size"?
    errors = [abs(w * s - r) for s, r in zip(sizes, rents)]
    return sum(errors) / len(errors)

w = 10.0      # the model's single knob: euros per square metre
step = 1.0    # how far to nudge the knob each time

for attempt in range(40):
    here, up, down = average_error(w), average_error(w + step), average_error(w - step)
    if up < here:
        w += step
    elif down < here:
        w -= step
    else:
        step /= 2          # neither direction helps: take smaller nudges
    if attempt % 5 == 0:
        print(f"attempt {attempt:2d}: w = {w:6.3f}  average error = {average_error(w):6.1f} euros")

print(f"learned: rent = {w:.2f} x size")
print(f"prediction for a 60 m2 flat: {w * 60:.0f} euros")

# attempt  0: w = 11.000  average error =  540.0 euros
# attempt  5: w = 16.000  average error =  240.0 euros
# attempt 10: w = 20.000  average error =   15.0 euros
# attempt 15: w = 19.875  average error =   14.4 euros
# ...
# learned: rent = 19.89 x size
# prediction for a 60 m2 flat: 1193 euros

The last line is the whole point. We never told the program the rent of a 60 m² flat. It worked out a rule from four examples and applied it to a new case.

Common pitfalls in thinking about learning

  • "The model understands rent." It found one number that fits four points. It knows nothing about balconies, neighbourhoods or the housing market. Bigger models capture far richer patterns, but they are still patterns in their training data.
  • "More knobs is always better." More knobs let a model fit more complex patterns, but also make it easier to memorise noise. It needs more data to stay honest.
  • "Zero training error means success." Usually it means memorisation. Judge a model on the test set.
  • "The machine chose the goal." People choose the data, the model's shape and the measure of error. The machine only turns knobs. If the measure of error rewards the wrong thing, the machine will faithfully optimise the wrong thing.

Where it leads

A bit of history

The idea of fitting a rule to data by minimising error is old. Adrien-Marie Legendre (1805) and Carl Friedrich Gauss (1809) published the method of least squares to fit planetary orbits to noisy astronomical observations. The idea of a machine that learns came with the computer. In 1959 Arthur Samuel at IBM described a checkers program that improved its own play, and popularised the term "machine learning". In 1958 Frank Rosenblatt built the The Perceptron, a machine that learned to recognise simple shapes by adjusting its weights after each mistake. The loop in this chapter is the direct descendant of both.

Try it

  1. Try Google's Teachable Machine (linked below). Train a model to tell two hand gestures apart using your webcam. Then test it with a third person's hands, or in different lighting. When does it fail, and why?
  2. By hand: add a fifth flat of 40 m² renting for 900 €. Does the best value of w go up or down? (Hint: 900 ÷ 40 = 22.5, higher than the others.)
  3. If you ran the code: change the starting value to w = 50.0. Does it still find about 19.9?

Check your understanding

4 quick questions. Pick an answer to see whether you were right and why. Your best score is saved on this device.

  1. In the rent example, the model is "rent = w × size". At w = 10 the average error is 600 €, at w = 11 it is 540 €, at w = 9 it is 660 €. What should the next nudge be?

  2. A model gets zero error on its training flats but badly misprices new flats it has never seen. What has happened?

  3. Every flat in your training data is in the city centre. What will the trained model most likely do with a suburban flat?

  4. Which of these does the machine choose for itself during training?

Finished this chapter?

Learn more

Hand-picked courses, books, videos and tutorials for going deeper. See all resources →

Course

Machine Learning Crash Course

Google · Beginner · Free

Short, practical lessons with interactive visualisations covering regression, classification, embeddings and neural nets.

Video

Neural Networks (video series)

3Blue1Brown · Beginner · Free

Gorgeous animated explanations of neural nets, gradient descent, backprop, Transformers and attention.

Interactive

Teachable Machine

Google · Beginner · Free

Train an image, sound or pose classifier in your browser with your webcam or microphone, no code needed. The quickest way to feel what training on examples means.

Interactive

TensorFlow Playground

Google · Beginner · Free

Train a small neural network in your browser and watch it learn decision boundaries live.

Course

Elements of AI

University of Helsinki & MinnaLearn · Beginner · Free

A gentle, maths-free online course on what AI is and how it works. Over a million people have taken it.

Facts checked as of 30 November 2026. Fast-moving details (products, prices, laws) change often. · Source: content/entries/start/how-machines-learn.md