Gradient LabStart the lesson

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 lesson

Step by step

Story

Many links at once

Pip must keep track of who did what, to which account, and when, all at once.

Concept

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.

Build

A second head

A second head, with its own query, key and value weights, learns something different: linking subjects to their verbs.

Build

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.

Key idea

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.

Real life

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?

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: