Linear algebra for LLMsPart 3: Matrices as transformations
Linear maps
7 min
A matrix is a function
Feed a vector in, get a vector out. In this part we draw the maps the way geometry books do, with column vectors and . Code does the same thing with rows and ; only the layout is transposed.
Write
Watch where the basis vectors go
is column 1 of (blue). is column 2 (pink). The whole grid follows them: every square becomes the same slanted parallelogram.
Write
Linear means: follow the basis vectors
A linear map keeps sums and scalings. So once you know where and go, you know where everything goes: lands on . That’s the column view of from Part 2.
Write
Scale
A diagonal matrix stretches each axis by its own factor: twice as wide, half as tall. No direction mixes with another.
Shear
stays where it is; leans over to . Each point slides sideways by its height. Areas don’t change.
Reflect
Flipping the sign of one column mirrors the plane: here across the horizontal axis. The plane comes out “inside out”, which the determinant will report as a minus sign.
Project
lands on , so the whole plane collapses onto the horizontal axis. Every point keeps its first coordinate and loses its second, for good. A map that loses information can’t be undone.
✏️ Your turn
What does this matrix do?
Write and , then describe the map in words.
Work it out on paper, then go to the next card to check.
Answer: a quarter turn
and : both basis vectors turn 90° anticlockwise, so everything does. The next deck but one is all about rotations.
Every layer is a linear map
In a transformer
A weight of shape maps one feature space to another, exactly like these 2D pictures but with thousands of axes. The interesting part of a transformer is what each map keeps, mixes or throws away.
y = x @ W # the same map, code layout
y = (W.T @ x.T).T # the math layout, transposed back📌 Recap
Linear maps in one card
- A matrix is a function .
- Column of is where lands; linearity gives everything else.
- Diagonal: scale. Off-diagonal: shear. Negative column: reflect. Zero column: project and lose information.
Write