Math for ML, by Hand · part 2 of 4

What Does a Weight Matrix Actually Do to Your Data?

Math for ML, by Hand (part 2 of 4): linear maps, matrix products, determinants, eigenvectors and the SVD, on one 2×2 matrix.

Four columns listing twenty topics across four parts; Part 2 is highlighted. Frame 1.
You are in Part 2: what a matrix does

A dense layer computes Ax + b, and most people learn to read A as a table of weights. That reading is correct and almost useless. It does not say what the layer does to the data.

A better reading is geometric. A matrix moves every point of the space at once, and it moves them in a very constrained way: straight lines stay straight, parallel lines stay parallel, and the origin stays put. Everything in this part follows from that.

The tiny model

We keep the cast from part 1: x = (3, 4) for the user who clicked, x′ = (1, 2) for the user who did not, and the model's first box, h = Ax with A = [[2, 1], [1, 2]]. Our two users land at h = (10, 11) and h′ = (4, 5).

Linear maps move the whole grid

A linear map keeps grid lines straight, parallel and evenly spaced, and keeps the origin fixed.

A square grid rotated 90 degrees sends x to (−4, 3); a curved warp is marked not linear; matrix A shears the grid and sends x to (10, 11). Frame 1.
Before: a square grid and x = (3, 4)
A square grid rotated 90 degrees sends x to (−4, 3); a curved warp is marked not linear; matrix A shears the grid and sends x to (10, 11). Frame 2.
Rotate by 90°: every grid line turns together, x lands on (−4, 3)
A square grid rotated 90 degrees sends x to (−4, 3); a curved warp is marked not linear; matrix A shears the grid and sends x to (10, 11). Frame 3.
Tempting: any smooth warp is a 'transformation' we can use here
A square grid rotated 90 degrees sends x to (−4, 3); a curved warp is marked not linear; matrix A shears the grid and sends x to (10, 11). Frame 4.
The cast's A: grid lines stay straight and evenly spaced, x lands on (10, 11)

Rotate the plane by 90° around the origin. Every grid line turns together, and x = (3, 4) lands on (−4, 3). Its length is still 5, and the new arrow is at a right angle to the old one (the inner product from part 1 is 0).

The rule in the equation says that scaling and adding can happen before or after the map with the same result. Check it on A: A(x + x′) = (14, 16), and Ax + Ax′ = (10, 11) + (4, 5) = (14, 16).

A curved warp fails this test. If grid lines bend, then mapping the sum is no longer the sum of the maps. That is why the layers of a network need a separate nonlinearity, such as σ or ReLU, to bend anything: the matrix itself cannot.

The payoff of linearity is that two arrows decide everything. Once you know where e₁ and e₂ go, every other vector follows, because every vector is a weighted sum of e₁ and e₂. Write those two images as columns and you have the matrix.

By hand
R  = [[0, −1], [1, 0]]     columns = where e₁ and e₂ land
R x = 3·(0, 1) + 4·(−1, 0) = (−4, 3)
A x = 3·(2, 1) + 4·(1, 2) = (6 + 4, 3 + 8) = (10, 11)

Why ML cares. h = Ax in our model is a linear map. So is every dense layer, every convolution and the query, key and value projections in attention.

What it cannot do. One map at a time. Networks stack layers, so we need to know what happens when maps are chained.

The matrix product is one map after another

Multiplying matrices composes the maps, so the formula is whatever makes (AB)x = A(Bx) true.

Matrix A times R: row 2 of A and column 1 of R give entry 2; the product is [[1, −2], [2, −1]]; both routes send x to (−5, 2). Frame 1.
Two maps: first R (rotate), then A
Matrix A times R: row 2 of A and column 1 of R give entry 2; the product is [[1, −2], [2, −1]]; both routes send x to (−5, 2). Frame 2.
Entry (2, 1) of AR: row 2 of A times column 1 of R = 1·0 + 2·1 = 2
Matrix A times R: row 2 of A and column 1 of R give entry 2; the product is [[1, −2], [2, −1]]; both routes send x to (−5, 2). Frame 3.
All four entries: AR = [[1, −2], [2, −1]]
Matrix A times R: row 2 of A and column 1 of R give entry 2; the product is [[1, −2], [2, −1]]; both routes send x to (−5, 2). Frame 4.
Check on x: (AR)x = (−5, 2), and A(Rx) = (−5, 2) too
Matrix A times R: row 2 of A and column 1 of R give entry 2; the product is [[1, −2], [2, −1]]; both routes send x to (−5, 2). Frame 5.
Order matters: RA = [[−1, −2], [2, 1]] is a different map

Rotate first, then apply A. Each entry of the combined matrix AR is one row of A against one column of R. Entry (2, 1) uses row 2 of A, (1, 2), and column 1 of R, (0, 1): 1·0 + 2·1 = 2. All four entries give AR = [[1, −2], [2, −1]].

