Skip to content
BIASED

Vectors, spaces and linear maps

Where the course starts: data as vectors, span, basis and dimension, linear maps as matrices, and the linear layer h = Wx + b.

Data are vectors

This course answers one question: what mathematics does a model need in order to represent data, learn from it and compute reliably? The short answer has four parts. Data are vectors. Models are maps between vector spaces. Learning is optimization. And all of it runs in floating point. This first topic starts the first two parts from scratch and assumes only school algebra.

A model never sees a flat; it sees numbers about it. Describe a flat by its area in square metres, its number of rooms and its age in years, and the flat becomes a list of three numbers, a vector:

x=(70325)∈R3.x = \begin{pmatrix} 70 \\ 3 \\ 25 \end{pmatrix} \in \mathbb R^3 .

Rn\mathbb R^n is the set of all lists of nn real numbers. In ML such a list is a feature vector, and its entries x1,…,xnx_1, \dots, x_n are the features. Vectors are columns; to save space we also write them in a row, x=(70,3,25)x = (70, 3, 25).

Two operations act entry by entry: adding two vectors and multiplying a vector by a number (scaling). Take a second flat y=(50,2,5)y = (50, 2, 5). Then

x+y=(120,5,30),12(x+y)=(60,2.5,15).x + y = (120, 5, 30), \qquad \tfrac12 (x + y) = (60, 2.5, 15).

The second vector is the average flat. An expression ax+bya x + b y with numbers aa and bb is a linear combination of xx and yy; the average is the case a=b=12a = b = \tfrac12.

In two dimensions a vector is also an arrow from the origin: v=(2,1)v = (2, 1) points two units right and one up. Adding vectors puts the arrows head to tail. The vector 2v2v is twice as long, and −v-v points the opposite way.

Vector spaces and subspaces

Lists of numbers are not the only things that can be added and scaled. Polynomials of degree at most 2, p(t)=c0+c1t+c2t2p(t) = c_0 + c_1 t + c_2 t^2, add and scale coefficient by coefficient. So do matrices of a fixed shape, images of a fixed size and functions on an interval.

A vector space is any set with addition and scaling by real numbers that obey the familiar rules of arithmetic. The order of addition does not matter, there is a zero vector, every vector has a negative, scaling distributes over sums, and so on. Everything in this topic uses only these rules, so it holds in all of these spaces at once. Their elements are all called vectors, even when they are polynomials.

A subspace is a subset that is a vector space by itself. It contains 00, and sums and multiples of its vectors stay inside. In R3\mathbb R^3 the plane

U={ x∈R3:x1+x2−x3=0 }U = \{\, x \in \mathbb R^3 : x_1 + x_2 - x_3 = 0 \,\}

is a subspace: if two vectors satisfy the equation, so do their sum and all their multiples. The parallel plane x1+x2−x3=1x_1 + x_2 - x_3 = 1 is not. It misses the origin, and the sum of two of its points lands on x1+x2−x3=2x_1 + x_2 - x_3 = 2.

Lines and planes through the origin are the typical subspaces of Rn\mathbb R^n. In ML they appear whenever features are tied by a linear equation. Describe a flat by its total area, its living area and the rest (kitchen, hall, bathroom). Every such feature vector satisfies x1=x2+x3x_1 = x_2 + x_3, so the whole dataset lies in a plane through the origin, and one of the three numbers is redundant. How many numbers data really need is the subject of the next two blocks.

Span and linear independence

Take vectors v1,…,vkv_1, \dots, v_k. A linear combination of them is

c1v1+c2v2+⋯+ckvkc_1 v_1 + c_2 v_2 + \dots + c_k v_k

with any numbers c1,…,ckc_1, \dots, c_k. All such combinations together form the span span⁡(v1,…,vk)\operatorname{span}(v_1, \dots, v_k). It is always a subspace, since sums and multiples of combinations are again combinations. It is also the smallest one that contains all the viv_i: any subspace that contains them contains all their combinations.

For example, take v1=(1,0,1)v_1 = (1, 0, 1) and v2=(0,1,1)v_2 = (0, 1, 1) in R3\mathbb R^3. Their combinations are

c1v1+c2v2=(c1, c2, c1+c2).c_1 v_1 + c_2 v_2 = (c_1,\ c_2,\ c_1 + c_2).

So (2,3,5)=2v1+3v2(2, 3, 5) = 2v_1 + 3v_2 lies in the span. The vector (1,1,1)(1, 1, 1) does not: its first two entries force c1=c2=1c_1 = c_2 = 1, and then the third entry would be 22. The span is exactly the plane UU from the previous block.

A list is linearly dependent if one of its vectors is a combination of the others: it adds nothing new to the span. Otherwise the list is linearly independent. In one line: the only combination equal to zero is the trivial one,

