The film
About 5:51, hosted by Pylon. It asks you two questions and waits for your answer before it shows you. The narrated cut runs 4:50, 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 chart room
Drag two crates and watch the dot product, the lengths and the cosine change. Then open the word table of the model from the attention lesson: 46 words, 32 numbers each. Pick a word and see its nearest neighbours, computed in your browser from the trained lists. Two controls rebuild the table: the lists shuffled between words, and the lists from before training. In the film's measurement, 80% of words had a nearest neighbour of the same kind, about 23% with shuffled lists, and 9% before training.
Everything is computed on your computer, in this page. Nothing is sent anywhere.
Everything the film says
Vectors
Vectors. A list of numbers is a place.
I run the dock at Night School. Every crate that comes in gets an address on my chart.
Two numbers make a place
Here is a crate. It sits four squares east of the corner and one and a half squares north.
Write that down as a list: four, then one and a half. That list is a vector.
You can read it as a place, or as an arrow from the corner to the crate. Same list, same thing.
Arrows add and stretch
Arrows add. Walk the first one, then walk the second from where you stopped. Where you end up is the sum, and its numbers are the two lists added place by place.
Arrows stretch. Times two keeps the direction and doubles the length. Times minus one turns it round to face the other way.
The dot product
Now two crates, and a game. Multiply their lists number by number, and add it all up.
I'll swing the second crate round the corner while the sum keeps count.
As the second crate swings round, where will the sum hit zero? (the film pauses here)
Right there. At a right angle, the sum is zero. That sum is called the dot product. It is big when two arrows point the same way, zero when they meet at a right angle, and below zero when they point apart.
Length, and the cosine
The dot product has a catch. Watch what happens if I push the second crate twice as far out, in the same direction.
What happens to the dot product? (the film pauses here)
It doubles. Nothing about the direction changed, but the score did.
So divide by the two lengths. What is left depends only on the angle. It is the cosine of the angle between the arrows: one when they point the same way, zero at a right angle, minus one when they point opposite. It always stays between minus one and one.
Thirty-two directions
My chart has two directions, east and north. A list can be as long as you like.
In the little language model from the attention lesson, every word starts as a list of 32 numbers. That makes each word a place in a space with 32 directions.
Nobody can picture 32 directions. The arithmetic doesn't care. The dot product is still multiply and add, now 32 times over, and the cosine is still a cosine.
Where the words live
Nobody set those numbers by hand. Training moved them, a little at a time, until the model got better at guessing the next word.
So look at who ended up close to whom. The nearest neighbours of kettle are map, shell and crate. Tallow's are rerun, pylon and map.
Across all 46 words, 80 percent have a nearest neighbour of the same kind as themselves: names next to names, things next to things, doing words next to doing words. Shuffle which word owns which list, and it drops to about 23 percent. Before training it was 9 percent.
Trained on hundreds of millions of words, the same idea goes much further. In 2013, researchers reported word lists where king, minus man, plus woman, landed closest to queen, once the three words in the question were left out of the search.
My cone
And then there's my cone.
A cone is cargo. It should sit with the crates. Its nearest neighbours are who, stood and mended.
In our sentences a cone only ever turns up in one phrase: put a cone on. Nothing there marks it as cargo, so the model had no reason to park it with the crates. Where it did park it is something we measured. Why there, we can only guess.
Closeness in these lists comes from how words are used in the training sentences, not from what they mean. Here, with 46 words, you can read every sentence the model learned from.
A flat picture of a big space
One warning about pictures, this lesson's included.
To lay the words out on my flat chart, I squash 32 directions down to the two that keep the most spread. Those two keep 31 percent of it.
On the flat chart, only 52 percent of the words sit nearest to a word of the same kind. In all 32 directions it was 80. Flattening throws a lot of closeness away.
Goodnight
A list of numbers is a place. The dot product mixes how much two places point the same way with how far out they are, and the cosine keeps only the direction.
Next along the coast is my crane. It moves every place on the chart at once, and that is a matrix. 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 vector is an ordered list of numbers; in two dimensions it can be drawn as a point or as an arrow from the origin (section 1.1). Vectors add component by component, and the sum is the arrow reached by following one arrow and then the other; multiplying by a number scales the length, and a negative number reverses the direction (section 1.1). The dot product multiplies matching components and adds them; it is zero exactly when the vectors are perpendicular (section 1.2). The dot product divided by both lengths is the cosine of the angle between the vectors, and its size never exceeds 1 (the Schwarz inequality, section 1.2).
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 (seed 7, 2,000 steps), read by tools/school/fill_vectors.mjs; categories fixed in system/school/vectors.mjs before any measurement. https://github.com/HarperZ9/HarperZ9.github.io/tree/main/media/school/vectors
Used for: Every word in the model starts as a list of 32 numbers (the embedding table, 46 words by 32). Nearest neighbours are by cosine. The share of words whose nearest neighbour is of the same kind (guide name, noun, verb, adjective, function word, punctuation) is measured on the trained table, on the table before training, and against 2,000 shuffles of which word owns which list. The word cone appears in the training sentences only in the phrase 'pylon put a cone on the' (system/school/corpus.mjs). The flat chart is the first two principal components of the 46 lists (mean subtracted), computed by power iteration; same-kind neighbours on it are measured by distance on the chart, as a viewer would judge them.
Does not prove: That word lists in large models are organised the same way, or that same-kind neighbours mean the model understands the words. Our categories are grammatical slots in a toy language, and slots are exactly what next-word training rewards.
Mikolov and others 2013, Efficient Estimation of Word Representations in Vector Space. Mikolov, T., Chen, K., Corrado, G., Dean, J. (2013). Efficient Estimation of Word Representations in Vector Space. arXiv:1301.3781. https://arxiv.org/abs/1301.3781
Used for: Word vectors trained by predicting words from their neighbours place similar words close together. The paper reports that vector('King') - vector('Man') + vector('Woman') is closest to vector('Queen'), citing earlier work (its reference [20]) whose vectors were trained on about 320 million words; the search for the nearest vector leaves out the question words. The paper's own models were trained on a Google News corpus of about 6 billion tokens.
Does not prove: That every analogy works, or that the offsets are exact; the paper reports accuracy on a test set, with many failures.
Jolliffe and Cadima 2016, Principal component analysis: a review and recent developments. Jolliffe, I. T., Cadima, J. (2016). Principal component analysis: a review and recent developments. Philosophical Transactions of the Royal Society A 374: 20150202. https://doi.org/10.1098/rsta.2015.0202
Used for: Principal components are the directions that keep the most variance of a data set; the first two give the best two-dimensional linear view in that sense, and the share of variance they keep says how much of the spread a flat picture retains.
Does not prove: That distances in the flat picture match distances in the full space; a projection can bring far points together.
What this lesson leaves out
The words here live in a tiny model trained on a made-up language of 46 words. Its neighbours track the job a word does in those sentences, which is exactly what next-word training rewards; they do not show that the model understands anything. The kinds of word (names, things, doing words and so on) were set by us before measuring, and other choices would give other numbers.
The flat chart throws most of the space away: its two directions keep about a third of the spread. Any flat picture of a word space, including the ones in this lesson, should be read with that in mind.
Next along the hill: Matrices move space (F2).