You are reading immutable version 28. 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

Scaled Dot-Product Attention

Scaled Dot-Product Attention

Scaled Dot-Product Attention

Source equation

Attention(Q,K,V)=softmax(QKTdk)V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V

The Scaled Dot-Product Attention mechanism computes attention weights by taking the dot product of queries with keys, scaling them by the square root of the key dimension, applying a softmax function, and using the resulting weights to compute a weighted sum of the values.

Sources

S3.E1

Attention​(Q,K,V)=softmax​(Q​KTdk)​V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V (1)
\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V

Attention(Q,K,V)=softmax(QKTdk)V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V

Sources

S3.E1

Attention​(Q,K,V)=softmax​(Q​KTdk)​V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V (1)
\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V
Deep dive

To compute the attention matrix:

  1. Compute the dot products of the queries QQ and keys KK by calculating QKTQ K^T.
  2. Scale the dot products by dividing each element by dk\sqrt{d_k} to prevent extremely large values that lead to vanishing gradients in the softmax function.
  3. Apply the softmax function row-wise to obtain the attention weights.
  4. Multiply the attention weights by the values VV to produce the final output matrix.
Sources

S3.E1

Attention​(Q,K,V)=softmax​(Q​KTdk)​V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V (1)
\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V
Implementation detail

Consider a simplified deterministic example with dk=4d_k = 4 (so dk=2\sqrt{d_k} = 2). Let the query matrix QQ and key matrix KK be:

Q=(2020),K=(2020)Q = \begin{pmatrix} 2 & 0 & 2 & 0 \end{pmatrix}, \quad K = \begin{pmatrix} 2 & 0 & 2 & 0 \end{pmatrix}

Then, the dot product is:

QKT=(22+00+22+00)=(8)Q K^T = \begin{pmatrix} 2 \cdot 2 + 0 \cdot 0 + 2 \cdot 2 + 0 \cdot 0 \end{pmatrix} = \begin{pmatrix} 8 \end{pmatrix}

Scaling by dk=2\sqrt{d_k} = 2 yields:

QKTdk=82=4\frac{Q K^T}{\sqrt{d_k}} = \frac{8}{2} = 4

Applying the softmax (which is trivial for a single element, yielding 1.01.0) and multiplying by a value vector V=(5)V = \begin{pmatrix} 5 \end{pmatrix} results in:

Attention(Q,K,V)=1.05=5\mathrm{Attention}(Q,K,V) = 1.0 \cdot 5 = 5

Sources

S3.E1

Attention​(Q,K,V)=softmax​(Q​KTdk)​V\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V (1)
\mathrm{Attention}(Q,K,V)=\mathrm{softmax}(\frac{QK^{T}}{\sqrt{d_{k}}})V
QQ
Query matrix of shape (d_seq, d_k) · [d_seq, d_k]
KK
Key matrix of shape (d_seq, d_k) · [d_seq, d_k]
VV
Value matrix of shape (d_seq, d_v) · [d_seq, d_v]
dkd_k
Dimension of the keys · scalar
softmaxsoftmax
Softmax activation function applied row-wise · function