Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 1 · BEGINNERModule 0 · Reading maths

Functions and arrows: f: ℝⁿ → ℝ, ↦ and f ∘ g

One arrow gives a function's type, another says what it does to an input, and a small circle chains functions together, which is what a neural network is.

10 min Prerequisites: Lesson 4 (ℝ and shapes)

1. The idea

f: ℝⁿ → ℝ is a function's type signature: f takes a list of n numbers and gives back one number. A loss function has exactly this type: it takes all of a model's parameters and returns one number to make small. The arrow → is about what kind of thing goes in and comes out; it says nothing about what f does.

The other arrow, ↦ (“maps to”), says what happens to a single input. x ↦ x² is the function that squares its input, the same thing as Python's lambda x: x**2.

f ∘ g, read “f after g”, means: apply g first, then apply f to the result, so (f ∘ g)(x) = f(g(x)). Read it right to left. A neural network is exactly this: layer 1, then layer 2, then layer 3, written f₃ ∘ f₂ ∘ f₁. Order matters: g(f(x)) is usually a different function, as the widget shows.

2. Try it: chain two functions

g(x) = 2x − 1 is a tiny linear layer and f(x) = max(0, x) is ReLU. Feed in x and follow it through both, then swap the order and watch the graph change.

A pipeline: x goes into one function, its output into the other, and the final value is shown. A toggle switches between f after g and g after f, where g(x) = 2x − 1 and f(x) = max(0, x). A graph shows the whole composed function: a bent line that is flat at 0 then rises (f after g), or a line that is flat at −1 then rises (g after f).

3. The math

A type signature
f:Rn→Rf : \mathbb{R}^{n} \to \mathbb{R}
n numbers in, one number out.
A rule
g:x↦2x−1g : x \mapsto 2x - 1
What g does to one input x.
Composition
(f∘g)(x)=f(g(x))(f \circ g)(x) = f(g(x))
g first, then f. Read right to left.
A network is a composition
y^=(f3∘f2∘f1)(x)=f3(f2(f1(x)))\hat{y} = (f_3 \circ f_2 \circ f_1)(\mathbf{x}) = f_3(f_2(f_1(\mathbf{x})))
Each layer is a function; the network applies them in turn.
Order matters
f(g(0))=max⁡(0,−1)=0g(f(0))=2⋅0−1=−1f(g(0)) = \max(0, -1) = 0 \qquad g(f(0)) = 2 \cdot 0 - 1 = -1
Same two functions, opposite order, different answer.

4. The code (python)

mm0_functions.py
def g(x):                     # g: R -> R, x |-> 2x - 1 (a tiny linear layer)
    return 2 * x - 1

def f(x):                     # f: R -> R, x |-> max(0, x) (ReLU)
    return max(0.0, x)

def compose(outer, inner):    # outer after inner
    return lambda x: outer(inner(x))

f_after_g = compose(f, g)
g_after_f = compose(g, f)
print(f_after_g(0.0), g_after_f(0.0))               # 0.0 -1.0
print([f_after_g(x) for x in (-1.0, 0.5, 2.0)])     # [0.0, 0.0, 3.0]

5. You’ll see this in

6. Check yourself

Answer all 4 questions correctly to complete the chapter · 0 / 4 done
Q1/4 A function has type f: ℝ³ → ℝ. What does it take, and what does it return?
Q2/4 What does (f ∘ g)(x) mean?
Q3/4 With g(x) = 2x − 1 and f(x) = max(0, x), what is (g ∘ f)(−2)?
Q4/4 Which Python construct is closest to x ↦ x²?

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.