c1v1+⋯+ckvk=0 ⟹ c1=⋯=ck=0.c_1 v_1 + \dots + c_k v_k = 0 \ \Longrightarrow\ c_1 = \dots = c_k = 0 .

The two descriptions agree. If a combination equal to zero has some cj≠0c_j \ne 0, divide by cjc_j and solve for vjv_j. If vjv_j is a combination of the others, move it to the other side: that gives a combination equal to zero with coefficient −1-1 on vjv_j.

The pair v1,v2v_1, v_2 above is independent, since (c1,c2,c1+c2)=0(c_1, c_2, c_1 + c_2) = 0 forces c1=c2=0c_1 = c_2 = 0. The vectors (1,2)(1, 2), (3,4)(3, 4), (5,6)(5, 6) are dependent:

(1,2)−2 (3,4)+(5,6)=(0,0),(1, 2) - 2\,(3, 4) + (5, 6) = (0, 0),

so (5,6)=2 (3,4)−(1,2)(5, 6) = 2\,(3, 4) - (1, 2) is redundant. Two vectors are dependent exactly when one is a multiple of the other.

Basis, dimension and coordinates

A basis of a space is a linearly independent list that spans it. Spanning means every vector is a combination of the basis vectors. Independence means that combination is unique: if two different combinations gave the same vector, their difference would be a nontrivial combination equal to zero. The unique coefficients are the coordinates of the vector in that basis.

The standard basis of Rn\mathbb R^n is e1=(1,0,…,0)e_1 = (1, 0, \dots, 0), …, en=(0,…,0,1)e_n = (0, \dots, 0, 1). In it the coordinates are just the entries: (3,1)=3e1+1e2(3, 1) = 3e_1 + 1e_2. Another basis gives other coordinates. With b1=(1,1)b_1 = (1, 1) and b2=(1,−1)b_2 = (1, -1),

(3,1)=2 b1+1 b2,(3, 1) = 2\,b_1 + 1\,b_2 ,

so the same vector has coordinates (2,1)(2, 1). A list of numbers describes a vector only once a basis is fixed; the vector itself does not depend on that choice.

Every basis of a space has the same number of vectors. The reason is the exchange lemma: if mm vectors span a space, no independent list in it has more than mm vectors. (Its proof swaps the independent vectors into the spanning list one at a time, and the list keeps spanning.) Apply it to two bases, each in turn as the spanning list: each length is at most the other. That common number is the dimension. dim⁡Rn=n\dim \mathbb R^n = n. The plane UU has dimension 2, with basis v1,v2v_1, v_2 from the previous block. Polynomials of degree at most 2 have the basis 1,t,t21, t, t^2 and dimension 3; the polynomial 3+2t+5t23 + 2t + 5t^2 has coordinates (3,2,5)(3, 2, 5).

Two consequences in a space of dimension nn. No list longer than nn can be independent: a basis is a spanning list of nn vectors. And any nn independent vectors already form a basis. If their span missed some vector ww, adding ww would keep the list independent, because a combination equal to zero with a nonzero coefficient on ww would put ww in their span. That would make n+1n + 1 independent vectors, one too many. Back to the flats: their vectors (total, living, rest) fill only the 2-dimensional plane x1=x2+x3x_1 = x_2 + x_3, so two numbers per flat describe the data without loss.

Linear maps

A model turns an input vector into an output vector. The simplest maps that respect addition and scaling are the linear ones. A map TT from a vector space VV to a vector space WW is linear if, for all vectors u,vu, v and numbers cc,

T(u+v)=T(u)+T(v),T(c v)=c T(v).T(u + v) = T(u) + T(v), \qquad T(c\,v) = c\,T(v).

Together the two rules say: TT sends a linear combination to the same combination of the images.

Examples: rotating or stretching the plane; a score that weighs features, x↦2x1−0.5x3x \mapsto 2x_1 - 0.5x_3; differentiating a polynomial, p↦p′p \mapsto p'. Non-examples: the shift x↦x+bx \mapsto x + b with b≠0b \ne 0, because every linear map sends 00 to 00 (take c=0c = 0); ReLU, x↦max⁡(0,x)x \mapsto \max(0, x), because max⁡(0,−1)=0\max(0, -1) = 0 while −max⁡(0,1)=−1-\max(0, 1) = -1; the length of a vector.

The key consequence: a linear map is fixed by what it does to a basis. If v=c1b1+⋯+cnbnv = c_1 b_1 + \dots + c_n b_n, then

T(v)=c1T(b1)+⋯+cnT(bn).T(v) = c_1 T(b_1) + \dots + c_n T(b_n).

So nn vectors T(b1),…,T(bn)T(b_1), \dots, T(b_n) determine TT everywhere. For example, a linear T:R2→RT: \mathbb R^2 \to \mathbb R with T(e1)=2T(e_1) = 2 and T(e2)=−1T(e_2) = -1 has T(3,4)=3⋅2+4⋅(−1)=2T(3, 4) = 3 \cdot 2 + 4 \cdot (-1) = 2.

