Linear algebra for LLMsPart 3: Matrices as transformations
Composition
6 min
One map after another
Apply , then apply to the result. The product does both in one step. Read it right to left: the matrix nearest acts first.
Write
Two maps to combine
is the quarter turn from the last deck. doubles every first coordinate.
Turn, then stretch:
turns to , and the stretch leaves it alone (its first coordinate is 0). turns to , then stretches to .
Stretch, then turn:
Now stretches to first, then turns to . A different map: . The same matrices in a different order do different things.
Write
✏️ Your turn
Compose
Write the matrix for “shear with , then turn with ”. Which order do you multiply in?
Work it out on paper, then go to the next card to check.
Answer:
acts first, so it sits on the right: . Check a column: sends to , and turns that to , which is column 2 of .
Stacked linear layers collapse
In a transformer
Two linear layers in a row are one linear layer: the product is a single matrix. A transformer’s MLP has a nonlinearity (GELU or SiLU) between its two matrices for exactly this reason. Without it, the extra matrix would add parameters but no new kinds of function.
Write
In code, read left to right
y = x @ W1 @ W2 # row-vector layout: W1 acts first
y = x @ (W1 @ W2) # the same map, one matrixWith row vectors the order flips: applies first. It’s the same composition, transposed: .
📌 Recap
Composition in one card
- : column layout reads right to left.
- Order matters: .
- Linear layers with nothing between them collapse into one matrix; activations prevent that.
- Code layout reads left to right.
Write