← Pytte

How a language model works

Inside Pytte

Type a sentence. Everything further down is what the model actually computes for that sentence, in your browser, while you watch. Pytte only knows Swedish: the sentence below means “Alma lives in a small”.

1

The text becomes pieces

The model does not see letters. It sees tokens: pieces of words that each have a number. Common words are one piece, rare ones are split into several. The space belongs to the word after it.

First in the row is a start token. It is put before every story during training and means “a new text begins here”.

2

Each piece becomes a row of numbers

Each token is swapped for its row in a table. The row is the model's description of what the token means. Here are the rows for your sentence, one cell per number: blue is negative, orange positive.

negativepositive

The numbers say nothing one by one. But tokens that are used in similar ways get rows that point in similar directions. Click a token above and the page finds the ones closest to it in the table.

Closest in the table

The number is the similarity between the rows, where 1 is the same direction and 0 is no similarity at all. Nobody told the model which words belong together. It worked that out from the stories.

3

The pieces look at each other

This is attention, the only place where tokens exchange information. Pick a token. The colour of the others shows how much the chosen one looks at them. It can only look backwards, never ahead.

Layer
Head

Every layer has several heads that look for different things at the same time. Change layer and head and watch the pattern change. Some look mostly at the word just before, some at the start token, some far back in the sentence.

All heads at once, for the chosen token

One row per layer, one group per head, one cell per token in the sentence. A darker cell means more attention. Click a group to select it.

I have not investigated what a particular head is “looking for”. The picture shows where it looks, not why.

Two helper parts show on the map. Normalisation keeps the numbers at a sensible size before every step. The skip connection means that what is computed is added to what came in, instead of replacing it. Each layer then only has to learn a change.

4

The answer takes shape, layer by layer

After attention each token is processed on its own in a small network, the feed-forward part. Then the next layer does the same two things again. The table shows two things for the last token of the sentence: how much each layer changed, and what the model would have guessed had it stopped there.

change from attentionchange from feed-forwardgreen guess = the one that wins in the end

The guess in the middle of the model comes from sending the intermediate result straight to the last step. The model is not trained to answer that way, so read the early rows as a hint. The last row is the model's real answer. The first row is almost always the same token once more: without any layers there is nothing to go on but the token itself.

5

Chance picks the next piece

What comes out is not a word but a probability for every one of the model's tokens. Then one of them is drawn. Two sliders control the draw. Move them and see what happens to the odds.

Low temperature makes the likely ones even more likely, and the story becomes predictable. High temperature evens out the odds, and the story gets wilder. Top-p removes the least likely ones altogether, so that one really unlucky draw does not ruin the sentence. Every time you draw a token it is added to the sentence, and everything on this page is computed again.

6

How it learned

At the start every number in the model is random. Training is the same path as above, millions of times: the model guesses the next token in a real story, is shown the right answer, and every number is nudged a little in the direction that would have given a better guess.

I retrained the smallest model that writes coherently, with one million numbers, and saved what it wrote along the way. Move the slider. The texts are in Swedish; watch them go from noise to sentences.

The loss measures how surprised the model is by the right answer. For a pure guess among 4,096 tokens it is 8.3. In the samples here full stops appear after five seconds and whole sentences after ten. After a little over a minute the text is about Alma. The rest of the time goes to the hard part: holding a whole story together.