How machines learn

13 min read
Every model learns by repeating one loop: predict, measure, adjustTraining examplesfeatures + labelsPredictmodel with currentparametersMeasure lossone number for howwrong it isAdjust parametersgradient descent:a small step downhillrepeat until the loss stops falling

In traditional programming, a person writes rules: if the email contains "free money", mark it spam. In machine learning, the computer finds the rules itself from examples.

The vocabulary

  • Example: one data point, such as one email.
  • Features: the measurable inputs describing it (words used, sender, time sent). In deep learning, the raw input (pixels, text) is used and the model discovers its own features.
  • Label: the correct answer, if known (spam or not spam).
  • Model: a mathematical function that turns features into a prediction. It has adjustable numbers called parameters (or weights).
  • Training: adjusting the parameters so predictions match labels.

The learning loop

  1. Start with a model whose parameters are random or zero.
  2. Predict on training examples.
  3. Measure the error with a loss function: a single number saying how wrong the predictions are (for example, the average squared difference between predicted and actual house prices).
  4. Adjust the parameters to reduce the loss. The standard method, gradient descent, computes which direction to nudge each parameter to make the loss go down, then takes a small step that way.
  5. Repeat thousands or millions of times.

That's it. Every model from a spam filter to a frontier LLM is trained by some version of this loop. What differs is the model's shape, the data and the scale.

A tiny example

Predict a house's price from its size. The model is a line: price = w × size + b, with two parameters, w and b. Start with random values, compare predictions to real sale prices, and nudge w and b to shrink the error. After training, w might settle at $300 per square foot. The model has "learned" a rule nobody wrote down.

Generalization: the actual goal

A model that memorizes its training data is useless. What we want is generalization: good predictions on new examples it has never seen. Two failure modes:

  • Underfitting: the model is too simple to capture the pattern (a straight line for a curved relationship). Bad on training data and new data.
  • Overfitting: the model is so flexible it learns noise and quirks of the training set. Great on training data, poor on new data.

Much of practical ML is managing this trade-off: enough flexibility to capture real patterns, not so much that it memorizes noise.

The three kinds of learning

TypeLearns fromExample
SupervisedLabeled examplesPredict loan default from past loans and their outcomes
UnsupervisedUnlabeled data, finding structureGroup customers into segments
ReinforcementRewards and penalties from acting in an environmentTeach a robot to walk; train a game-playing agent

Check your understanding

Check 1 of 2Module 3 · Machine learning

What does a loss function do?

Check 2 of 2Module 3 · Machine learning

A model scores 99% on its training data but 60% on new data. It is:

Answer the checks, then mark this lesson complete
Completion and scores are saved in this browser.