ViewWiki

Lesson 4 of 4

Attention in a language model.

How a language model reads and writes: tokens as numbers, an attention head choosing what to look at, copying a made-up word, and a wheel of next-token probabilities that the temperature sharpens or flattens.

Step 1 of 6 · Tokens

Step 1 of 6

Tokens

A large language model, like those behind chatbots, is a huge neural network with one task: take text in and predict the next token. It does that again and again, token after token, and so it writes. The whole model is neurons and weights, as in the last Lessons, only billions of them.

On the rack is an English sentence, a tile per token, with its number in the vocabulary. Behind each tile is a column of lit cells, the token’s vector. The numbers here are made up for illustration, but they work as in a real model.

To the question

Step 2 of 6

Attention

Before it predicts, each token gathers information from the tokens before it, through an attention head: it gives each earlier token a weight, the shares add up to 100%, and its vector is updated by their weighted blend. It cannot look at the tokens after it: they have not been written yet. The Attending token is raised, and smoked glass covers what comes after it. Predict, and then Look inside will draw arcs, as thick as the attention.

To the question

Step 3 of 6

Sharp attention

In most places attention spreads over several tokens. Change the Sentence and the Attending token, and look for a place where one token takes nearly all the attention.

To the question

Step 4 of 6

Copying

In the zorblat sentence the last token is zor, for the second time. Look where it is looking, and at what came after the first zor.

To the question

Step 5 of 6

The next token

After attention, the model gives each possible token a probability of coming next. The wheel at the end of the rack is divided by the probabilities, with one more hatched slice for the long tail of everything else. Then it samples: spins the wheel and picks. The Temperature decides how: it raises each probability to the power 1T\dfrac{1}{T}⁠ and rescales them. Predict what a low temperature will do.

To the question

Step 6 of 6

Temperature

At temperature 11⁠ the wheel is the model’s distribution as it is. Tune it until the leading candidate gets the chance in the question.

To the question

Interactive scene

Loading the interactive version…

Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.

To the experiment

Practice · Choose

What does a language model receive when it “reads” the sentence on the rack?

To the experiment

Practice · Predict, then look

After “The keys to the cabinet in the hall” the model has to choose between is and are. Which earlier token will hall give the most attention to?

To the experiment

Practice · Reach the target

Find a Sentence and an Attending token where the token gives about 80% of its attention to a single token, and press Check.

To the experiment

Practice · Choose

The word zorblat is made up, and no model met it in training. How does the model know that blat comes after the second zor?

To the experiment

Practice · Predict, then look

After “The Eiffel Tower is in the city of”, Paris has an 88% chance at temperature 1. What will happen to that chance when Temperature goes down to 0.2?

To the experiment

Practice · Reach the target

After “…in the hall” the leading candidate, are, gets 55%. Change Temperature until it gets about 70%, and press Check.