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:
is the set of all lists of real numbers. In ML such a list is a feature vector, and its entries are the features. Vectors are columns; to save space we also write them in a row, .
Two operations act entry by entry: adding two vectors and multiplying a vector by a number (scaling). Take a second flat . Then
The second vector is the average flat. An expression with numbers and is a linear combination of and ; the average is the case .
In two dimensions a vector is also an arrow from the origin: points two units right and one up. Adding vectors puts the arrows head to tail. The vector is twice as long, and 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, , 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 , and sums and multiples of its vectors stay inside. In the plane
is a subspace: if two vectors satisfy the equation, so do their sum and all their multiples. The parallel plane is not. It misses the origin, and the sum of two of its points lands on .
Lines and planes through the origin are the typical subspaces of . 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 , 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 . A linear combination of them is
with any numbers . All such combinations together form the span . It is always a subspace, since sums and multiples of combinations are again combinations. It is also the smallest one that contains all the : any subspace that contains them contains all their combinations.
For example, take and in . Their combinations are
So lies in the span. The vector does not: its first two entries force , and then the third entry would be . The span is exactly the plane 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,
The two descriptions agree. If a combination equal to zero has some , divide by and solve for . If is a combination of the others, move it to the other side: that gives a combination equal to zero with coefficient on .
The pair above is independent, since forces . The vectors , , are dependent:
so 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 is , …, . In it the coordinates are just the entries: . Another basis gives other coordinates. With and ,
so the same vector has coordinates . 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 vectors span a space, no independent list in it has more than 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. . The plane has dimension 2, with basis from the previous block. Polynomials of degree at most 2 have the basis and dimension 3; the polynomial has coordinates .
Two consequences in a space of dimension . No list longer than can be independent: a basis is a spanning list of vectors. And any independent vectors already form a basis. If their span missed some vector , adding would keep the list independent, because a combination equal to zero with a nonzero coefficient on would put in their span. That would make independent vectors, one too many. Back to the flats: their vectors (total, living, rest) fill only the 2-dimensional plane , 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 from a vector space to a vector space is linear if, for all vectors and numbers ,
Together the two rules say: sends a linear combination to the same combination of the images.
Examples: rotating or stretching the plane; a score that weighs features, ; differentiating a polynomial, . Non-examples: the shift with , because every linear map sends to (take ); ReLU, , because while ; the length of a vector.
The key consequence: a linear map is fixed by what it does to a basis. If , then
So vectors determine everywhere. For example, a linear with and has .
Every linear map is a matrix
Now let be linear. Write in the standard basis, . By the previous block,
Each is a vector in . Put them side by side as columns. The result is a table with rows and columns, the matrix of . The matrix–vector product is defined as exactly this combination of the columns. Entry by entry,
Hence for every . Conversely, every map is linear. So linear maps and matrices are the same thing: column of the matrix is where goes. An matrix takes numbers in and gives numbers out.
For example, if and , then
Other spaces work the same way once we fix bases. Differentiation takes polynomials of degree at most 2 (basis ) to degree at most 1 (basis ). It sends , , , so its columns are , , :
which are the coordinates of .
A linear layer
The basic building block of a neural network is a linear layer. It takes and returns
The matrix holds the weights and is the bias. Row of holds the weights of output : . For example,
Strictly speaking, is not linear when : it sends to . A linear map plus a constant shift is called affine. A small trick makes it linear again. Append a constant to the input:
Multiplying by a standard basis vector picks out a column: is column of . Embedding tables in language models work this way. Token number is the one-hot vector , and its embedding is column of the table . (PyTorch stores the same table with rows and columns swapped, so there it is row .)
One layer is one matrix. A network chains layers, . 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 , which inputs solve — 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.
Answers are saved on the server and tied to this browser until you sign in.