Skip to content
coden

Functions and mappings: why every AI model is a function

Domain, image, injectivity, composition, inverses, functions of several variables and convexity — with graphs of the sigmoid, ReLU and a loss landscape.

13 minutes Matěj Žalmánek 2026-10-15

Series AI basics Part 6 of 52
  1. Map of artificial intelligence: what is what and how it all fits together
  2. A brief history of artificial intelligence: from Turing to language models
  3. Data, model, training, inference: the basic terms on a single example
  4. How to read mathematical notation: symbols, indices, sums and quantifiers
  5. Sets and relations: the building blocks of definitions in AI
  6. Functions and mappings: why every AI model is a function
  7. Logic and proofs: the rules AI reasons by
  8. Graphs and trees: the structure that both search and neural networks stand on
  9. Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
  10. Derivatives, the gradient and the chain rule: how a model finds out which way to learn
  11. Gradient descent: how a model learns
  12. What probability is: from rolling dice to the Kolmogorov axioms
  13. Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
  14. Random variables and the cumulative distribution function: one function that tells you everything about a distribution
  15. Probability density and probability mass functions: why a continuous variable has zero probability at a point
  16. Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
  17. Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
  18. Law of large numbers and central limit theorem: why averages behave so predictably
  19. Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
  20. Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
  21. State-space search: BFS, DFS, UCS and A* with animations
  22. Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
  23. Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
  24. Logic and knowledge systems: forward and backward chaining, resolution and expert systems
  25. Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
  26. Local search and simulated annealing: a travelling salesman through the Czech regional capitals
  27. Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
  28. Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
  29. Ant colony optimization: how a pheromone trail finds a short path
  30. Particle swarm optimisation: inertia, personal experience and the swarm’s experience
  31. Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
  32. Fuzzy sets and membership functions: how to write down “somewhat warm”
  33. Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
  34. Fuzzy logic: t-norms, implications and IF–THEN rules
  35. Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
  36. Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
  37. Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
  38. Markov chains: the transition matrix, the stationary distribution and PageRank
  39. Hidden Markov models: the Viterbi algorithm and the dishonest casino
  40. Bayesian networks: why the grass is wet and what it means for the sprinkler
  41. The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
  42. Types of machine learning: supervised, unsupervised, semi-supervised and more
  43. Prompts, context and RAG: how to give a model the right information
  44. Variational autoencoders (VAE): new images from a continuous latent space
  45. Generative adversarial networks (GAN): the forger versus the detective
  46. Diffusion models: how pure noise becomes an image
  47. Markov decision processes: states, actions, rewards and the Bellman equations
  48. Q-learning and DQN: learning the value of actions, from a table to a neural network
  49. Policy gradient and PPO: learning the policy directly
  50. How an AI agent works: a language model in a loop with tools
  51. Programming with AI agents: the task, tests and review
  52. Evaluating and securing AI systems: how to measure and how to keep watch

Full series syllabus 52 of 83 topics published

What you will take away
  • What a function, its domain and its image are — and which functions you will meet most often in AI.
  • What injective, surjective and bijective mean, why a neural network is a composition of functions and when an inverse exists.
  • Why a loss function is a landscape and why convexity matters when searching for its bottom.

Useful to know first: How to read mathematical notation: symbols, indices, sums and quantifiers, Sets and relations: the building blocks of definitions in AI

Every model in artificial intelligence is a function. A spam classifier takes an e-mail and returns “spam” or “not spam”, a language model takes text and returns probabilities of the next word, and a neural network is a function composed of thousands of smaller functions. Learning is then nothing more than searching for the right function.

The previous part ended by defining a function as a special relation. Here we pick up from there: what functions look like, what properties they have, how they are composed and why it matters in AI.

1. What a function is

Definition — function (mapping)

Let X and Y be sets and X × Y = {(x, y) : x ∈ X, y ∈ Y} their Cartesian product (see the part Sets and relations). A function (mapping) from X to Y is a relation f ⊆ X × Y such that

∀x ∈ X ∃! y ∈ Y : (x, y) ∈ f

