Data, model, training, inference: the basic terms on a single example
The whole machine learning path on estimating the price of a flat: data and how to split them, a model with parameters, training by gradient descent, hyperparameters and the learning rate, evaluation, inference and a glossary of terms with links to the parts of the series.
Series AI basics Part 3 of 52
- Map of artificial intelligence: what is what and how it all fits together
- A brief history of artificial intelligence: from Turing to language models
- Data, model, training, inference: the basic terms on a single example
- How to read mathematical notation: symbols, indices, sums and quantifiers
- Sets and relations: the building blocks of definitions in AI
- Functions and mappings: why every AI model is a function
- Logic and proofs: the rules AI reasons by
- Graphs and trees: the structure that both search and neural networks stand on
- Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
- Derivatives, the gradient and the chain rule: how a model finds out which way to learn
- Gradient descent: how a model learns
- What probability is: from rolling dice to the Kolmogorov axioms
- Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
- Random variables and the cumulative distribution function: one function that tells you everything about a distribution
- Probability density and probability mass functions: why a continuous variable has zero probability at a point
- Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
- Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
- Law of large numbers and central limit theorem: why averages behave so predictably
- Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
- Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
- State-space search: BFS, DFS, UCS and A* with animations
- Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
- Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
- Logic and knowledge systems: forward and backward chaining, resolution and expert systems
- Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
- Local search and simulated annealing: a travelling salesman through the Czech regional capitals
- Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
- Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
- Ant colony optimization: how a pheromone trail finds a short path
- Particle swarm optimisation: inertia, personal experience and the swarm’s experience
- Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
- Fuzzy sets and membership functions: how to write down “somewhat warm”
- Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
- Fuzzy logic: t-norms, implications and IF–THEN rules
- Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
- Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
- Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
- Markov chains: the transition matrix, the stationary distribution and PageRank
- Hidden Markov models: the Viterbi algorithm and the dishonest casino
- Bayesian networks: why the grass is wet and what it means for the sprinkler
- The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
- Types of machine learning: supervised, unsupervised, semi-supervised and more
- Prompts, context and RAG: how to give a model the right information
- Variational autoencoders (VAE): new images from a continuous latent space
- Generative adversarial networks (GAN): the forger versus the detective
- Diffusion models: how pure noise becomes an image
- Markov decision processes: states, actions, rewards and the Bellman equations
- Q-learning and DQN: learning the value of actions, from a table to a neural network
- Policy gradient and PPO: learning the policy directly
- How an AI agent works: a language model in a loop with tools
- Programming with AI agents: the task, tests and review
- Evaluating and securing AI systems: how to measure and how to keep watch
Full series syllabus 52 of 83 topics published
- What data, model, training and inference mean and how they relate to each other.
- The difference between parameters and hyperparameters and why the learning rate matters.
- Why training is expensive once and inference cheap, but frequent.
Useful to know first: Map of artificial intelligence: what is what and how it all fits together
Four words keep coming back throughout the series: data, model, training and inference. Whether it is digit recognition, a spam filter or a large language model, the path is always the same: a function is learned from examples and then answers new questions. In this part we walk the whole path on the smallest possible example — estimating the price of a flat from its floor area — and at each step we say what it looks like in large models. At the end there is a glossary of terms with links to the parts where you can learn more about them.
It builds on the parts Map of artificial intelligence and A brief history of AI.
1. Data: examples with correct answers
Definition — data, features and labels
Data are the examples the model learns from. Each example consists of
- features — the inputs we know about it (here the floor area of the flat),
- and, in supervised learning, also a label — the correct answer (here the price of the flat).
The data are split into three mutually disjoint parts, each with a different purpose:
- training — the model learns from them (the parameters are searched for using them);
- validation — we use them to tune the learning settings and to choose between models;
- test — used only once, at the end, to give an honest estimate of how the model will do on new data.
The precise notation is in the definition “a dataset formally” below.
A textbook with exercises and the answers at the back. You learn on the practice exercises, a mock exam tells you whether you have got it, and the real exam (which you have never seen before) shows where you really stand.
Our data are made up: 60 flats, each with its floor area (the feature) and its price in millions of Czech crowns (the label). The price grows with the area, but not exactly — two flats of the same size differ in location, condition and floor. We split them into 40 training, 10 validation and 10 test flats.
Definition — a dataset formally
Let d, n ∈ ℕ, d, n ≥ 1, and let Y be the set of possible labels (Y = ℝ in regression, a finite set of classes in classification). A dataset is a finite sequence of pairs
D = ((x(1), y(1)), …, (x(n), y(n))), where x(i) ∈ ℝd and y(i) ∈ Y for i = 1, …, n.
The vector x(i) contains the d features of the i-th example and y(i) is its label. It is assumed that there is an (unknown) probability distribution on ℝd × Y from which the pairs are drawn mutually independently and from which future queries come as well.
A split of the data is given by a partition of the set of indices into three mutually disjoint sets
{1, …, n} = Itr ∪ Ival ∪ Itest, Itr ∩ Ival = Itr ∩ Itest = Ival ∩ Itest = ∅;
the parts of the data are Dtr = ((x(i), y(i)))i ∈ Itr and similarly Dval, Dtest. So each example belongs to exactly one part. Here d = 1 (floor area), Y = ℝ (price in millions of CZK), n = 60 and the parts have 40, 10 and 10 examples.
A table where each row is one flat: columns with the features and a last column with the price. Disjoint means that no flat is in two parts at once — otherwise the “real exam” would contain examples the model has already seen. And the same-distribution assumption says that the test flats come from the same market as the ones we will ask about later; without it, the error on the test would say nothing about the future.
For large language models the data are trillions of tokens of text from the web, books and code; the feature is the text so far and the label is the next token (the part on pretraining). The principle is the same.
2. Model: a function with parameters
Definition — model and parameters
Let X be a set of inputs, Y a set of outputs and Θ ⊆ ℝp, p ≥ 1, the set of admissible parameter values. A model is a family of functions
{fθ : X → Y | θ ∈ Θ},
i.e. for each choice of θ = (θ1, …, θp) ∈ Θ one function that computes an output fθ(x) ∈ Y from an input x ∈ X. The numbers θ1, …, θp are the parameters of the model (in neural networks, weights). We choose the shape of the function (and with it X, Y, Θ); learning finds the values of the parameters.
Our model is a straight line: X = Y = ℝ, Θ = ℝ2, θ = (w, b) and fw,b(x) = w · x + b, where x is the floor area, w says how much the price rises with each unit of area, and b where the line starts.
A model is a machine with knobs. Ours has two knobs. A language model has billions of them — but it is still a machine that computes an output from an input, and its “knowledge” is nothing more than the setting of its knobs.
ŷ = w · x + b
- ŷ
- the predicted price; the hat distinguishes it from the actual price y, which we then compare it with
- w
- slope — how many millions each extra square metre adds; without it the price would not depend on the area at all
- x
- floor area in m², the only feature; the only thing the model knows about the flat
- b
- intercept — “the price of a zero-sized flat”; without it the line would have to pass through the origin, and flat prices do not head towards zero
3. Training: tuning the parameters
Definition — loss function and training
Let {fθ : θ ∈ Θ}, Θ ⊆ ℝp, be a model and Dtr = ((x1, y1), …, (xn, yn)), n ≥ 1, the training data. A loss function is a function
J : Θ → [0, ∞),
that assigns to each choice of parameters θ a number J(θ) saying how wrong the model fθ is on Dtr — here the mean of the squared errors (MSE), written out in full in the breakdown below. Training is the search for parameters with the smallest possible loss, ideally θ* ∈ argminθ ∈ Θ J(θ).
If J is differentiable and Θ = ℝp, denote by ∇J(θ) = (∂J/∂θ1, …, ∂J/∂θp) its gradient. Gradient descent is the algorithm:
- Input: initial parameters θ0 ∈ ℝp, learning rate η > 0, number of steps T ∈ ℕ.
- Output: parameters θT ∈ ℝp.
- For t = 0, 1, …, T − 1 set θt+1 = θt − η · ∇J(θt).
- After T steps, stop and return θT.
So in every step the descent computes in which direction the loss grows fastest and moves the parameters a small step the opposite way. An epoch is one pass through all the training data; in our descent, where every step computes the gradient from all n examples, one epoch is exactly one step.
Fog on a hill and the goal of getting down into the valley. You only see the slope under your feet, so you take a step downhill, look again, take another step. Each step is one adjustment of the knobs; the valley is the knob setting with which the model is least wrong.
Our loss function, the mean squared error (MSE), written out in full:
J(w, b) = 1/n ∑i=1n ( w xi + b − yi )2
- J(w, b)
- the loss as a function of the parameters — the data are fixed, we may only change w and b, which is why only they are in the brackets
- 1/n ∑
- the average over all n = 40 training flats — so that the loss is comparable for datasets of different sizes and no flat takes precedence
- w xi + b − yi
- the error on the i-th flat: the prediction ŷi minus the actual price; positive when the model overshoots, negative when it undershoots
- 2
- the square — so that positive and negative errors do not cancel out, large errors weigh more than small ones, and the loss can be differentiated smoothly
Theorem — the gradient of MSE for a straight line
Let n ≥ 1, let (x1, y1), …, (xn, yn) ∈ ℝ2 be the training data and J : ℝ2 → ℝ the MSE of a straight line,
J(w, b) = 1/n ∑i=1n (w xi + b − yi)2.
Denote ŷi = w xi + b for i = 1, …, n. Then for all (w, b) ∈ ℝ2
∂J/∂w = 2/n ∑i=1n (ŷi − yi) · xi, ∂J/∂b = 2/n ∑i=1n (ŷi − yi).
Proof. The derivative of a sum is the sum of the derivatives, and the constant factor 1/n is preserved by differentiation. For a single term, by the chain rule: ∂/∂w (w xi + b − yi)2 = 2 (w xi + b − yi) · xi, because the inside of the bracket has derivative xi with respect to w; with respect to b its derivative is 1. ∎
Every flat “pulls” on the parameters in proportion to how wrong the model was on it. For w the pull is also multiplied by the floor area: an error on a large flat says more about the slope than an error on a studio.
One step of gradient descent then moves both parameters against the gradient:
w ← w − η · ∂J/∂w
- w
- the parameter before and after the step — the arrow ← means “overwrite with the new value”, not equality
- −
- we go against the gradient, i.e. downhill: we want to make the loss smaller, not larger
- η
- learning rate — how long a step we take; the gradient only holds near the point, so the step must be small (see the next section)
- ∂J/∂w
- the gradient — in which direction and how steeply the loss grows when we increase w
The whole training as an algorithm (exactly how the figure below computes it):
w, b = 0, 0 # initial parameters
for epocha in range(200): # hyperparameter: number of epochs
gw = gb = 0
for plocha, cena in trenovaci_data: # one epoch = all the data
x = plocha / 100 # area in hundreds of m²
chyba = (w * x + b) - cena # ŷ − y
gw += 2 * chyba * x / n # ∂J/∂w
gb += 2 * chyba / n # ∂J/∂b
w, b = w - eta * gw, b - eta * gb # step downhill, eta = learning rate
We plug in the area in hundreds of m² (52 m² → 0.52). In metres, the gradient with respect to w would come out dozens of times larger than with respect to b, and a single learning rate would not suit both parameters at once. Such feature scaling is common in practice. That is why w below is in millions per 100 m².
Example — the first step of descent
At the point w = b = 0 the model predicts 0 everywhere, so the error on every flat is −yi. The gradient is therefore
∂J/∂w = −2 · 1/n ∑i=1n xi yi = −2 · 5.678 = −11.36, ∂J/∂b = −2 · ȳ = −2 · 6.88 = −13.76,
where 5.678 is the mean of the products of area (in hundreds of m²) and price, and ȳ = 6.88 is the mean price in millions. With learning rate η = 0.5 (the colours match the terms of the step formula above):
w ← 0 − 0.5 · (−11.36) = 5.68, b ← 0 − 0.5 · (−13.76) = 6.88.
(w is in millions per 100 m² here.) The loss drops from 54.2 to 17.7. Both gradients were negative (the model undershot everywhere), so both parameters increased.
We start with w = 0 and b = 0 (the model says “everything costs nothing”, loss 54.2) and run 200 epochs of gradient descent. The result: w = 0.0910 million per m², that is roughly CZK 91,000 for every extra metre, and b = 0.35 million. The exact least-squares solution (for a straight line it can be computed with a formula, see the part on linear regression) differs only in the fourth decimal place. The descent got where it was supposed to.
4. Hyperparameters: what we set ourselves
Definition — hyperparameters
Training is a procedure that computes the parameters θ* from the training data Dtr and from a setting h (e.g. gradient descent above). Hyperparameters are the components of the setting h: quantities that affect the shape of the model or the course of learning, are not part of θ, are not changed by training and are chosen by us in advance. They include:
- the shape and size of the model (the shape of the function fθ and the set Θ);
- the learning rate η > 0 — how big a step of gradient descent is;
- the number of epochs;
- the batch size B ∈ {1, …, n} — how many training examples are used to compute the gradient in one step.
Hyperparameters are tuned by the results on the validation data Dval, never on the test data.
Parameters are what the pupil learns. Hyperparameters are how we teach them: how fast, how long, how many examples at once.
With a rate of 0.05 the model learns slowly — after 200 epochs its loss is 0.48 and it is still not there. With 0.5 it reaches 0.165 within a few dozen epochs. With 1.6 the steps are so large that the model overshoots the valley further and further and the loss explodes — after five epochs it is 46,310,590. Choosing the right learning rate is one of the most common kinds of tuning in all of machine learning.
For a straight line and MSE, one can compute exactly where the boundary between “slow” and “explodes” lies. The loss is a quadratic function of the parameters, so its curvature is the same everywhere and is described by two numbers — the curvature in the steep and in the flat direction of the valley (the eigenvalues λ of the Hessian matrix, the table of second derivatives).
et+1 = (1 − η λ) · et
- et+1
- the distance of the parameters from the best values after the next epoch (in one of the two directions) — this is what we want to bring to zero
- 1
- without a step the distance would stay as it was; the step takes away a share η λ of it. If |1 − η λ| < 1, the distance shrinks; if it is greater than 1, it grows as a geometric sequence
- η
- learning rate — the only thing here we choose
- λ
- the curvature of the loss in the given direction — given by the data, not by us; in the steep direction the gradient is large, and so the step is long
- et
- the distance before the epoch; the formula repeats, so after t epochs et = (1 − η λ)t e0
Theorem — when gradient descent converges
Let p ≥ 1, let H ∈ ℝp×p be a symmetric matrix with eigenvalues λ1, …, λp, where λmin = minj λj > 0 and λmax = maxj λj, let θ* ∈ ℝp, c ∈ ℝ, and let the loss be the quadratic function
J(θ) = c + 1/2 (θ − θ*)T H (θ − θ*), θ ∈ ℝp,
with Hessian matrix H and minimum at θ*. Let η ∈ ℝ and θt+1 = θt − η ∇J(θt) for t = 0, 1, 2, … Then θt → θ* for every starting point θ0 ∈ ℝp if and only if
0 < η < 2/λmax.
Proof. We have ∇J(θ) = H(θ − θ*), so the deviation et = θt − θ* satisfies et+1 = (I − ηH) et. In an orthonormal basis of eigenvectors of H, the j-th component et(j) evolves as et+1(j) = (1 − η λj) et(j), so et(j) = (1 − η λj)t e0(j). All components go to zero for every e0 if and only if |1 − η λj| < 1 for all j, i.e. 0 < η λj < 2; since λj > 0, the strictest condition is the one for λmax. ∎
Example — the limit for our flats
For the MSE of a straight line, H = 2/n · [[∑xi2, ∑xi], [∑xi, n]] = [[1.193, 1.435], [1.435, 2]] (area in hundreds of m²). The eigenvalues come out as λmax = 3.087 and λmin = 0.106, so the descent converges for η < 2/3.087 = 0.648. The colours match the terms of the formula et+1 = (1 − η λ) · et above.
η = 0.05: the multiplier in the flat direction is 1 − 0.05 · 0.106 =0.9947; after 200 epochs what remains is 0.9947200 ≈ 35% of the original deviation — which is why the model is not there yet.
η = 0.5: the multipliers are |1 − 0.5 · 3.087| = 0.544 and 1 − 0.5 · 0.106 = 0.947; after 200 epochs what remains is 0.947200 ≈ 2 · 10−5 of the deviation — practically nothing.
η = 1.6 > 0.648: in the steep direction |1 − 1.6 · 3.087| = 3.94. The deviation roughly quadruples every epoch, and the loss, which grows with its square, 15.5-fold — in the chart from 801 to 12,395.
The valley is a narrow trough: steep across, almost flat along. The learning rate must be small enough for the steep walls, otherwise you jump onto the opposite slope higher than you were. But then you creep along the trough ever so slowly. This conflict between the steep and the flat direction is the reason why large models use smarter variants of descent.
5. How good the model is
Let D′ be any part of the data with m ≥ 1 examples (x, y) and ŷ(x) = fθ*(x) the prediction of the trained model. We measure the error on D′ by the root mean squared error (RMSE):
RMSE = √1/m ∑(x, y) ∈ D′ (ŷ(x) − y)2
- √
- the square root brings the result from (million CZK)² back to millions of crowns — the number can be read as the “typical error”
- 1/m ∑D′
- the average over the chosen part — training, validation or test; each answers a different question
- (ŷ(x) − y)2
- the squared error on one flat, just as in the loss — we measure with what we minimised
The average error (the root of the MSE) is 0.41 million on the training data, 0.60 on the validation data and 0.51 million on the test data, i.e. roughly half a million crowns. With a single feature it cannot get better: the noise we added to the prices (location, condition) is exactly that large and cannot be guessed from the area. Anyone who wants a more accurate estimate needs more features — not a smarter line. If, on the other hand, we made the model too flexible, it would memorise the noise in the training data (overfitting) and be more wrong on new flats.
6. Inference: the model in operation
Definition — inference
Let {fθ : θ ∈ Θ} be a model and θ* ∈ Θ the parameters found by training. Inference is the computation of the prediction
ŷ = fθ*(x)
for a new input x ∈ X, with the parameters θ* kept fixed. The model does not learn during inference (unless we explicitly train it again). For language models every answer is inference — the model generates token by token and makes one pass through the network for each token.
Training is studying, inference is the exam — or the job. You study once and for a long time, then you work daily and quickly. When you chat with a chatbot, you are not teaching it (that happens at another time and in another way), you are just using it.
For a flat of 65 m² our model estimates 6.27 million. That costs 3 arithmetic operations. Training cost roughly 72,000 operations. In large models the ratio is similar, just on a different scale: training takes months on thousands of GPUs, a single answer takes seconds. But there are billions of answers, so for the most widely used models inference can in total cost more computation than their training — which is why so much effort goes into making models smaller and faster.
Example — inference and the number of operations
We plug in the learned parameters w = 9.0957 million per 100 m² (i.e. 0.0910 per m²) and b = 0.3538; the area in hundreds of m², x = 0.65:
ŷ = 9.0957 · 0.65 + 0.3538 = 5.9122 + 0.3538 = 6.2660 ≈ 6.27 million
(the colours match the terms of the formula ŷ = w · x + b in the section on the model).
That is 3 operations: a unit conversion (division by a hundred), a multiplication and an addition. One epoch of training makes, for each of the 40 flats, a prediction, an error and both contributions to the gradient — we count roughly 9 operations per flat (the exact count depends on what we consider a single operation), so 40 · 9 · 200 = 72,000 operations for the whole training. The ratio of training to a single prediction is 24,000 : 1.
7. Glossary
The terms from this part and where in the series you can learn more about them:
- data
- examples the model learns from — here 60 flats with floor area and price — in detail
- feature
- an input value describing an example (floor area); an image has thousands of features — its pixels — in detail
- label
- the correct answer we want to predict (the price) — in detail
- training, validation and test data
- the model learns on the first, the settings are tuned on the second, the third is used once at the end — in detail
- model
- a function with adjustable parameters that computes an output from an input — in detail
- parameter (weight)
- a number inside the model that is learned; language models have billions of them — in detail
- loss function
- a number that measures how wrong the model is; learning makes it smaller — in detail
- training (learning)
- searching for parameters that make the loss on the training data small — in detail
- gradient
- the direction in which the loss grows fastest; training goes the opposite way — in detail
- epoch
- one pass through all the training data — in detail
- batch
- how many examples are used for one learning step — in detail
- hyperparameter
- a setting we choose ourselves and the model does not learn (learning rate, number of epochs, model size) — in detail
- overfitting
- the model memorises the training data and fails on new data — in detail
- inference
- using a trained model on a new input — what happens when you ask a chatbot — in detail
- token
- a piece of text that a language model works with — in detail
- embedding
- a vector of numbers that represents a word, an image or another object — in detail
- context and prompt
- all the text the model receives during inference — in detail
8. Summary
- Data = examples with features and labels, split into a training, a validation and a test part.
- Model = a function with parameters; training searches for the parameters with the smallest loss, typically by gradient descent.
- Our model learned CZK 91,000 per m² — the same as the exact calculation.
- We choose the hyperparameters: with a small learning rate training is slow, with a large one it falls apart.
- The test error (0.51 million) matches the noise in the data; a better model needs more features.
- Inference = using a trained model; it is cheap per query, but there is an enormous number of queries.
Want to get hands-on? Ask an AI agent to download real data on flat prices in your city, split them into three parts and train the same line — and then have it add a second feature (say, the distance from the centre) and compare the test error.
Do you see it differently? Write to support@coden-app.com.