The formula looks arbitrary until you test what it buys. Route A combines the maps first: (AR)x = (−5, 2). Route B applies them one at a time: Rx = (−4, 3), then A(Rx) = (−5, 2). Same answer, by construction. The row-times-column rule is the only rule that makes this hold for every x.

Order matters. RA = [[−1, −2], [2, 1]] applies A first and then rotates, and it is a different map. Matrix multiplication is associative, (AB)C = A(BC), but not commutative.

By hand
row 2 of A · column 1 of R = 1·0 + 2·1 = 2
AR = [[2·0 + 1·1, 2·(−1) + 1·0],
      [1·0 + 2·1, 1·(−1) + 2·0]] = [[1, −2], [2, −1]]
(AR)x = (1·3 − 2·4, 2·3 − 1·4) = (−5, 2)
A(Rx) = A(−4, 3) = (−8 + 3, −4 + 6) = (−5, 2)   ✓

Why ML cares. Stacking two linear layers without a nonlinearity between them gives one linear layer, W₂W₁. Associativity also decides cost: in attention, computing (QKᵀ)V and Q(KᵀV) gives the same result at very different prices.

What it cannot do. The product says how to compute the map, not how much it stretches space. That is the determinant's job.

The determinant is the area factor

The determinant says how much a map scales area, and its sign says whether it flips orientation.

The unit square maps to a parallelogram of area 3 under A, collapses to a line under a singular matrix, and is mirrored by a flip with determinant −1. Frame 1.
The unit square: area 1
The unit square maps to a parallelogram of area 3 under A, collapses to a line under a singular matrix, and is mirrored by a flip with determinant −1. Frame 2.
After A: a parallelogram with area 3 = det A
The unit square maps to a parallelogram of area 3 under A, collapses to a line under a singular matrix, and is mirrored by a flip with determinant −1. Frame 3.
Tempting: det A = 0 means 'nothing happened'
The unit square maps to a parallelogram of area 3 under A, collapses to a line under a singular matrix, and is mirrored by a flip with determinant −1. Frame 4.
Actually det = 0 flattens the square onto a line: area 0, information lost
The unit square maps to a parallelogram of area 3 under A, collapses to a line under a singular matrix, and is mirrored by a flip with determinant −1. Frame 5.
A flip: det = −1. Area kept, orientation reversed (the F reads backwards)

Send the unit square through A. It becomes a parallelogram with corners at (0, 0), (2, 1), (3, 3) and (1, 2), and its area is 3. Every shape in the plane has its area multiplied by the same factor, so det A = 3.

A tempting misreading is that det = 0 means nothing happened. It means the opposite. The matrix [[1, 2], [2, 4]] sends both columns onto the same line, so the square is flattened to a segment with area 0. Two different inputs now share one output, and the map cannot be undone. Zero determinant means not invertible.

Negative determinants mean a mirror. The flip [[0, 1], [1, 0]] swaps the axes, keeps every area, and has det = −1: a letter F drawn in the square comes out reading backwards.

The general formula for an n×n determinant is a sum over permutations, and it is rarely computed that way in practice. The geometric meaning is the one to keep. 3Blue1Brown's video on determinants animates it well.

By hand
det A = 2·2 − 1·1 = 3
shoelace area of A(unit square) = 3   ✓
det [[1, 2], [2, 4]] = 4 − 4 = 0      flattened
det [[0, 1], [1, 0]] = 0 − 1 = −1     mirrored

Why ML cares. Normalizing flows track how a map stretches probability density, which is a log-determinant. A near-zero determinant warns that a layer is collapsing directions and losing information.

What it cannot do. One number for the whole map. It does not say which directions get stretched and which get squeezed.

Eigenvectors: the directions a matrix only stretches

An eigenvector keeps its direction under the map; the eigenvalue is how much it stretches.

Unit arrows and their images under A. Only the directions (1, 1) and (1, −1) keep their line, scaled by 3 and 1. Frame 1.
Feed A a unit arrow and draw where it lands
Unit arrows and their images under A. Only the directions (1, 1) and (1, −1) keep their line, scaled by 3 and 1. Frame 2.
Most arrows get turned as well as stretched
Unit arrows and their images under A. Only the directions (1, 1) and (1, −1) keep their line, scaled by 3 and 1. Frame 3.
At 45°, A only stretches: A(1, 1) = 3·(1, 1). Eigenvalue 3
Unit arrows and their images under A. Only the directions (1, 1) and (1, −1) keep their line, scaled by 3 and 1. Frame 4.
At 135°, A(1, −1) = 1·(1, −1). Eigenvalue 1
Unit arrows and their images under A. Only the directions (1, 1) and (1, −1) keep their line, scaled by 3 and 1. Frame 5.
x = (3, 4) is not an eigenvector: Ax = (10, 11) points elsewhere

