Step 2 of 6
Linear transformations & matrices
A matrix is a transformation of space. Understand what each column means, how composition works, and why matrix multiplication isn't arbitrary.
16 min read
A matrix isn't a table of numbers - it's a machine that moves space. Every matrix takes a vector as input and produces another vector as output, and the entire transformation is determined by just two things: where the basis vectors land.
Matrices as transformations#
The key mental shift: a matrix is a transformation of space. A 2 × 2 matrix takes every point on the plane and moves it somewhere else, preserving the grid lines as parallel and evenly spaced (that's what "linear" means). The first column is where î = (1, 0) goes; the second column is where ĵ = (0, 1) goes. Every other point follows along.
Column 1 = where î lands. Column 2 = where ĵ lands. Every other point moves by linear combination.
This is why matrix-vector multiplication works the way it does: you're writing the output as a linear combination of the matrix's columns, weighted by the input's components.
A gallery of transformations#
Each transformation has a visual signature. The grid tells you what the matrix does:
Rotation 90°
[[0, -1], [1, 0]] · det 1
Shear
[[1, 1], [0, 1]] · det 1
Scale ×2
[[2, 0], [0, 2]] · det 4
Reflection
[[-1, 0], [0, 1]] · det -1
Projection
[[1, 0], [0, 0]] · det 0
Squeeze
[[2, 0], [0, 0.5]] · det 1
Rotation preserves lengths and angles - it's a rigid motion. Shear slides one axis along the other (like pushing a deck of cards) while preserving area. Scaling stretches or shrinks. Reflection flips orientation. Projection flattens space onto a lower-dimensional subspace. Squeeze stretches one axis while compressing the other.
In ML: every linear layer is one of these transformations (usually a combination). The weight matrix rotates, scales, and shears the feature space so that the next nonlinearity can separate classes.
- Linear layer
- A neural network layer that multiplies its input by a weight matrix and adds a bias: y = Wx + b. The core building block of deep learning.
- Nonlinearity
- An activation function (like ReLU) applied after a linear layer - without it, stacking layers would collapse into a single matrix multiplication.
Composition is multiplication#
Applying transformation A followed by transformation B is the same as applying the single matrix BA (note the reversed order). This is why matrix multiplication has its particular rule - it comes from composing the underlying transformations.
Apply A first, then B. The composite transformation is the product BA - columns of A transformed by B.
Order matters: rotation then shear ≠ shear then rotation. This is why matrix multiplication isn't commutative (AB ≠ BA in general), even though it is associative (A(BC) = (AB)C).
Three dimensions and beyond#
Everything extends to 3D (and to n dimensions). A 3 × 3 matrix is defined by where three basis vectors land - its three columns. The rules are the same; only the visual intuition gets harder.
In 3D, three column vectors define where î, ĵ, and k̂ go. In nD, n columns define the full transformation.
In ML: real models work in hundreds or thousands of dimensions. A weight matrix W of shape (768, 3072) in a transformer MLP maps from 768 dimensions to 3072 dimensions - it's a linear transformation between those spaces, and each column is where one basis vector lands.
Non-square matrices change dimension#
A matrix doesn't have to be square. An m × n matrix maps from ℝⁿ to ℝᵐ - it changes the dimensionality of the space:
| Shape | Effect |
|---|---|
| m > n | Embeds a smaller space into a larger one (e.g., 64 → 768) |
| m < n | Projects a larger space down (e.g., 768 → 64) |
| m = n | Transforms within the same space |
In ML: the query, key, and value projections in attention are non-square matrices: they map from the model's hidden dimension (say 768) to a smaller head dimension (say 64). LoRA adapters use two thin non-square matrices to approximate a large square weight update.
- Query, Key, Value projections
- Three separate linear transformations applied to each token in attention - Query asks 'what am I looking for?', Key says 'what do I contain?', Value provides the actual information.
- LoRA
- Low-Rank Adaptation - a fine-tuning method that approximates weight updates with two small matrices instead of modifying the full weight matrix, dramatically reducing trainable parameters.
- Head dimension
- The size of each individual attention head's internal space - typically the model dimension divided by the number of heads (e.g., 768 / 12 = 64).
The column space and null space#
The column space of a matrix is the set of all possible outputs - the span of its columns. If you transform a vector, the result always lands in the column space. The null space is the set of all inputs that get mapped to zero - the information the transformation destroys.
Where it shows up in ML#
Linear layers are transformations
y = Wx + b is a linear transformation (W) followed by a translation (b). The activation function adds nonlinearity between them.
Attention is composed multiplications
Q = XWQ, K = XWK, V = XWV - three linear transformations, then QKᵀ computes all pairwise similarities at once.
LoRA decomposes transformations
Instead of a full weight update ΔW, LoRA uses BA - two thin matrices whose product approximates the update with far fewer parameters.
Composition = depth
Stacking linear layers (without activation) collapses to a single matrix. Nonlinearities between them are what give depth its power.