Skip to content
I.STEM Lab

Concept · Back to Basics

How machine learning models learn: loss, gradients and why data matters

A plain-language tour of training, overfitting and generalisation, so you know what a model can and cannot be trusted to do.

Intermediate6 min read

Key takeaways

  • A model is a function with adjustable settings. Training adjusts them to reduce a measure of error called the loss.
  • Gradient descent improves the settings in small steps, like walking downhill in fog.
  • A model learns patterns in its training data, so poor, narrow or biased data produces a poor, narrow or biased model.
  • Always test on data the model has not seen, and in conditions like those of real use.

Machine learning can feel like a black box that magically finds patterns. In truth, the central idea is surprisingly simple, and understanding it makes you a much sharper user of AI tools. It helps you see why a model fails in a new region, why more data does not always help and why the way you measure success shapes what you get.

This article explains how models learn, with a tiny example you can follow by hand.

A model is a machine with knobs

Think of a model as a function that takes inputs and returns an output. A simple example is a line, y equals w times x plus b, where w and b are the settings, often called parameters or weights. Change the settings, and the line changes. A neural network is the same idea with millions or billions of settings. Training means finding the settings that make good predictions.

Measuring wrongness: the loss

To improve anything, you need a way to measure how bad it is now. That measure is the loss function. For predicting a number, a common choice is the average of the squared differences between prediction and truth. For classifying images, it is a measure of how much probability the model gave to the correct answer. A lower loss means better predictions.

Gradient descent: walking downhill in fog

Imagine standing on a hilly landscape in thick fog, trying to reach the lowest point. You cannot see far, but you can feel which way the ground slopes under your feet. So you take a small step downhill, feel again and repeat. That is gradient descent. The landscape is the loss, your position is the setting of the model and the slope is the gradient, found with calculus.

A tiny example makes it concrete. Suppose a single setting w should be 3, and the loss is the square of the distance from 3. The gradient is two times (w minus 3). Start at w equals 0, and use a step size, called the learning rate, of 0.1.

  • Step 1: the gradient is 2 × (0 − 3) = −6. Move w to 0 − 0.1 × (−6) = 0.6.
  • Step 2: the gradient is 2 × (0.6 − 3) = −4.8. Move w to 1.08.
  • Step 3: w becomes 1.464. Step 4: 1.7712.
  • Each step gets closer to 3, and the steps shrink as the slope flattens.

The learning rate matters. Too small, and training crawls. Too large, and the steps overshoot the bottom and bounce around, or even diverge. Real training adjusts millions of settings at once, using the same principle.

Training, validation and test data

  1. 01Training data: the examples the model learns from.
  2. 02Validation data: examples held back to compare settings and decide when to stop.
  3. 03Test data: examples used once at the end, to estimate real-world performance.

Keeping them separate is essential. A model that is judged on the examples it has learned from is like a student who sees the exam questions in advance.

Overfitting and generalisation

A model can learn the training data too well, memorising quirks and noise instead of the underlying pattern. It then performs brilliantly on training data and poorly on anything new. This is overfitting. The aim is generalisation, which means performing well on data that the model has not seen.

  • Warning sign: training accuracy keeps rising while validation accuracy stalls or falls.
  • Remedies: more varied data, simpler models, regularisation, data augmentation and stopping training earlier.

Why data decides so much

  • A model can only learn patterns that are in its data. If crops appear only in summer images, it will not recognise them in winter.
  • Mislabelled examples teach the wrong lessons, and noisy labels cap how good a model can be.
  • Imbalanced classes lead a model to favour the common one, because it scores well while ignoring the rare one.
  • Bias in the data becomes bias in the model. If one region or group is missing, the model will serve it poorly.
  • Domain shift, such as a new sensor, season or country, can quietly reduce accuracy.

Scaling and units

Models are sensitive to the numbers you give them. If one input ranges from 0 to 1 and another from 0 to 100,000, the large one can dominate training. Standardising inputs to a similar scale is a routine step. Units matter too: feeding a model elevation in metres in training and in feet at use gives nonsense, without any error message.

What fine-tuning means

Fine-tuning takes a model already trained on a large, general dataset and continues training on a smaller, specific one. It is like hiring an experienced professional and teaching them your organisation’s way of working. It saves time and data, and is common for image models and language models. It does not remove the need for good, representative data in your own setting, and for honest testing.

Randomness and reproducibility

Training involves randomness: the starting values, the order of examples, and the way data is split. Two runs can give slightly different results. Fix the random seeds, record the settings, and run a few repeats before drawing conclusions from a small difference.

Frequently asked questions

Does more data always make a model better?
Not always. More varied, accurate data helps. More of the same, or data with errors, can add little or even hurt.
Why does my model work in the lab and fail in the field?
Usually because the field data differs from the training data, through a different season, sensor, region or quality. Test on conditions that match real use.
What is the difference between a parameter and a hyperparameter?
Parameters are learned during training, like the weights. Hyperparameters are choices you make beforehand, such as the learning rate or the number of layers.
Do I need to understand calculus to use machine learning?
Not to use it. But knowing what a gradient and a loss are will help you diagnose problems and talk sensibly with specialists.

A model is not intelligent in the human sense. It is a very flexible function, adjusted to fit examples. Knowing that tells you where to trust it, where to test it and what you must supply yourself: good data, a sensible measure of success and the judgement to apply it in context.

Share

Join the conversation

Reactions & comments

Comments are opening soon.

In the meantime, tell us what you think by email at info.istem.lab@gmail.com.

Start a project

Need this built for your organization?

If you would rather have a working system than a tutorial, tell us your goal and we will scope it with you.