You are reading immutable version 39. The current guide may be newer.

Study the paper

Attention Is All You Need

Lessons, visuals, quizzes, flashcards, and resources—organized in teaching order.

All activities

Multi-Head Attention

Multi-Head Attention

Multi-Head Attention

Source equation

MultiHead(Q,K,V)=Concat(head1,...,headh)WO\displaystyle\mathrm{MultiHead}(Q,K,V)=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}

The Multi-Head Attention mechanism allows the model to jointly attend to information from different representation subspaces at different positions. Instead of performing a single attention function with dmodeld_{\text{model}}-dimensional queries, keys, and values, we project them hh times with different, learned linear projections.

Sources

equation

MultiHead​(Q,K,V)\displaystyle\mathrm{MultiHead}(Q,K,V) =Concat​(head1,…,headh)​WO\displaystyle=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}
\displaystyle\mathrm{MultiHead}(Q,K,V)=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}

MultiHead(Q,K,V)=Concat(head1,...,headh)WO\displaystyle\mathrm{MultiHead}(Q,K,V)=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}

Sources

equation

MultiHead​(Q,K,V)\displaystyle\mathrm{MultiHead}(Q,K,V) =Concat​(head1,…,headh)​WO\displaystyle=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}
\displaystyle\mathrm{MultiHead}(Q,K,V)=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}

Each head is computed as an independent scaled dot-product attention: headi=Attention(QWiQ,KWiK,VWiV)\mathrm{head}_i = \mathrm{Attention}(Q W_i^Q, K W_i^K, V W_i^V) where the projections are parameter matrices WiQRdmodel×dkW_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}, WiKRdmodel×dkW_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k}, WiVRdmodel×dvW_i^V \in \mathbb{R}^{d_{\text{model}} \times d_v}, and WORhdv×dmodelW^O \in \mathbb{R}^{h d_v \times d_{\text{model}}}.

Sources

equation

MultiHead​(Q,K,V)\displaystyle\mathrm{MultiHead}(Q,K,V) =Concat​(head1,…,headh)​WO\displaystyle=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}
\displaystyle\mathrm{MultiHead}(Q,K,V)=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}
Implementation detail

Illustrative Toy Calculation

Let us consider a simplified setup with h=2h = 2 heads, dv=2d_v = 2, and dmodel=4d_{\text{model}} = 4.

Suppose the outputs of the two heads for a single token are:

  • head1=[1.0,2.0]\mathrm{head}_1 = [1.0, 2.0]
  • head2=[3.0,4.0]\mathrm{head}_2 = [3.0, 4.0]
  1. Concatenation: Concat(head1,head2)=[1.0,2.0,3.0,4.0]\mathrm{Concat}(\mathrm{head}_1, \mathrm{head}_2) = [1.0, 2.0, 3.0, 4.0]

  2. Linear Projection: Let the output projection matrix WOW^O be a 4×44 \times 4 identity matrix: WO=(1000010000100001)W^O = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}

  3. Final Output: MultiHead(Q,K,V)=[1.0,2.0,3.0,4.0]×WO=[1.0,2.0,3.0,4.0]\mathrm{MultiHead}(Q, K, V) = [1.0, 2.0, 3.0, 4.0] \times W^O = [1.0, 2.0, 3.0, 4.0]

Sources

equation

MultiHead​(Q,K,V)\displaystyle\mathrm{MultiHead}(Q,K,V) =Concat​(head1,…,headh)​WO\displaystyle=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}
\displaystyle\mathrm{MultiHead}(Q,K,V)=\mathrm{Concat}(\mathrm{head_{1}},...,\mathrm{head_{h}})W^{O}
QQ
Query matrix containing representation vectors · Matrix of shape (N, d_model)
KK
Key matrix containing representation vectors · Matrix of shape (M, d_model)
VV
Value matrix containing representation vectors · Matrix of shape (M, d_model)
headihead_i
The output of the i-th attention head · Matrix of shape (N, d_v)
hh
Number of attention heads · Scalar integer
WOW^O
Learned output projection parameter matrix · Matrix of shape (h * d_v, d_model)
ConcatConcat
Concatenation operation along the feature dimension · Function mapping h matrices of shape (N, d_v) to a single matrix of shape (N, h * d_v)