Lessons › Chapter 11: Transformers and LLMs
Multi-head attention
One attention calculation, called one attention head, tends to focus on one kind of relationship. This head links the pronoun “it” to the noun it refers to.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
Many links at once
Pip must keep track of who did what, to which account, and when, all at once.
One head, one kind of link
One attention calculation, called one attention head, tends to focus on one kind of relationship. This head links the pronoun “it” to the noun it refers to.
A second head
A second head, with its own query, key and value weights, learns something different: linking subjects to their verbs.
A third head
A third simply looks at the previous word, which is useful for grammar and phrases. Big models run dozens of heads in every layer.
Combine the heads
All the heads run side by side and their results are combined. Each word ends up informed by many kinds of relationships at once.
In real life: a team of editors
A good publication has one editor checking grammar, another checking facts, another checking tone, all reading the same article. Multi-head attention gives a model many such readers, each noticing different things, in every layer.
Quick check: Why use several attention heads?
- Each can focus on a different kind of relationship (correct)
- To make training slower
- To avoid needing data
Separate heads with separate weights learn different patterns, which are then combined.
Pseudocode
heads = 8 separate attention calculations each head has its own query, key and value weights head 1 might track: which noun a pronoun refers to head 2 might track: which verb goes with a subject head 3 might track: the word just before output = combine(all heads)
Key terms
- attention head
- One complete attention calculation. Models run many in parallel, each spotting different relationships.
In the news
Recent models and papers that build on this idea: