Representations

What are embeddings?

An embedding is the list of numbers a language model uses in place of a word. Watch a token id open a drawer in the embedding table, see similar words share similar numbers and fall into groups in 3D, measure the angle between two words, and do sums with meaning.

Word
Compare with
Numbers per row 8
Loading 3D view

Why numbersA model cannot do arithmetic on words, so, before it does anything else, every single token is swapped for a list of numbers, hundreds or thousands long, called an embedding.

Narrated with James Phoenix's AI voice.

5822
Token id
8
Numbers per row
0.79
Cosine king · queen
38°
Angle
prince0.99
Nearest to king

king is token 5822, so the table hands over row 5822: 8 numbers. Against queen the rows point 38 degrees apart, a cosine of 0.79, which is close. Of every word here, prince is nearest to king at 0.99.

A language model never sees a word. The text is first cut into tokens, each token is replaced by a number from the vocabulary, and each number is then replaced by a list of numbers called an embedding. Everything the model does after that, every attention head and every layer, is arithmetic on those lists. This film follows one token through that last swap and shows what the numbers mean.

The embedding table is the cabinet. It has one row for every token the model knows, about fifty thousand for GPT-2 and well over a hundred thousand for recent models, and each row holds the same count of numbers. The token id is simply the row number, so the lookup is a drawer sliding out. Real models store hundreds or thousands of numbers per row; the slider lets you see eight, sixteen, twenty-four or thirty-two of them, and the film prints the values when there are few enough to read.

Nobody writes those numbers by hand. They start random and training nudges them, billions of times, towards whatever helps the model predict the next token. Words that appear in similar places end up with similar rows, because the model gains nothing by telling them apart. That is why king and queen share most of their pattern on the board, lit in gold, while an animal or a city looks nothing like either.

Plot every row as a point and those shared patterns become places. The scatter beside the bench squeezes each row down to three numbers, keeping the three directions along which the groups differ most, and the words fall into groups: royalty, animals, places, money and water. A picture like this is a map of the groups, not a ruler. Squeezing throws most of each row away, so two points that look close may not be, which is why similarity is always measured on the full rows. The scatter also draws each group spread to the same size so every label reads; where the groups sit relative to each other, and where bank sits between money and water, is the projection's.

Similarity has a precise meaning here. Each row is an arrow in a space with as many directions as there are numbers, and two words are alike when their arrows point the same way. The cosine of the angle between them is the standard measure: close to one for close relatives, close to zero for words with nothing in common, and negative when the arrows point apart. The dial draws the true angle, because it is drawn in the flat plane the two rows share.

Because meaning is stored as direction, you can do arithmetic with it. Take the row for king, subtract the row for man, add the row for woman, and the result lands nearer to queen than to any other word. The famous demonstration came from word2vec in 2013; the same kind of structure shows up in the input embeddings of large language models, though it is messier there than in a tidy example.

There is one thing the table cannot do. The row for bank is the same whether the sentence is about a river or about money, because a lookup has no idea what surrounds it. Telling the two apart is the job of the layers that come next, which read the whole sentence and blend neighbouring words into each position. The film shows the direction of that effect with a simple blend, first on the board and then in the scatter, where the two banks drift apart, one towards the water and one towards the money; in a real model it takes many layers of attention to get there.

The honest caveat: the vectors on this page are hand-built so the effects are clean. Each word is written as a mix of a few concepts and then rotated into unlabelled numbers, which is roughly how learned embeddings behave but far tidier. Real rows are longer, noisier, and no single number means anything on its own; meaning lives in directions spread across all of them. The token ids are illustrative GPT-2 style values.

The maths

The lookup
e  =  E[id]Rd\mathbf{e} \;=\; E[\,\text{id}\,] \in \mathbb{R}^{d}

E is the embedding table: one row per token in the vocabulary, d numbers per row. Looking up a token is reading one row, which is the drawer sliding out. There is no arithmetic in it, which is why the same token always gets the same row.

Cosine similarity
cosθ  =  abab\cos\theta \;=\; \frac{\mathbf{a}\cdot\mathbf{b}}{\lVert\mathbf{a}\rVert\,\lVert\mathbf{b}\rVert}

Multiply the two rows number by number, add up, and divide by both lengths. The result depends only on the angle between the rows, which is what the dial draws: close to one when two words point the same way, close to zero when they have nothing in common, negative when they point apart.

Arithmetic with meaning
ekingeman+ewoman    equeen\mathbf{e}_{\text{king}} - \mathbf{e}_{\text{man}} + \mathbf{e}_{\text{woman}} \;\approx\; \mathbf{e}_{\text{queen}}

Subtracting and adding rows moves along directions that training has tied to meanings. The result is not exactly any row; it is nearest to queen, which is what the fourth row and the dial show. The famous version came from word2vec; large language models keep similar structure in their input embeddings.

Context comes later
hi  =  f(e1,,en)i\mathbf{h}_i \;=\; f\big(\mathbf{e}_1, \ldots, \mathbf{e}_n\big)_i

The table row for bank is identical in every sentence. The vector that ends up meaning riverbank or high-street bank is h, the output of the layers that read the whole sentence. The film shows the direction of that change with a simple blend; a real model gets there through many layers of attention.

Related terms

More visualisations

Building with language models?

These explainers come out of the work. If you want the same thinking applied to your own system, that is what I do.

See how I can help