← CS Lab AI & Machine Learning / Multi-Head Attention Open standalone ↗

Multi-head attention runs several attention layers in parallel and stitches the results together. Self-attention lets a token look at every other token through one lens; multi-head gives it many lenses at once — and the tool shows it: same sentence, switch heads, watch a different pattern light up.

Where you meet it

What it does

One attention head can only express one way of relating tokens. Language relates them in many ways simultaneously — a verb to its subject, a pronoun to its antecedent, a word to its neighbour. Multi-head attention runs h heads side by side, lets each learn its own relationship, then combines them so the next layer sees all those views at once.

One head can express exactly one way words relate. Splitting d into many heads buys many relationships at once — roughly for the price of one.

How it works

Self-attention computes softmax(QKᵀ/√dₖ)·V for one set of queries, keys and values. Multi-head does this h times, each head with its own learned projections Wᵢ^Q, Wᵢ^K, Wᵢ^V that map the model dimension d down to d/h. The heads run in parallel, their outputs are concatenated back to width d, and a final projection W^O mixes them.

MultiHead(Q,K,V) = concat(head₁, …, headₕ) · W^O

head_i = Attention(Q·Wᵢ^Q, K·Wᵢ^K, V·Wᵢ^V)
       = softmax( (Q·Wᵢ^Q)(K·Wᵢ^K)ᵀ / √(d/h) ) · (V·Wᵢ^V)

Because each head projects into a different subspace, head 1 can specialise on the previous word while head 3 links subject and verb. The original paper uses h = 8 heads with d = 512, so each head works in 64 dimensions.

Watch out

  • d must be divisible by h — the model dimension is split evenly, so d/h has to be a whole number.
  • Per-head dimension shrinks to d/h, so the total compute stays close to a single full-width head — you get many views roughly for the price of one.
  • More heads is not automatically better; past a point extra heads add cost without adding signal.
  • Heads are often redundant. Studies show many can be pruned after training with little loss in quality — so a busy attention map doesn't mean every head is essential.

Go deeper

Multi-Head Attention rechnet mehrere Attention-Schichten parallel und fügt die Ergebnisse zusammen. Self-Attention lässt ein Token alle anderen durch eine Linse betrachten; Multi-Head gibt ihm viele Linsen gleichzeitig — genau das zeigt das Tool: gleicher Satz, Head wechseln, ein anderes Muster leuchtet auf.

Wo es vorkommt

Was es tut

Ein einzelner Attention-Head kann nur eine Art ausdrücken, Token in Beziehung zu setzen. Sprache verknüpft sie gleichzeitig auf viele Arten — ein Verb mit seinem Subjekt, ein Pronomen mit seinem Bezugswort, ein Wort mit seinem Nachbarn. Multi-Head Attention rechnet h Heads nebeneinander, lässt jeden eine eigene Beziehung lernen und kombiniert sie, sodass die nächste Schicht alle diese Sichten auf einmal sieht.

Ein Head drückt genau eine Art von Wortbeziehung aus. d auf viele Heads aufzuteilen kauft viele Beziehungen gleichzeitig — fast zum Preis von einer.

Wie es funktioniert

Self-Attention berechnet softmax(QKᵀ/√dₖ)·V für ein Set aus Queries, Keys und Values. Multi-Head tut das h-mal, jeder Head mit eigenen gelernten Projektionen Wᵢ^Q, Wᵢ^K, Wᵢ^V, die die Modell-Dimension d auf d/h herunterprojizieren. Die Heads laufen parallel, ihre Outputs werden zurück auf Breite d konkateniert, und eine finale Projektion W^O mischt sie.

MultiHead(Q,K,V) = concat(head₁, …, headₕ) · W^O

head_i = Attention(Q·Wᵢ^Q, K·Wᵢ^K, V·Wᵢ^V)
       = softmax( (Q·Wᵢ^Q)(K·Wᵢ^K)ᵀ / √(d/h) ) · (V·Wᵢ^V)

Weil jeder Head in einen anderen Unterraum projiziert, kann sich Head 1 auf das vorige Wort spezialisieren, während Head 3 Subjekt und Verb verbindet. Das Original-Paper nutzt h = 8 Heads bei d = 512 — jeder Head arbeitet also in 64 Dimensionen.

Worauf achten

  • d muss durch h teilbar sein — die Modell-Dimension wird gleichmäßig aufgeteilt, also muss d/h ganzzahlig sein.
  • Die Dimension pro Head schrumpft auf d/h, daher bleibt der Gesamtaufwand nahe an einem einzelnen Head voller Breite — viele Sichten praktisch zum Preis von einer.
  • Mehr Heads ist nicht automatisch besser; ab einem Punkt kosten zusätzliche Heads, ohne Signal hinzuzufügen.
  • Heads sind oft redundant. Studien zeigen, dass viele nach dem Training mit kaum Qualitätsverlust geprunt werden können — eine volle Attention-Map heißt also nicht, dass jeder Head nötig ist.

Mehr dazu

Next up
AI & Machine LearningPositional Encoding AI & Machine LearningBPE Tokenization All concepts →