Sonith Manakkat

Algorithms, part 10 of 128 min read11 visuals

How machine learning algorithms learn

Learning from examples instead of fixed rules, gradient descent as walking downhill, decision trees, neural networks, overfitting, and how this connects to language models.

  • Machine learning adjusts a model to reduce its error on examples
  • Gradient descent repeatedly steps downhill, and the step size matters
  • A model must be tested on data it has not seen, and it can still be wrong

Share on LinkedIn

Share
Reading level

Most algorithms in this series follow rules a person wrote down. Machine learning flips that: instead of writing the rules, you show a program many examples and let it adjust itself until it gets them right. This part explains how that adjustment works, using hills, questions and dials. It is a general overview, not a guide to any specific tool. Each section has a plain layer and an Expert view.

Learning from examples

Hand-written rules

  • A person lists words that look suspicious
  • Breaks when senders change their wording

Learned from examples

  • Thousands of messages marked spam or not spam
  • The program finds patterns that separate the two
Two ways to build a spam filter.

There are two phases. In training, the model adjusts itself using example data. In using (also called inference), the finished model makes predictions on new inputs without changing.

Example dataTrainingTrainedmodelPredictions
The usual life of a model.
  1. 1Take some examplesInputs with known right answers
  2. 2PredictThe model makes its current best guess
  3. 3Measure the errorCompare each guess with the right answer
  4. 4Adjust the settingsNudge them so the error shrinks
  5. 5RepeatUntil the error stops improving

Takeaway. Training is a loop of guessing, measuring and nudging, repeated many times.

The training loop: predict, measure the error, adjust, and go round again.

Error as a hill, learning as walking downhill

A ball starts on a hill of "error". Rolling downhillmeans getting less wrong.Step 1: the slope says which way is down; the ballis now at 1.32.Step 2: the slope says which way is down; the ballis now at 0.53.Step 3: the slope says which way is down; the ballis now at 0.21.Step 4: the slope says which way is down; the ballis now at 0.08.Step 5: the slope says which way is down; the ballis now at 0.03.Step 6: the slope says which way is down; the ballis now at 0.01.Step 7: the slope says which way is down; the ballis now at 0.01.Step 8: the slope says which way is down; the ballis now at 0.00.
A sensible step size: the ball settles into the valley.
Show the 9 steps as text
  1. A ball starts on a hill of "error". Rolling downhill means getting less wrong.
  2. Step 1: the slope says which way is down; the ball is now at 1.32.
  3. Step 2: the slope says which way is down; the ball is now at 0.53.
  4. Step 3: the slope says which way is down; the ball is now at 0.21.
  5. Step 4: the slope says which way is down; the ball is now at 0.08.
  6. Step 5: the slope says which way is down; the ball is now at 0.03.
  7. Step 6: the slope says which way is down; the ball is now at 0.01.
  8. Step 7: the slope says which way is down; the ball is now at 0.01.
  9. Step 8: the slope says which way is down; the ball is now at 0.00.

The method is called gradient descent. At each step the program works out the slope of the loss at the current settings and moves a little in the downhill direction. The size of each move is the learning rate.

A ball starts on a hill of "error". Rolling downhillmeans getting less wrong.Step 1: the step was too big, so it overshoots to-1.8: worse than before.Step 2: the step was too big, so it overshoots to2.2: worse than before.Step 3: the step was too big, so it overshoots to-2.6: worse than before.Step 4: the step was too big, so it overshoots to3.1: worse than before.Step 5: the step was too big, so it overshoots to-3.7: worse than before.
Too large a step: the ball overshoots and bounces around.
Show the 6 steps as text
  1. A ball starts on a hill of "error". Rolling downhill means getting less wrong.
  2. Step 1: the step was too big, so it overshoots to -1.8: worse than before.
  3. Step 2: the step was too big, so it overshoots to 2.2: worse than before.
  4. Step 3: the step was too big, so it overshoots to -2.6: worse than before.
  5. Step 4: the step was too big, so it overshoots to 3.1: worse than before.
  6. Step 5: the step was too big, so it overshoots to -3.7: worse than before.
Expert viewThe update rule

In words: new value = old value minus step size times slope. The slope (the gradient, when there are many settings) points uphill, so subtracting it moves downhill. A tiny step size is safe but slow; a large one can overshoot, bounce, or even diverge. Real loss landscapes have millions of dimensions, plateaus and many valleys, and gradient descent usually finds a good valley, not provably the best one.