(∃! is read “there exists exactly one”). This unique y is denoted f(x), and we write f : X → Y, x ↦ f(x). So a function assigns to every element x ∈ X exactly one element f(x) ∈ Y.

For a function f : X → Y we introduce:

  1. the domain is the set X;
  2. the codomain (target set) is the set Y;
  3. the image (range) of the function is the set of values f actually takes: f(X) = {y ∈ Y : ∃x ∈ X : f(x) = y} = {f(x) : x ∈ X} ⊆ Y;
  4. the graph of the function is the set of pairs {(x, f(x)) : x ∈ X} ⊆ X × Y.

Two functions f, g : X → Y are equal if and only if f(x) = g(x) for all x ∈ X.

In plain words

A function is a vending machine: you put in an input and exactly one output drops out — the same one every time. The domain says what you are allowed to put into the machine, the codomain what can come out of it. The image is what actually comes out.

The difference between codomain and image shows on the sigmoid σ : ℝ → ℝ: it may return any real number, but it actually returns only numbers between 0 and 1. That is why it can be read as a probability.

Example — codomain and image of the function x2

f : ℝ → ℝ, f(x) = x2. The codomain is ℝ, but the image is only f(ℝ) = [0, ∞).

Why: x2 ≥ 0 for every real x, so negative numbers never appear. And every y ≥ 0 does appear: f(√y) = y, for example f(3) = 9.

2. Functions you will meet in AI

A function of one variable is best understood from its graph. These keep showing up in artificial intelligence over and over — as models, as activation functions of neural networks or as loss functions:

x (unitless) f(x) (unitless) linear: f(x) = ax + b
x (unitless) f(x) (unitless) quadratic: x2
x (unitless) f(x) (unitless) exponential: ex
x (unitless) f(x) (unitless) logarithm: ln x
x (unitless) f(x) (unitless) sigmoid: 1/1 + e−x
x (unitless) f(x) (unitless) hyperbolic tangent: tanh x
x (unitless) f(x) (unitless) ReLU: max(0, x)
The functions of one variable you will meet most often
  • A linear function is the simplest model — linear regression is nothing else.
  • A quadratic function is the basis of the mean squared error: the penalty grows with the square of the deviation.
  • The exponential and the logarithm are inverse to each other; the logarithm turns multiplication into addition, which is why you will find it in every probability-based loss.
  • Sigmoid and tanh “squash” any number into the interval (0, 1) and (−1, 1), respectively.
  • ReLU lets positive numbers through and sets negative ones to zero. It is so simple that for a long time nobody believed it would work — today it is the most widely used activation function.
Definition — sigmoid, hyperbolic tangent and ReLU

Let e ≈ 2.718 be Euler’s number (the base of the natural logarithm). All three functions are defined for every x ∈ ℝ.

  1. The sigmoid (logistic function) is the function σ : ℝ → ℝ, σ(x) = 1/1 + e−x.
  2. The hyperbolic tangent is the function tanh : ℝ → ℝ, tanh x = ex − e−x/ex + e−x.
  3. ReLU (rectified linear unit) is the function ReLU : ℝ → ℝ, ReLU(x) = max(0, x) = x for x > 0,   ReLU(x) = 0 for x ≤ 0.

The denominators in 1 and 2 are always positive (et > 0 for every t ∈ ℝ), so there is never division by zero. The image of the sigmoid is the interval (0, 1), the image of tanh the interval (−1, 1) and the image of ReLU the interval [0, ∞). The hyperbolic tangent is just a shifted and stretched sigmoid: tanh x = 2σ(2x) − 1 for all x ∈ ℝ.

σ(x) = 1/1 + e−x

σ(x)
a result between 0 and 1 — we want to turn any score into something that can be read as a probability
e−x
always positive; for large x it approaches 0, for very negative x it grows without bound — this is the “lever” that moves the whole result
1 +
so the denominator is always greater than 1: the result never exceeds 1 and there is never division by zero
1
the one in the numerator and the fraction bar turn things around: a large denominator (very negative x) gives almost 0, a denominator close to 1 (large x) gives almost 1
The sigmoid is increasing and squashes the whole real line into the interval (0, 1).
Example — the sigmoid at a few points

