What is attention in a transformer?
Attention is the part of a transformer that lets each token decide which earlier tokens matter to it. It scores every earlier token, turns the scores into weights that add up to 100%, and takes in a weighted mix of their information. That is how “it” finds “the cat”, and how “bank” knows it means a river.
▶ Try itWhy it matters
A word alone is often ambiguous. Attention is how a language model works out what “it”, “bank” or “too big” refers to, by letting every word borrow meaning from the words before it.
In 6 minutes you'll be able to
- Attention lets each word look back and borrow meaning from the words that matter to it.
- The weights always add up to 100%, so more focus on one word means less on the others.
- Change one word and the attention shifts, which is why context changes what a model understands.
What you'll practise
Be the attention head
Tap earlier words to pour attention onto them. Spend all 10 drops, then compare with a trained head. The weights are illustrative.
The cat sat because it
Who is “it”?
Trained head: cat 64% · sat 18% · because 12%
Reads as: it = the cat “It” has to find the thing it stands for. The trained head puts most of its weight on “cat”, the only candidate that can sit.
We had a picnic on the river bank
Which “bank”: money or river?
Trained head: river 66% · picnic 18% · on 6%
Reads as: bank = the side of a river On its own, “bank” could mean money. Leaning on “river”, with a little help from “picnic”, pulls its meaning to the water's edge.
The vase won't fit in the box . Too big
What is too big?
Trained head: vase 56% · fit 14% · box 13%
Reads as: too big = the vase Something too big can't go inside. To make sense of “big”, the head looks back at the vase, the thing that has to fit.
The vase won't fit in the box . Too small
And now: what is too small?
Trained head: box 54% · fit 14% · vase 13%
Reads as: too small = the box One word flipped the meaning, and the weights flipped with it. Attention is worked out fresh for every text, it isn't a fixed lookup table.
Inside one attention head
Tap each card. Every token carries three small lists of numbers.
- 🔦 Query: what am I looking for?
- The token that is looking sends out a query, like “I'm a pronoun, I need a noun that can get tired”.
- 🏷️ Key: what do I offer?
- Every earlier token holds up a key, a label of what it is. The better a key matches the query, the higher its score.
- 📦 Value: what do I pass on?
- Scores become weights that add up to 100%. The query then takes in a weighted mix of the earlier tokens' values: mostly “cat”, a little of the rest.
- 🧠 Why many heads?
- Each layer runs many heads side by side. One might link pronouns to nouns, another track the word just before, another match up brackets.
What can a head look for?
Researchers peek inside trained models. Which of these patterns could an attention head pick up? Choose all that apply.
- Which noun a pronoun like “she” refers to
- The word right before the current one
- The opening bracket that a “)” closes
- The subject that a verb belongs to
- Words that will come later in the reply
- Whether the sentence is true in the real world
Remember
Every token spreads 100% of its attention over earlier tokens and takes in a mix of what they mean. Context changes the mix.
Each word spreads 100% of its attention over earlier words and takes in a mix of what they mean.
Key facts
- The transformer, the design behind today's large language models, was introduced in a 2017 paper titled “Attention Is All You Need”. · Source: Vaswani et al., Google, 2017
- The original transformer ran 8 attention heads side by side in each layer. · Source: Vaswani et al., Attention Is All You Need, 2017
- GPT-3 has 96 layers with 96 attention heads in each. · Source: Brown et al., Language Models are Few-Shot Learners, OpenAI, 2020
- When researchers inspected a trained language model, they found individual heads that link pronouns to the nouns they refer to and verbs to their objects, without ever being taught grammar. · Source: Clark et al., What Does BERT Look At?, 2019
Updated:
Questions people ask
What is self-attention in simple terms?
Self-attention lets every word in a text look at the other words in the same text and decide how much each one matters to it. The word then takes in a weighted mix of their meaning.
What are query, key and value?
Three small lists of numbers each token carries. The query says what a token is looking for, the key says what it offers, and the value is what it passes on once it's picked.
What is multi-head attention?
Running several attention heads in parallel in the same layer. Each head learns to look for a different kind of relationship, and their results are combined.
Can attention look at future words?
Not in a model that writes text left to right. Each token may only attend to itself and earlier tokens, because the later ones haven't been written yet.
Related terms
