Zain Dana Harpernight school / F2

Matrices. One move for every place at once.

A matrix is a move that takes every place on a chart somewhere at once. Pylon's crane shows you how to read one, why the order of two moves matters, and how the attention lesson's question was made by one.

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.

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.