The colours match the terms of the formula above.

σ(0) = 1/1 + e0 = 1/1 + 1 = 0.5 — a score of 0 means “I don’t know”.

σ(2) = 1/1 + e−2 ≈ 1/1 + 0.1353 ≈ 0.881

σ(−2) = 1/1 + e2 ≈ 1/1 + 7.389 ≈ 0.119

Notice that σ(2) + σ(−2) = 1. This holds in general, σ(−x) = 1 − σ(x): the opposite score gives the opposite probability.

And tanh 1 = 2σ(2) − 1 ≈ 2 · 0.8808 − 1 ≈ 0.762.

In AI the quadratic function shows up most often in the mean squared error (MSE). The model estimates n numbers ŷ1, …, ŷn, and the correct results were y1, …, yn:

MSE = 1/n ∑i=1n (yi − ŷi)2

yi − ŷi
the deviation on the i-th example — by how much the model was wrong and in which direction
2
the square makes every deviation a positive number (errors up and down do not cancel out) and penalizes large errors more than proportionally
∑i=1n
adds up the penalties over all examples — we judge the model on the whole data, not on one point
1/n
turns the total into an average, so that the error on 10 and on 10,000 examples can be compared
Example — the MSE of three estimates

Correct: y = (3, 5, 2), model: ŷ = (2.5, 5, 4).

The colours match the terms of the formula above.

Deviations: 3 − 2.5 = 0.5; 5 − 5 = 0; 2 − 4 = −2.

Squares: 0.25; 0; 4. Sum 4.25.

MSE = 1/3 · 4.25 = 4.25/3 ≈ 1.42. The single large error (−2) makes up 4 of the 4.25 — exactly the “penalizes large errors more”.

Theorem — the logarithm turns a product into a sum

Theorem. Let ln : (0, ∞) → ℝ be the natural logarithm, i.e. the inverse function of x ↦ ex (see the section on inverse functions below), and let a, b ∈ ℝ, a > 0, b > 0. Then

ln(a · b) = ln a + ln b.

Proof. By the rule for powers es + t = es · et we have eln a + ln b = eln a · eln b = a · b. Taking the logarithm of both sides, since ln is the inverse of ex, gives ln a + ln b = ln(ab). ∎

Example. A model gave three correct answers the probabilities 0.9, 0.8 and 0.5. The probability of all three (assuming independence) is 0.9 · 0.8 · 0.5 = 0.36. In logarithms: ln 0.9 + ln 0.8 + ln 0.5 ≈ −0.105 + (−0.223) + (−0.693) ≈ −1.02 = ln 0.36. With a million examples the product would underflow to zero, while the sum of logarithms stays a reasonable number — that is why losses work with logarithms.

3. Injective, surjective and bijective

Definition — one-to-one, onto and bijective function

Let X, Y be sets and f : X → Y a function.

  1. f is injective (one-to-one) if and only if ∀x1, x2 ∈ X : f(x1) = f(x2) ⇒ x1 = x2. Equivalently: x1 ≠ x2 ⇒ f(x1) ≠ f(x2) — different inputs give different outputs.
  2. f is surjective (onto) if and only if ∀y ∈ Y ∃x ∈ X : f(x) = y, i.e. if and only if f(X) = Y — the function hits every element of Y.
  3. f is bijective (a one-to-one correspondence) if and only if it is both injective and surjective, i.e. if and only if ∀y ∈ Y ∃! x ∈ X : f(x) = y.
In plain words

Injective: no two inputs share an output — from the output you can tell where it came from. Surjective: nothing in the target set is left out. Bijective: every input corresponds to exactly one output and vice versa — like a cloakroom where every coat has its own ticket.

