AI / ML Theory
Chapter 0 · 18 min

0.1 — Vector spaces and inner products, the ML view

Goal: Re-derive ⟨x,y⟩ = xᵀy and convince yourself a linear layer is an affine map.

1. Where the data lives

In ML, the underlying object of any data point is a vector in a finite-dimensional real vector space V=RdV = \mathbb{R}^d. A 28×28 grayscale image becomes a vector with d=784d = 784 components; a sequence of TT tokens in a language model, once embedded, becomes a matrix in RT×dmodel\mathbb{R}^{T\times d_\text{model}}, but each row is a vector in Rdmodel\mathbb{R}^{d_\text{model}}.

The choice of Rd\mathbb{R}^d is not innocent — it commits us to three things at once: additive structure (x+y∈Vx + y \in V), scalar multiplication (λx∈V\lambda x \in V), and the existence of a canonical inner product, which we'll now derive.

2. Inner product via basis expansions

Fix a basis {e1,…,ed}\{e_1, \dots, e_d\} of VV. Every vector has a unique expansion

x=∑i=1dxi ei,xi∈R.x = \sum_{i=1}^{d} x_i \, e_i, \qquad x_i \in \mathbb{R}.

Suppose VV is equipped with a symmetric positive-definite bilinear form ⟨⋅,⋅⟩:V×V→R\langle \cdot, \cdot \rangle : V \times V \to \mathbb{R}. By bilinearity, for y=∑jyjejy = \sum_j y_j e_j,

⟨x,y⟩=⟨∑ixiei, ∑jyjej⟩=∑i,jxi yj ⟨ei,ej⟩.\langle x, y \rangle = \Big\langle \sum_i x_i e_i,\, \sum_j y_j e_j \Big\rangle = \sum_{i,j} x_i\, y_j\, \langle e_i, e_j \rangle.

When the basis is orthonormal, ⟨ei,ej⟩=δij\langle e_i, e_j \rangle = \delta_{ij}, and the double sum collapses:

  ⟨x,y⟩=∑ixiyi=x⊤y.  \boxed{\;\langle x, y \rangle = \sum_i x_i y_i = x^\top y.\;}

This is the form that shows up in every ML derivation: inner product is row-times-column multiplication.

3. Induced norm and angle

The norm induced by the inner product is

∥x∥=⟨x,x⟩=(∑ixi2)1/2.\|x\| = \sqrt{\langle x, x \rangle} = \Big( \sum_i x_i^2 \Big)^{1/2}.

And the angle θ\theta between xx and yy is defined implicitly by

cos⁡θ=⟨x,y⟩∥x∥ ∥y∥.\cos\theta = \frac{\langle x, y \rangle}{\|x\|\, \|y\|}.

The cosine similarity used in semantic search and embedding normalisation is this ratio. When vectors are pre-normalised to unit norm, cos⁡θ=x⊤y\cos\theta = x^\top y — that's it.

4. A linear layer is an affine map

A fully connected layer with weights W∈Rm×dW \in \mathbb{R}^{m\times d} and bias b∈Rmb \in \mathbb{R}^m implements the map

f:Rd→Rm,f(x)=Wx+b.f : \mathbb{R}^d \to \mathbb{R}^m, \qquad f(x) = Wx + b.

This is the classical definition of an affine transformation: linear (x↦Wxx \mapsto Wx) plus translation (+ b+\,b). The bias trick — appending a 11 to the end of xx and stacking bb as the last column of WW — turns an affine map into a purely linear one in the augmented space Rd+1\mathbb{R}^{d+1}. This simplifies proofs, but in code we keep bb separate for efficiency (it broadcasts).

5. Where this is going

All the algebra to come — attention, softmax, layer normalisation — rests on these three facts: there is a basis, there is an inner product, and layers are affine. Internalise them; there's no shortcut.