Laid Out in a Grid, It Computes — Linear Algebra
Read deep-learning code and almost every line is either "multiply matrices" or "multiply element by element". This chapter puts both down from the definition, states when shapes are allowed to meet, and adds two ways of looking at a matrix: eigenvalues and rank. The backpropagation of chapter 3 and the attention of chapter 8 are written in exactly the notation set here.
1. The inner product — how much two directions agree, as one number
Definition. For the inner product is
and is the Euclidean norm of .
Geometrically , where is the angle between the two directions. Rearranging,
which cancels the lengths and leaves agreement of direction alone. Equation (2) is cosine similarity, the quantity used to compare embeddings (chapter 10).
Worked example. For and , .
2. The matrix product — inner products, laid out in a grid
Definition. For and , the product has entries
The right-hand side of equation (3) is the inner product of row of with column of . So a matrix product is just inner products arranged in a table. It also explains why the column count of must equal the row count of : the summation index appears on both sides, so its range has to be the same or the sum cannot be written.
Worked example.
: the shape contracted. Check one entry: .
Note. and are different in general. Above, is a matrix — not even the same shape.
3. The Hadamard product — same shape or nothing
Definition. For of equal shape, the Hadamard (element-wise) product is
Equations (3) and (4) look alike but demand opposite things of the shapes: the matrix product needs the inner dimensions to agree, the Hadamard product needs all of them to agree.
Worked example.
The Hadamard product takes the lead in two fixed places: multiplying a gradient by the derivative of an activation (chapter 3 has in its equations), and gating how much to let through (the LSTM of chapter 5).
4. Rank — how many directions there really are
Definition. The rank of is the dimension of the space spanned by its columns.
When rows or columns can be written as multiples of each other, the number of genuine directions falls below the apparent size.
Worked example.
Row 2 is twice row 1. It is , yet only two rows are independent. Computing the singular values gives 8.4354, 0.9183, 0.0000 — two of them nonzero, so (the rank equals the number of nonzero singular values).
This view pays off when a large matrix can be replaced by a product of two small ones. Restricting to rank lets us write with of shape and of shape , cutting the stored numbers from to . That is what LoRA does (chapter 9).
5. Eigenvalues and diagonalisation — the directions a matrix cannot turn
Definition. For a square , a scalar with for some is an eigenvalue and an eigenvector.
An eigenvector's direction survives multiplication by unchanged; only its length scales, by .
Derivation. is . A nonzero solution exists exactly when has no inverse, i.e. when . Writing it out for ,
that is, .
Worked example. For we have and , so and , with eigenvectors and .
Diagonalisation. Put the eigenvectors in the columns of and the eigenvalues on the diagonal of ; then . Since is symmetric, is orthogonal and . Computing returns the original .
From here, repeated multiplication becomes cheap.
Every interior collapses to , leaving the -th power of a diagonal matrix — each eigenvalue raised to . Check it: multiplying by itself four times gives , and with gives the same . They agree.
Equation (6) pays off in chapter 5. An RNN multiplies by the same weights once per time step, so eigenvalues above 1 make explode and eigenvalues below 1 crush it to zero. Exploding and vanishing gradients are a statement about in equation (6).
6. Matching tensor shapes — nine-tenths of implementation
A "tensor" here is an array of numbers with three or more indices. What actually breaks in implementation is shape mismatch, so here is one layer, concretely.
A linear layer taking a batch of 32 inputs of 784 dimensions down to 128:
The inner 784 disappears; the outer 32 and 128 remain — exactly the shape rule of equation (3). The number of multiplications is .
The bias has shape — a single vector — yet it is added to all 32 rows. That is broadcasting: a missing axis is treated as a repetition of the same value. .
What this chapter does not claim
- Diagonalisation of non-symmetric matrices is not covered. requires a full set of eigenvectors, and some matrices do not have one (Jordan form is not used in this book).
- Singular value decomposition is used only for the fact "rank = number of nonzero singular values"; it is not derived. The definition of rank taken here is the dimension of the column space.
- Numerical error (rounding, conditioning) is not covered. The singular value printed as 0.0000 above is not exactly zero; it computes to around .
Every number in this chapter was computed and checked (matrix product, Hadamard product, singular values, eigenvalues, and the agreement of with ).
Going deeper
- Linear algebra for AI — what vectors and matrices are actually doing
- The linear algebra behind LoRA and RAG — eigenvalues, low rank and vector search, hands on
- Tensors and shape manipulation — read einsum and you can read the papers
- Principal component analysis from scratch — eigenvectors are the axes of your data
Comments
Sign in to comment