AB 123 abcd injective injective: yes surjective: no
AB 1234 abc surjective injective: no surjective: yes
AB 123 abc bijective injective: yes surjective: yes
AB 123 abc neither injective: no surjective: no
Four mappings between small sets

In AI almost every model is non-injective: a classifier sends thousands of different cat photos into a single class “cat”. The information about which photo it was is lost in the process — and that is fine, it is exactly what we want from a classifier. Compression models (autoencoders), on the other hand, try to be “almost injective”, so that the input can be reconstructed again from its compressed form.

Example — how a property is verified or refuted

f(x) = x2 on ℝ is not injective: one counterexample is enough, f(−2) = f(2) = 4, yet −2 ≠ 2.

On [0, ∞) it is injective: x12 = x22 implies (x1 − x2)(x1 + x2) = 0, and since for non-negative numbers x1 + x2 = 0 only when x1 = x2 = 0, we always get x1 = x2.

f(x) = 2x + 1 on ℝ is bijective: for any y we find the unique x = y − 1/2.

Theorem — how many functions, injections and bijections there are

Theorem. Let X, Y be finite sets, |X| = m and |Y| = n, where |M| denotes the number of elements of a set M and m, n ∈ ℕ, m, n ≥ 1. Then

  1. there are nm functions f : X → Y in total;
  2. there are n · (n − 1) · … · (n − m + 1) injective functions f : X → Y for m ≤ n, and 0 for m > n;
  3. if m = n, a function f : X → Y is injective if and only if it is bijective, and there are n! = n · (n − 1) · … · 1 bijections.

Proof. 1. Each of the m inputs independently chooses one of the n outputs. 2. The first input has n options, the second only n − 1 (it must not hit an occupied output), and so on; the m-th input has n − m + 1 options. For m > n the (n + 1)-th input would have no free option left — more pigeons than pigeonholes. 3. An injective function hits m = n different elements of the n-element set Y, i.e. all of them, and so it is surjective too; substituting m = n into 2 gives n!. ∎

Checked by brute force (by going through all functions):

|X| → |Y|allinjectivesurjectivebijective
3 → 333 = 273 · 2 · 1 = 666
3 → 443 = 644 · 3 · 2 = 2400
4 → 334 = 810360

There are 36 surjections 4 → 3: we choose which two of the four inputs share an output (6 options), and then distribute the three outputs among the three groups (3! = 6 options); 6 · 6 = 36.

4. Composing functions

Definition — composite function

Let X, Y, Z be sets and f : X → Y, g : Y → Z functions (the codomain of f is the domain of g). The composite function (“g after f”) is the function

g ∘ f : X → Z,   (g ∘ f)(x) = g(f(x)) for all x ∈ X.

So f is applied first, then g. Properties:

  1. associativity: if moreover W is a set and h : Z → W a function, then h ∘ (g ∘ f) = (h ∘ g) ∘ f, because both sides send every x ∈ X to h(g(f(x)));
  2. in general not commutative: even if X = Y = Z and both compositions exist, g ∘ f ≠ f ∘ g is possible (example below).
In plain words

Composition is an assembly line: the output of one machine goes straight into the next. The order matters — getting dressed and then washing turns out differently than the other way round.

Take f(x) = 2x + 1 and g(x) = x2:

(g ∘ f)(3) = g(f(3)) = g(7) = 49

(f ∘ g)(3) = f(g(3)) = f(9) = 19

This is exactly how a neural network is built. Every layer is a function — a linear step followed by an activation function — and the whole network is their composition: f = f3 ∘ f2 ∘ f1. A “deep” network is simply a long composition. And when we learn to differentiate in later parts, the chain rule will tell us how to differentiate precisely composite functions.

h = φ(W x + b)

