4. Matrices: machines that turn vectors
A matrix is a grid of numbers that turns one vector into another. Every layer of a transformer is mostly matrices, and the numbers in them are what “parameters” means.
A square grid with two arrows, (1, 0) and (0, 1). The grid moves step by step until every point (x, y) has become (x, y) times the matrix W: the first arrow lands on W's first row, the second on its second row, and the grid lines stay straight and evenly spaced. Then one row of numbers, X = [1, 1], is multiplied by the same W, one entry at a time, and a bias b is added: Y = XW + b.
The idea
A matrix is a grid of numbers. Multiply a vector by it and you get another vector: each entry of the result is the vector dotted with one column of the matrix. That is chapter 3's dot product, done once per column.
The animation shows what that does to space. A matrix can stretch it, squash it, shear it, turn it or flip it, but straight lines stay straight and the centre stays put. That is why it is called a linear transformation: a machine that turns vectors in a fixed, simple way.
Charles's Part 1 post calls these the building block of attention: “linear transformations, mathematical operations that project input vectors into new spaces”. A transformer is full of them. Each word's vector is turned into a query, a key and a value by three matrices (chapter 8), the heads' outputs are combined by another, and the feed-forward layer uses two more.
A worked example
Take apples from Charles's example, X = [1, 1], and the matrix \(W_V\) = [[1, 0], [−1, 1]]. The first entry of XW is X dotted with \(W_V\)'s first column, the second with its second column: XW = [0, 1]. Add a bias b = [0.5, −0.5] and Y = [0.5, 0.5].
Our model does the same with 64 numbers. In the first encoder layer, the vector for “She” (its embedding plus its position, chapters 5 and 12) is multiplied by the model's 64 × 64 matrix \(W^Q\). Each of the 64 entries of the query is a dot product of 64 pairs: 4,096 multiplications for one word. The first eight entries of each:
| Token | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | … 56 more |
|---|---|---|---|---|---|---|---|---|---|
| x | 0.18 | 1.67 | 0.16 | 1.99 | −0.62 | 1.39 | −1.55 | 1.60 | … |
| q | −0.11 | 1.25 | 0.11 | 0.51 | −0.89 | −1.93 | 0.67 | −0.49 | … |
The formula
Charles's post writes the building block like this:
- \(X\)
- the input: one row per token, each row a vector of d_in numbers
- \(W\)
- the weight matrix, d_in rows by d_out columns: learned in training
- \(b\)
- the bias: d_out numbers added to every row, also learned
- \(Y\)
- the output: one row per token, d_out numbers each
Entry \(j\) of a row of \(Y\) is that row of \(X\) dotted with column \(j\) of \(W\), plus \(b_j\). The paper's projections are matrices of this kind without a bias, named by their sizes: \(W^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}\) is a matrix with \(d_{\text{model}}\) rows and \(d_k\) columns.
Why it matters
When a model card counts parameters, it means these numbers: every entry of every W and b. Our paper model has 244,800 of them; the paper's base model about 65 million, its big model 213 million (Table 3). Every token you send passes through every one of those matrices, so the work per token grows with the parameter count, and so do the memory and the hardware a provider needs to serve the model. That is a large part of why bigger models cost more per token.
Where it fails
- Two matrices in a row are still one. Multiplying by W₁ and then by W₂ is the same as multiplying once by their product: stacking linear steps adds nothing. That is why the feed-forward layer puts a bend, the ReLU, between its two matrices (chapter 13).
- A matrix cannot decide. It does the same thing to every vector; it has no “if”. The choices in a transformer come from the softmax in attention and the bends between matrices, not from the matrices alone.
- Big matrices are slow to move. Serving a large model is often limited by moving its weights through memory, not only by the arithmetic.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
A row times a matrix
X = [1, 2] and W = [[2, 1], [0, 1]] (rows written one after the other). What is the second entry of XW?
What shape comes out?
A sentence of 5 tokens is X, a 5 × 64 matrix. One head's \(W^Q\) in our model is 64 × 16. What shape is \(Q = XW^Q\)?
Counting the work
Our model's \(W^Q\) is 64 × 64. How many multiplications does it take to turn one 64-number vector into its 64-number query?
What does it do?
What does the matrix \(W_K\) = [[0, 1], [1, 0]] do to the grid? Pick it in the animation to check.
In the paper
Section 3.2.2 writes the projections as matrices with their sizes, “\(W_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}\)”, and section 3.3's feed-forward network is two of them with a ReLU between: \(\max(0, xW_1 + b_1)W_2 + b_2\). Both are this chapter's Y = XW + b.
In the paper: section 3.2.2, 3.3