Every linear map is a matrix

Now let T:Rn→RmT: \mathbb R^n \to \mathbb R^m be linear. Write xx in the standard basis, x=x1e1+⋯+xnenx = x_1 e_1 + \dots + x_n e_n. By the previous block,

T(x)=x1T(e1)+⋯+xnT(en).T(x) = x_1 T(e_1) + \dots + x_n T(e_n).

Each T(ej)T(e_j) is a vector in Rm\mathbb R^m. Put them side by side as columns. The result is a table with mm rows and nn columns, the matrix A∈Rm×nA \in \mathbb R^{m \times n} of TT. The matrix–vector product AxAx is defined as exactly this combination of the columns. Entry by entry,

(Ax)i=ai1x1+ai2x2+⋯+ainxn=∑j=1naijxj.(Ax)_i = a_{i1} x_1 + a_{i2} x_2 + \dots + a_{in} x_n = \sum_{j=1}^n a_{ij} x_j .

Hence T(x)=AxT(x) = Ax for every xx. Conversely, every map x↦Axx \mapsto Ax is linear. So linear maps Rn→Rm\mathbb R^n \to \mathbb R^m and m×nm \times n matrices are the same thing: column jj of the matrix is where eje_j goes. An m×nm \times n matrix takes nn numbers in and gives mm numbers out.

For example, if T(e1)=(2,0)T(e_1) = (2, 0) and T(e2)=(1,1)T(e_2) = (1, 1), then

A=(2101),A(3−2)=3(20)−2(11)=(4−2).A = \begin{pmatrix} 2 & 1 \\ 0 & 1 \end{pmatrix}, \qquad A \begin{pmatrix} 3 \\ -2 \end{pmatrix} = 3 \begin{pmatrix} 2 \\ 0 \end{pmatrix} - 2 \begin{pmatrix} 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 4 \\ -2 \end{pmatrix}.

Other spaces work the same way once we fix bases. Differentiation takes polynomials of degree at most 2 (basis 1,t,t21, t, t^2) to degree at most 1 (basis 1,t1, t). It sends 1↦01 \mapsto 0, t↦1t \mapsto 1, t2↦2tt^2 \mapsto 2t, so its columns are (0,0)(0, 0), (1,0)(1, 0), (0,2)(0, 2):

D=(010002),D(325)=(210),D = \begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 2 \end{pmatrix}, \qquad D \begin{pmatrix} 3 \\ 2 \\ 5 \end{pmatrix} = \begin{pmatrix} 2 \\ 10 \end{pmatrix},

which are the coordinates of (3+2t+5t2)′=2+10t(3 + 2t + 5t^2)' = 2 + 10t.

A linear layer

The basic building block of a neural network is a linear layer. It takes x∈Rnx \in \mathbb R^n and returns

h=Wx+b,W∈Rm×n,b∈Rm.h = Wx + b, \qquad W \in \mathbb R^{m \times n},\quad b \in \mathbb R^m .

The matrix WW holds the weights and bb is the bias. Row ii of WW holds the weights of output ii: hi=wi1x1+⋯+winxn+bih_i = w_{i1} x_1 + \dots + w_{in} x_n + b_i. For example,

W=(10−1210),x=(123),b=(0.5−1):Wx=(−24),h=(−1.53).W = \begin{pmatrix} 1 & 0 & -1 \\ 2 & 1 & 0 \end{pmatrix}, \quad x = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix}, \quad b = \begin{pmatrix} 0.5 \\ -1 \end{pmatrix}: \qquad Wx = \begin{pmatrix} -2 \\ 4 \end{pmatrix}, \quad h = \begin{pmatrix} -1.5 \\ 3 \end{pmatrix}.

Strictly speaking, x↦Wx+bx \mapsto Wx + b is not linear when b≠0b \ne 0: it sends 00 to bb. A linear map plus a constant shift is called affine. A small trick makes it linear again. Append a constant 11 to the input:

Wx+b=(Wb)(x1).Wx + b = \begin{pmatrix} W & b \end{pmatrix} \begin{pmatrix} x \\ 1 \end{pmatrix}.

Multiplying by a standard basis vector picks out a column: WejW e_j is column jj of WW. Embedding tables in language models work this way. Token number jj is the one-hot vector eje_j, and its embedding EejE e_j is column jj of the table EE. (PyTorch stores the same table with rows and columns swapped, so there it is row jj.)

One layer is one matrix. A network chains layers, x↦W1x↦W2(W1x)x \mapsto W_1 x \mapsto W_2 (W_1 x). What is the matrix of such a chain, and why do two linear layers in a row buy nothing over one? And in reverse: given an output hh, which inputs solve Wx=hWx = h — is there one, many, or none? The next topic, Matrices and linear systems, answers both.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.