h
the output of the layer (a vector) — it will go as input into the next layer; this is how the composition arises
x
the input of the layer: the data, or the output of the previous layer
W
the weight matrix: every output is a weighted sum of all inputs and the weights decide which ones matter — these are what the network learns
b
the bias: without it a zero input would have to give a zero sum; the bias lets us set the threshold at which the neuron “switches on”
φ
the activation function, applied component-wise (ReLU, sigmoid, tanh) — the only non-linear part; without it the whole network would collapse into a single linear function
A linear step Wx + b, then the non-linearity φ; a network is this pair repeated and composed.
Example — one layer with two neurons

W = ( 1 −1 ; 0.5 2 ) (two rows, one per neuron), b = (−2, −1), x = (2, 1), φ = ReLU (colours as in the formula above).

Neuron 1: 1 · 2 + (−1) · 1 + (−2) = −1, ReLU(−1) = 0 — the neuron stays silent.

Neuron 2: 0.5 · 2 + 2 · 1 + (−1) = 2, ReLU(2) = 2.

The output of the layer h = (0, 2) goes as input into the next layer.

def sit(x, vrstvy):                     # vrstvy = [(W1, b1), (W2, b2), ...]
    h = x
    for W, b in vrstvy:                  # f = f_k ∘ ... ∘ f_2 ∘ f_1
        h = relu(W @ h + b)              # the output of one layer is the input of the next
    return h
Theorem — a composition of linear functions is a linear function

Theorem. Let a, b, c, d ∈ ℝ and let f, g : ℝ → ℝ be the functions f(x) = ax + b and g(x) = cx + d. Then for all x ∈ ℝ

(g ∘ f)(x) = (ca)x + (cb + d),

i.e. g ∘ f is again a function of the form x ↦ a′x + b′ with a′ = ca and b′ = cb + d.

Proof. g(f(x)) = c(ax + b) + d = cax + cb + d. ∎

The same holds for vectors and matrices: let W1 ∈ ℝk×n, b1 ∈ ℝk, W2 ∈ ℝm×k and b2 ∈ ℝm. Then for every x ∈ ℝn, W2(W1x + b1) + b2 = (W2W1)x + (W2b1 + b2), where W2W1 ∈ ℝm×n.

Example. f(x) = 2x + 1, g(x) = 3x − 4 ⇒ (g ∘ f)(x) = 6x − 1. Check at the point 3: g(f(3)) = g(7) = 17 and 6 · 3 − 1 = 17.

That is why networks need activation functions: ten linear layers without φ can do exactly the same as one.

5. Inverse function

Definition — inverse function

Let X, Y be sets and f : X → Y a function. A function g : Y → X is the inverse of f if and only if both conditions hold:

  1. ∀x ∈ X : g(f(x)) = x   (i.e. g ∘ f = idX),
  2. ∀y ∈ Y : f(g(y)) = y   (i.e. f ∘ g = idY),

where idX : X → X, x ↦ x is the identity on X. The inverse function is denoted f−1. In words: g takes every output of f back to the input it came from, and vice versa.

Theorem. Let f : X → Y be a function. The inverse of f exists if and only if f is bijective; if it exists, it is unique, and for every y ∈ Y, f−1(y) is the unique x ∈ X for which f(x) = y.

In plain words

An inverse function takes a step back: from the output it works out the input. That only works when no two inputs lead to the same output (otherwise it would be unclear where to go back to) and when every output really leads somewhere.

The exponential and the logarithm are a pair of inverse functions: ln(ex) = x. For the sigmoid, understood as a bijection σ : ℝ → (0, 1), the inverse function is the logit : (0, 1) → ℝ, logit(p) = ln p/1 − p — it turns a probability back into an arbitrary real number. That is why the outputs of a network before the sigmoid or softmax are called “logits”.

Example — deriving the logit step by step

We find the inverse by solving the equation p = σ(x) for x (for p ∈ (0, 1)):

p = 1/1 + e−x

1 + e−x = 1/p  (taking the reciprocal of both sides)

e−x = 1/p − 1 = 1 − p/p

−x = ln 1 − p/p  (ln is the inverse of ex)

x = −ln 1 − p/p = ln p/1 − p = logit(p)

