The film
About 5:30, hosted by Pylon. It asks you two questions and waits for your answer before it shows you. The narrated cut runs 4:29, voiced by Pylon in a synthesized voice made from the author's own voice; the author approved these cuts for publishing. Captions are on by default. The live version under it draws the same film in your browser, waits for your answer at each question, and runs a little longer; the full words are also below.
Nothing loads until you press play.
The lab: Pylon's crane cab
Type a matrix, or pick a move, and watch the tracing sheet go where it sends it; put two cranes in a row and swap their order. Then the attention question: every earlier word's score, q . k / 4, computed in your browser from the trained query and key matrices (32 x 16 each), with a control that sets the query matrix to zero. In the film's sentence the score of "the" against "kettle" is 41.28 / 4 = 10.32.
Everything is computed on your computer, in this page. Nothing is sent anywhere.
Everything the film says
Matrices
Matrices move space.
Last time every crate got a place on my chart. Tonight the crane moves all of them at once.
One move for every place
A crane that lifts one crate is useful. This crane takes the whole chart, and every crate on it, and turns it, or stretches it, or leans it over, in one go.
A matrix is that kind of move written as a grid of numbers. It sends every place on the chart somewhere, it keeps straight lines straight, and it leaves the corner where it is.
Read the columns
Here's how to read one. Forget the crates and watch two short arrows from the corner: one step east, one step north.
The first column of the matrix says where the east step lands. The second column says where the north step lands.
That is all a matrix has to say. Every place on the chart is so many east steps plus so many north steps, so it lands at the same number of the new east steps plus the new north steps.
Turn, stretch, lean
A quarter turn sends east to north, and north to west. Columns: zero, one, and minus one, zero.
A stretch sends east to two steps east and leaves north alone. Every patch of the dock gets twice the area, and that factor has a name: the determinant.
A lean, called a shear, slides the top of the chart sideways. Squares become slanted boxes, and yet the area stays the same: determinant one.
Two cranes in a row
Two cranes, one after the other. The first stretches the dock east, twice as wide. The second turns it a quarter turn.
Does it matter which crane goes first? (the film pauses here)
Stretch then turn sends the east step to 0, 2: two steps north. Turn then stretch sends it to 0, 1: one step north. So yes, the order matters.
Two moves in a row are still one move, and its matrix is the product of the two. Multiplying matrices means doing the moves one after the other, and that is why the order of a product matters.
The corner never moves
Here's a harder one.
Can any matrix move the corner of the chart? (the film pauses here)
No. A matrix times a list of zeros gives a list of zeros, so the corner always stays put. To slide the whole chart over, you add a fixed list after the move.
Many language models do exactly that: a matrix, then a list added on, called a bias. Every layer of the model from the attention lesson does it.
How a word makes its question
Back at the lighthouse, in the attention lesson, the word 'the' asked a question and kettle answered. Tallow skipped how the question was made. A matrix made it.
By the second layer, the list of 32 numbers at each word has been through a step called layer normalisation. It shifts the numbers to average zero and scales them to a standard spread, then applies a learned scale and shift of its own. Then it meets the head's query matrix: 32 rows by 16 columns, plus a bias.
Out comes the question, a list of 16 numbers. Kettle's list goes through a different matrix, the key matrix, and comes out as a label of the same length.
Multiply the question and the label number by number and add it up: 41.28. Divide by 4, the square root of 16: 10.32. That is the score the attention film showed, rebuilt here from the trained weights.
What a matrix cannot do
A matrix can only do moves that keep lines straight. Stack ten of them and you still have one straight-line move: their product.
So a model puts bends between its matrices. In the one from the attention lesson, a smooth curve called GELU sits between the two matrices in the second half of each layer and bends every number that passes through. The softmax and the layer normalisation bend things too.
How a model learns the numbers in its matrices is where the next lessons along the hill go.
Goodnight
A matrix moves every place at once. Read its columns to see where the steps go, multiply two to do one move after another, and mind the order.
Next along the hill, we find out which way is downhill. Goodnight.
Sources
Every claim in the film, with where it comes from and what that source does not prove. The numbers about the model are computed from its weights, which you can load and check: facts.json, model.json.
Strang 2016, Introduction to Linear Algebra. Strang, G. (2016). Introduction to Linear Algebra, 5th edition. Wellesley-Cambridge Press. Chapter 1 (vectors, linear combinations, lengths and dot products) and chapters 2 and 8 (matrices, multiplication, linear transformations). https://math.mit.edu/~gs/linearalgebra/
Used for: A matrix applied to a vector gives a combination of the matrix's columns, weighted by the vector's components; so the first column is where the first unit vector lands and the second column where the second lands (section 2.1). A linear transformation keeps straight lines straight and sends the zero vector to zero (chapter 8). A rotation by 90 degrees has columns (0, 1) and (-1, 0); a stretch along x by 2 has columns (2, 0) and (0, 1); a shear [[1, 1], [0, 1]] slides each row sideways by its height. The determinant of a 2 by 2 matrix is the factor by which it scales areas; a shear has determinant 1. Doing one transformation after another is multiplying their matrices, and matrix multiplication is not commutative in general: AB and BA can differ (section 2.4). A product of linear transformations is again a linear transformation, so stacking matrices without anything between them gives one matrix.
Does not prove: Anything about language models; it is the mathematics the lesson uses.
Night School's tiny transformer, the attention lesson's trained weights. media/school/attention/model.json, read by tools/school/fill_matrices.mjs, which rebuilds the attention film's query, key and score from the layer-norm output and the head's slices of the query-key-value matrix, and refuses to write if they differ from the film's by more than 1e-4. https://github.com/HarperZ9/HarperZ9.github.io/tree/main/media/school/matrices
Used for: Each layer has biases after its matrices (the query-key-value projection, the output projection and both MLP matrices). At layer 2, head 2, the input (after layer normalisation) is 32 numbers; the head's query and key slices are 32 by 16 with 16 biases each; q . k = 41.28 and q . k / 4 = 10.32, the score the attention film shows. The model's MLP uses GELU between its two matrices.
Does not prove: Anything about how large models' query matrices behave.
Vaswani and others 2017, Attention Is All You Need. Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L., Polosukhin, I. (2017). Attention Is All You Need. NeurIPS 30. https://arxiv.org/abs/1706.03762
Used for: Queries, keys and values are made by multiplying the input by learned projection matrices, one set per head (section 3.2.2). Scores are dot products of queries and keys divided by the square root of the key length (section 3.2.1). The paper writes the attention projections as matrices without biases; our model's projections have biases.
Does not prove: How a particular trained model uses its projections.
Ba, Kiros and Hinton 2016, Layer Normalization. Ba, J. L., Kiros, J. R., Hinton, G. E. (2016). Layer Normalization. arXiv:1607.06450. https://arxiv.org/abs/1607.06450
Used for: Layer normalisation shifts each input's numbers to mean zero and scales them to unit spread, then applies a learned gain and bias, so the output's spread is set by the learned gain.
Does not prove: Why it helps in a particular model.
Hendrycks and Gimpel 2016, Gaussian Error Linear Units. Hendrycks, D., Gimpel, K. (2016). Gaussian Error Linear Units (GELUs). arXiv:1606.08415. https://arxiv.org/abs/1606.08415
Used for: GELU is a smooth nonlinearity, x times the standard normal cumulative distribution at x, so it treats large, small and negative inputs differently. Our model uses the paper's tanh approximation of GELU.
Does not prove: That GELU is the best choice.
What this lesson leaves out
Real models use matrices thousands of numbers wide, and what a trained query matrix has learned is far harder to read than a crane's turn. The lesson shows one head of a two-layer toy model on one sentence.
The bends between matrices (GELU, softmax, layer normalisation) get one picture here; how a model learns the numbers in its matrices is the next lessons' subject.
Before this: Vectors (F1). Next along the hill: gradients, which way is downhill (F3), planned.