Computing the slope on all the data at every step is expensive, so stochastic and mini-batch versions estimate it from one example or a small random batch. The estimate is noisy but cheap, and the noise can even help. Many practical variants adapt the step size during training.

Decision trees: twenty questions

  1. Is the sky cloudy?If no, answer: no umbrella
  2. Is rain forecast?If yes, answer: take an umbrella
  3. OtherwiseAnswer: no umbrella
A tiny tree for "will I need an umbrella?"
Expert viewHow trees are learned

Training builds the tree greedily: at each node, try candidate questions about the features and keep the one that best separates the example labels, using a measure of how mixed the groups remain (for example Gini impurity or entropy). Stop at a depth limit or when groups are pure. A single deep tree tends to memorise its training data, so practical use often combines many trees, for example by averaging many trees trained on different random samples. Trees are easy to explain, which is a real advantage.

Neural networks: many tiny dials

Input layerThe numbers describing the exampleHidden layersMany adjustable dials (weights)Output layerThe prediction
A neural network in layers.
Expert viewBackpropagation in one paragraph

Each connection has a weight, and each unit applies a simple non-linear function so that stacking layers can represent complicated patterns. Training repeats: run an example forward to get a prediction, measure the loss, then use backpropagation to work backwards through the layers and compute how much each weight contributed to the loss. This uses the chain rule of calculus to reuse work, so the full set of slopes costs about as much as one forward pass. Gradient descent then nudges every weight a small step downhill.

Overfitting and underfitting

Underfitting

  • The model is too simple to catch the pattern
  • Poor on training data and on new data

Overfitting

  • The model memorises noise and quirks
  • Excellent on training data, poor on new data
Fitting too little, or too much.

The standard defence is a train/test split: keep some data hidden during training, then measure performance on it. If training results are far better than test results, the model is overfitting.

  1. 1All the examplesShuffle them first
  2. 2Training setThe model learns from this part
  3. 3Validation setUsed to compare and tune choices
  4. 4Test setKept hidden for one final check

Takeaway. Judge the model only on data it never trained on.

Splitting the examples so the model is judged on data it has not seen.
Expert viewRegularisation and evaluation

Regularisation discourages over-complex models, for example by penalising large weights, limiting tree depth, or stopping training when error on held-out data starts rising. A separate validation set is used to tune choices, leaving the test set for one final check.

Accuracy can mislead on imbalanced data: if 1 percent of cases are positive, a model that always says "negative" is 99 percent accurate and useless. Precision asks: of the items the model flagged, how many were truly positive? Recall asks: of all the truly positive items, how many did the model find? There is usually a trade-off between them.

Data quality matters as much as the algorithm. Leakage happens when information from the answer, or from the test data, sneaks into training, giving flattering results that vanish in real use.

Two main kinds of learning

  • SupervisedExamples come with answers (labels); learn to predict them
  • UnsupervisedNo answers; find groups or structure in the data
  • Typical supervised taskPredicting a category or a number
  • Typical unsupervised taskGrouping similar items together
Supervised and unsupervised.

Connection to language models

A language model such as Claude is, in general terms, a very large neural network trained to predict the next piece of text given the text so far. Repeating that prediction one piece at a time produces whole replies. The ideas in this article (examples, loss, gradient descent, held-out testing) are the same family of ideas, applied at far greater scale.

To learn how to work with one in practice, see the Claude AI series and start with What is Claude AI?.

Try it by hand

The loss is x squared, a bowl-shaped curve with its bottom at 0. Its slope at x is 2x. Start at x = 3 with step size 0.3.

  1. Slope at 32 x 3 = 6
  2. New x3 - 0.3 x 6 = 1.2
  3. Slope at 1.22 x 1.2 = 2.4, so new x = 1.2 - 0.72 = 0.48
  4. Keep goingEach step multiplies x by 0.4: next 0.192
  5. Try step size 1.1Each step multiplies x by -1.2, so it overshoots further each time
Rolling the ball downhill.

A model learns by taking many small steps that reduce its error on examples.

Frequently asked questions

Does the model understand what it learns?

It captures statistical patterns that help it predict. Whether to call that understanding is debated, so rely on testing, not assumptions.

Why not always use the biggest model?

Larger models cost more to train and run, need more data, and can still overfit or be wrong.

What is the difference between training and inference?

Training changes the model's settings; inference uses the finished model without changing it.

Why hold back test data?

To measure how the model does on cases it has never seen, which is what matters in use.

Back to all articles