In numbers: logit(0.8) = ln 0.8/0.2 = ln 4 ≈ 1.386. And back (colours as in the breakdown of the sigmoid above): σ(ln 4) = 1/1 + 1/4 = 0.8, because e−ln 4 = 1/4. The ratio p/1 − p = 4 is the odds (“4 to 1”); the logit is their logarithm.

6. Functions of several variables

Definition — function of several variables

Let n, m ∈ ℕ, n, m ≥ 1, and let ℝn = ℝ × … × ℝ (n times) be the set of all n-tuples of real numbers (vectors) x = (x1, …, xn).

  1. A (real) function of n variables is a function f : ℝn → ℝ,   (x1, …, xn) ↦ f(x1, …, xn), i.e. it assigns exactly one real number to every vector.
  2. A vector-valued function is a function f : ℝn → ℝm,   x ↦ (f1(x), …, fm(x)), where fi : ℝn → ℝ for i = 1, …, m are its components; it assigns exactly one vector to every vector.
In plain words

Most real functions take several inputs at once: the price of a flat depends on its floor area, location and age. A layer of a neural network takes a whole vector and returns another vector.

A function of two variables can be drawn as a map: we colour every point (x, y) according to the value f(x, y). Here is Himmelblau's function, a classic test for optimization algorithms. The dark areas are valleys with a small value, and the white dots are the four minima where the function equals zero:

x (unitless) y (unitless)
A function of two variables as a landscape: four equally deep valleys
Example — Himmelblau’s function and why its minima are at zero

f(x, y) = (x2 + y − 11)2 + (x + y2 − 7)2.

Both brackets are squared, so f ≥ 0 everywhere. Zero comes out exactly where both brackets are zero at the same time — where the system x2 + y = 11 and x + y2 = 7 holds. It has four solutions, hence four valleys.

At the point (3, 2): (9 + 2 − 11)2 + (3 + 4 − 7)2 = 0 + 0 = 0 — a minimum.

At the point (0, 0): (−11)2 + (−7)2 = 121 + 49 = 170 — high up on a hill.

This is exactly how to picture a model's loss function: its input is the model's parameters, its output the error. Training is searching for the lowest point in the landscape — except that instead of two dimensions, the landscape of a real model has millions.

7. Monotonicity and convexity

Definition — increasing and convex function

Let I ⊆ ℝ be an interval and f : I → ℝ a function.

  1. f is increasing if and only if ∀x1, x2 ∈ I : x1 < x2 ⇒ f(x1) < f(x2). An increasing function is always injective (x1 ≠ x2 implies x1 < x2 or x2 < x1, and hence f(x1) ≠ f(x2)).
  2. f is convex if and only if ∀x1, x2 ∈ I ∀t ∈ [0, 1] : f(tx1 + (1 − t)x2) ≤ t f(x1) + (1 − t) f(x2). (The point tx1 + (1 − t)x2 lies between x1 and x2, and so in the interval I.)
  3. A point x* ∈ I is a local minimum of f if and only if ∃δ > 0 ∀x ∈ I : |x − x*| < δ ⇒ f(x*) ≤ f(x), and a global minimum if and only if ∀x ∈ I : f(x*) ≤ f(x).

Theorem. Let I ⊆ ℝ be an interval, f : I → ℝ a convex function and x* ∈ I a local minimum of f. Then x* is a global minimum of f.

In plain words

A convex function is shaped like a bowl: the line segment between two points of the graph always lies above the graph. A bowl has only one bottom — whoever keeps walking downhill will find it. With a non-convex function you can end up in a smaller pit and never learn about the deeper one next to it.

f(tx1 + (1 − t)x2) ≤ t f(x1) + (1 − t) f(x2)

tx1 + (1 − t)x2
a point between x1 and x2; the weights t and 1 − t add up to 1, so as t goes from 1 to 0 we pass through the whole segment from x1 to x2
f( … )
the height of the graph at that point
t f(x1) + (1 − t) f(x2)
the height of the line segment (chord) between the points of the graph above x1 and x2 — with the same weights, so it is above the same point
≤
the graph never rises above the chord — that is the bowl shape; if it rose somewhere, there would be a “hump” behind which another valley could hide
Example — the convexity inequality in numbers

