Curious Pandas

Attention, without the maths first

Attention lets every word decide which earlier words matter to it. Click through a sentence and watch "it" work out who was hungry.

15 min+35 XP for finishing

Read this: the panda ate the bamboo because it was hungry. Who was hungry? The panda, obviously. Now: the panda ate the bamboo because it was tasty. Now “it” is the bamboo.

You resolved “it” by looking back at the earlier words and deciding which one mattered. Attention is a way for a model to do exactly that, and it’s the core of the transformer, the architecture behind nearly every large language model today. The 2017 paper that introduced it was titled, boldly, Attention Is All You Need. Its model was “based solely on attention mechanisms, dispensing with recurrence and convolutions entirely”. It set new translation records and trained faster than the methods it replaced.

How a word “looks back”

Every token gets three small lists of numbers:

  • a query: what am I looking for?
  • a key: what do I offer?
  • a value: what I’ll pass along if you pick me.

To decide where a token looks, the model compares its query with every earlier token’s key. Similar means a high score. The scores go through a softmax so they add up to 100%. Then the token takes a weighted blend of everyone’s values, pulling most from whoever scored highest.

Who is "it"? Watch the attention weightshand-set numbers, real arithmetic

Click a word to see where it looks. Each word can only look at itself and the words before it.

The whole attention matrix (each row sums to 100%)

Things to notice:

  • Click it. The weight splits evenly between panda and bamboo. At that point in the sentence nothing has decided the question yet, so the honest answer is “one of these two”.
  • Click the last word. hungry looks back at panda. Switch the sentence and tasty looks back at bamboo. The word that settles the question is the one that does the looking.
  • Words can only look left. Language models write one token at a time, so a token is never allowed to peek at what comes after it. That’s the striped half of the matrix.
  • Sharpness scales the scores before the softmax: higher makes a word commit to one target, lower spreads its attention out. It’s the same idea as temperature in the previous lesson.
Check yourself+10 XP

In a GPT-style model, which tokens can a given token attend to?

Check yourself+10 XP

Why do the attention weights for one token always add up to 100%?

The rest of a transformer, in one paragraph

A transformer stacks this many times. In each layer, attention lets every token gather information from earlier tokens, and then a small neural network (the kind from Why hidden layers exist) processes each token’s blended result on its own. Early layers tend to pick up local patterns such as grammar. Later ones build on those. After the last layer, the final token’s numbers are turned into a probability for every possible next token: the step you played with in One token at a time.

Sources

Finished reading?

Missions and quizzes above earn their own XP. This marks the lesson done.