Skip to content
StackDecode
AI & ML Atlas
Deep LearningtechniqueInteractive simulation

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.

thecatsatonthemat
  • positive
  • negative
  • attention weight

Take the sentence. We will compute what the word "cat" (highlighted) should pay attention to.

1/4

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

  1. 1Split the model dimension into h heads with their own Q, K, V projections.
  2. 2Run scaled dot-product attention in every head.
  3. 3Concatenate the heads.
  4. 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.

Related