Matrices and linear systems
The matrix product as a composition of layers, the row and column pictures of Ax = b, rank, null space and rank–nullity, when a system is solvable, Gaussian elimination, the inverse, and over- and underdetermined systems.
The matrix product is composition
The topic Vectors, spaces and linear maps showed that every linear map is an matrix and that a linear layer computes . Two questions were left open: how to compute with such maps, and when an equation can be solved for . This topic answers both.
Start with composition. Apply first and second. The result is linear again, so it is a matrix too. We call that matrix the product and define it by
Column of is applied to column of . Entry by entry, row of meets column of :
The inner sizes must match: an matrix times an matrix is .
Order matters. Take the matrix that will run through this topic and one more:
Then , but . "First , then " is a different map from "first , then ". Grouping, on the other hand, is free: , because composition is associative. Transposing reverses the order: .
One consequence for networks: two linear layers with no activation between them are no deeper than one. Without biases, is a single layer with the matrix . That is why every hidden layer is followed by a nonlinearity.
Ax = b: the row and column pictures
A system of linear equations is a single matrix equation. The two equations
are with the running matrix and . There are two ways to read them.
The row picture. Each equation is a line in the plane; in it is a hyperplane. A solution is a point that lies on all of them. Here the two lines cross at . Lines can also be parallel or coincide, and the row picture alone does not say in advance which case you are in.
The column picture. The product mixes the columns of with weights taken from :
Solving means finding the weights that mix the columns into . Indeed, .
The column picture is the one that scales. In linear regression the data matrix has one row per example and one column per feature, and we look for weights with . The row picture says that every example is one constraint on . The column picture says that the predictions are a mix of feature columns, so the question becomes: can the targets be mixed from the features at all?
Column space, null space and rank
Two subspaces of a matrix answer two questions: which outputs can the map reach, and which inputs does it lose?
The column space is the set of all outputs:
By the column picture, it is the span of the columns, the image of the map. Its dimension is the rank , the number of linearly independent columns.
The null space, or kernel, is the set of inputs that the map sends to zero:
It is a subspace: if and , then too.
Here is an example from practice. Weekly sales are modeled from three features: spend on search ads, spend on social ads, and total ad spend, which someone helpfully added to the table. Four weeks of data, in thousands:
The third column is the sum of the first two, so only two columns are independent: , and is a plane inside . The null space contains , because for the columns .
This matters for training. If weights fit the data, then so do for every : the predictions do not change at all. The data cannot tell these weights apart, so no single coefficient measures "the effect of total spend". Dropping the redundant column or adding regularization picks one answer.
The rank–nullity theorem
Rank and null space are tied together. Each of the input dimensions either survives into the column space or collapses into the kernel:
This is the rank–nullity theorem. For the ad-spend matrix it reads .
One more fact, explained in the elimination block below: a matrix has as many independent rows as independent columns. So the rank is at most the smaller side, . A composition is no richer than its narrowest part: . Every output is an output of , so . And every with also has , so ; by rank–nullity, a kernel at least as large leaves a rank no larger than .
Two consequences for models:
- Bottlenecks. A linear autoencoder squeezes a -pixel image into a -number code and back: with . The product has rank at most , so by rank–nullity its kernel has dimension at least . Those input directions are lost and cannot be reconstructed.
- Low-rank updates. LoRA fine-tunes a weight matrix by adding with , and . The update has rank at most and costs parameters instead of . For and that is numbers instead of .
When Ax = b has a solution
Now the main question. The system has a solution exactly when : the target must be a mix of the columns. If is one solution (a particular solution), then all solutions are
The reason is one line: and give . So existence is decided by the column space and uniqueness by the null space. The solution is unique exactly when , that is, when .
An equivalent test is : appending as an extra column must not add a new independent direction.
Back to the ad-spend data. Every output of has the form
The sales vector has this form with . One solution is , and all of them are
The vector does not have this form. Its first two coordinates force , and then the last one would have to be . No weights fit those four weeks exactly.
Gaussian elimination
How do we find ranks, solutions and kernels in practice? With Gaussian elimination. Its one operation is to subtract a multiple of one equation from another, which does not change the set of solutions. The goal is zeros below the diagonal.
For the running system, write the augmented matrix and subtract half of row 1 from row 2:
Now back substitution: the last row gives , and the first gives , so . The leading entries and are the pivots.
The final staircase shape tells you the structure of the system:
- the rank is the number of pivots;
- a column without a pivot is a free variable, and each free variable gives one direction of the kernel;
- a row that reads with means there is no solution.
On the ad-spend matrix , elimination leaves pivots only in columns 1 and 2. Column 3 is free, and its kernel direction is .
The staircase also explains why a matrix has as many independent rows as independent columns. Every step can be undone, so elimination changes neither the span of the rows nor which combinations of columns vanish: and the eliminated system have the same solutions. In the staircase form the nonzero rows are independent, and so are the pivot columns. All columns have zeros below the last pivot row, so they lie in a space whose dimension is the number of pivots, and the pivot columns span it. Both counts equal the number of pivots.
Two practical notes. A zero pivot calls for a row swap. A tiny pivot is nearly as bad: it makes a huge multiplier, and rounding wipes out the other equations. So in floating point one always swaps in the entry of largest absolute value in the column, which is called partial pivoting. Elimination on an matrix costs about multiply–adds, roughly for . The multipliers can be stored as the factorization , or when rows were swapped, after which every new right-hand side costs only about . This is what np.linalg.solve does.
The inverse, and why it is rarely formed
Let be square, , with rank . Then and , so has exactly one solution for every . The map can be undone by the inverse matrix , with and . Read column by column: column of is the solution of .
For a matrix the inverse has a closed form:
The number is the determinant, the subject of the topic Determinants and eigenvalues. For the running matrix it is , so
Two rules follow from composition. : to undo "first , then ", undo first. And .
In formulas, is everywhere. In code, it is almost never computed that way. Forming takes solves, one per column, about multiply–adds in all, three times as much as elimination. It is also less accurate and turns a sparse matrix into a dense one. The covariance term of a Gaussian density and the ridge regression weights are both computed as solves, and so is every step of Newton's method later in the course. Normalizing flows go further: Glow learns its invertible convolutions directly in the factored form , so that undoing them is cheap.
Overdetermined and underdetermined systems
Put existence and uniqueness together for with rank :
| Shape | Rank | Solutions of |
|---|---|---|
| square, | exactly one, for every | |
| tall, | none or exactly one | |
| wide, | infinitely many, for every | |
| any | and | none or infinitely many |
A tall system has more equations than unknowns and is called overdetermined. This is ordinary regression: a thousand examples, a handful of features, and targets with noise. The column space is a thin subspace of , and noisy almost never lies in it exactly.
A wide system has more unknowns than equations and is called underdetermined. This is an overparameterized model with more weights than training examples. It fits the data in infinitely many ways, which differ by vectors from a kernel of dimension at least .
Both cases ask for something this topic cannot yet give. In the overdetermined case we want the for which is closest to . In the underdetermined case we want the shortest of all exact solutions. Closest and shortest need lengths and angles, and those come from inner products: the topic Inner products, norms and orthogonality.
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.