Feed A a unit arrow and draw where it lands. Most arrows get turned as well as stretched. Sweep the input around the circle and two directions stand out: along (1, 1) the output stays on the same line and is 3 times longer, and along (1, −1) it does not move at all. A has eigenvalues 3 and 1.

Our user x = (3, 4) is not one of them. Ax = (10, 11), and 10/3 is not 11/4, so the output points somewhere new.

These are the tilted measuring sticks from part 1. In that basis, A is easy: x has coordinates 4.95 and −0.71, and A multiplies them by 3 and 1. Rebuild with the scaled coordinates and you get (10, 11) again. Diagonal matrices are the easiest maps there are, and eigenvectors are the basis in which a matrix becomes diagonal.

To find eigenvalues by hand, ask when A − λI flattens space, that is, when det(A − λI) = 0. Here that is (2 − λ)² − 1 = 0, so λ = 3 or λ = 1.

By hand
A(1, 1)  = (3, 3)  = 3·(1, 1)
A(1, −1) = (1, −1) = 1·(1, −1)
det(A − λI) = (2 − λ)² − 1 = 0   →  λ = 3, 1
x in the eigenbasis: (4.95, −0.71) → (3·4.95, 1·(−0.71)) → rebuild = (10, 11) = Ax   ✓

Why ML cares. PCA's directions are the eigenvectors of the data's covariance matrix. The eigenvalues of the loss curvature decide how large a learning rate can be, which part 3 shows on this exact model.

What it cannot do. Not every matrix has a full set of real eigenvectors. A rotation turns every arrow, so it has none. We need a decomposition that works for every matrix.

The SVD: rotate, stretch, rotate

Every matrix is a rotation, then a stretch along the axes, then another rotation.

The unit circle is rotated, stretched into an ellipse with semi-axes 6.71 and 2.24, then rotated again; the result matches B applied to the circle. A square is crossed out. Frame 1.
Take the unit circle and B = [[3, 0], [4, 5]]
The unit circle is rotated, stretched into an ellipse with semi-axes 6.71 and 2.24, then rotated again; the result matches B applied to the circle. A square is crossed out. Frame 2.
Step 1, Vᵀ: rotate (the circle looks the same; watch the two marked axes)
The unit circle is rotated, stretched into an ellipse with semi-axes 6.71 and 2.24, then rotated again; the result matches B applied to the circle. A square is crossed out. Frame 3.
Step 2, Σ: stretch the axes by 6.71 and 2.24. The circle becomes an ellipse
The unit circle is rotated, stretched into an ellipse with semi-axes 6.71 and 2.24, then rotated again; the result matches B applied to the circle. A square is crossed out. Frame 4.
Step 3, U: rotate the ellipse into place. This is exactly B applied to the circle
The unit circle is rotated, stretched into an ellipse with semi-axes 6.71 and 2.24, then rotated again; the result matches B applied to the circle. A square is crossed out. Frame 5.
A linear map can never turn a circle into a square

Take a matrix with no nice eigenvectors, B = [[3, 0], [4, 5]], and watch what it does to the unit circle. The SVD splits the map into three steps. Vᵀ rotates (the circle looks unchanged, but the two marked axes turn). Σ stretches along the axes by 6.71 and 2.24, so the circle becomes an ellipse. U rotates the ellipse into its final place. The result matches B applied to the circle exactly.

The stretch factors are the singular values. They are the lengths of the ellipse's semi-axes, and their product, 6.71 × 2.24 = 15, is the area factor |det B|. A linear map can never turn a circle into a square, because it has to send the circle to an ellipse.

For a symmetric matrix like our A, the two rotations line up with the eigenvectors, and the singular values are the eigenvalues 3 and 1. In general they differ, and the SVD exists even when eigenvectors do not.

By hand
BᵀB = [[25, 20], [20, 25]]   eigenvalues 45, 5
singular values = √45 = 6.71,  √5 = 2.24
U Σ Vᵀ = [[3, 0], [4, 5]] = B   ✓
6.71 × 2.24 = 15 = |det B|

Why ML cares. Keep only the largest singular values and you get the best low-rank approximation of a matrix. That is PCA, latent semantic analysis, matrix-factorisation recommenders and LoRA fine-tuning.

What it cannot do. All of part 2 treats A as fixed. Training changes it, and to change it in the right direction we need derivatives.

Where this leaves us

The single idea to keep: a matrix is a map, and the matrix product is one map after another. Determinants, eigenvalues and singular values are three ways of summarising what the map does to areas and directions.

Part 3 lets the map change. Training adjusts A, w and b to lower the loss, and that needs derivatives: how the output moves when one weight moves.

Next: part 3, Why Does Gradient Descent Bounce?