Multi-Head Attention
Runs several attention operations in parallel, each focusing on something different.
The idea
Instead of one attention pattern, the model projects the input into several smaller subspaces and attends in each independently. One head may track the previous word, another a subject-verb link, another a distant reference. The heads' outputs are concatenated and mixed by a final linear layer.
MultiHead = Concat(head₁ … head_h)·W_O
Watch it run
This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.
- positive
- negative
- attention weight
Take the sentence. We will compute what the word "cat" (highlighted) should pay attention to.
The vectors here are random, so the pattern of attention is arbitrary. In a trained model these weights are learned. Pick another query word with the slider.
Step by step
- 1Split the model dimension into h heads with their own Q, K, V projections.
- 2Run scaled dot-product attention in every head.
- 3Concatenate the heads.
- 4Apply an output projection.
Where it is used
All transformer models.
Watch out for
Some heads turn out to be redundant and can be pruned.