The colours match the terms of the inequality above.

f(x) = x2, x1 = −1, x2 = 3, t = 1/2: the point in the middle is 1, left side f(1) = 1, right side 1 + 9/2 = 5. 1 ≤ 5 ✓.

f(x) = x4 − 3x2 + x, for x1, x2 we take both minima −1.30 and 1.13, t = 1/2: the point in the middle is −0.085,

left side f(−0.085) ≈ −0.11, right side −3.51 + (−1.07)/2 = −2.29.

−0.11 > −2.29 — the inequality ≤ does not hold, the function is not convex. The “hump” between the minima is exactly what separates the two valleys.

global x (unitless) f(x) (unitless)

convex: x², a single minimum

global local x (unitless) f(x) (unitless)

non-convex: x⁴ − 3x² + x

Convex and non-convex function

The non-convex function x4 − 3x2 + x has two minima: a global one at x ≈ −1.30 with value ≈ −3.51 and a local one at x ≈ 1.13 with value ≈ −1.07. Whoever starts searching on the right and keeps going down ends up in the worse one. The loss functions of linear and logistic regression are convex — that is why their training reliably finds the best solution. The losses of neural networks are not convex, and how to live with that will be the subject of the part on gradient descent.

We found the minima in the chart above numerically, without derivatives: with a dense grid of candidates, which are then refined by the golden-section search. In every step it discards the part of the interval where the minimum cannot be, and the interval shrinks to ≈ 0.618 of its previous length.

def minima(f, a, b, n=2000):
    krok = (b - a) / n
    vysledek = []
    for i in range(1, n):
        x = a + i * krok
        if f(x) < f(x - krok) and f(x) < f(x + krok):   # candidate: lower than its neighbours
            l, p = x - krok, x + krok
            z = (5 ** 0.5 - 1) / 2                        # ≈ 0.618
            for _ in range(60):                           # golden section
                c, d = p - z * (p - l), l + z * (p - l)
                if f(c) < f(d): p = d                     # the minimum is to the left of d
                else:           l = c                     # the minimum is to the right of c
            vysledek.append((l + p) / 2)
    return vysledek

8. Functions in artificial intelligence

Now the whole of machine learning can be said in the language of functions:

  • A model is a function fθ : X → Y with parameters θ — a whole family of functions, one for every choice of parameters.
  • A loss function L : Y × Y → ℝ says how much the answer ŷ costs when the correct one is y.
  • Learning is picking one function from the family: the argmin over θ of the average loss — a function of the parameters, which we just drew as a landscape.
  • Activation functions (sigmoid, tanh, ReLU) turn linear layers into a non-linear network; without them a composition of linear functions would again be just a linear function.

θ* = argminθ 1/n ∑i=1n L(fθ(xi), yi)

θ*
the learned parameters — they pick one function fθ* out of the whole family
argminθ
we do not want the smallest error itself, but the place where it occurs — parameters, not a number
1/n ∑i=1n
the average over all n training examples — the model should be good on the data as a whole
L
the loss function — turns the difference between the answer and the truth into a single number that can be minimized (for example the square of the deviation, as in MSE)
fθ(xi)
the model’s answer to the i-th input; the only place in the formula where θ appears
yi
the correct answer from the data, which the model’s answer is compared with
Learning = finding the lowest point of a landscape whose coordinates are the parameters and whose height is the average loss.

9. Summary

  • A function assigns exactly one output to every input; the image is where it actually lands.
  • An injective function never mixes up two inputs, a surjective one covers the whole target, a bijective one has an inverse.
  • Composing functions is an assembly line — and a neural network is a composition of layers.
  • A loss function is a landscape over the parameter space; a convex landscape has a single bottom, a non-convex one can have several.

Do you see it differently? Write to support@coden-app.com.

An Agent on your computer, driven from your phone