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

Scaled Dot-Product Attention 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 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

equation

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

equation

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

The scaling factor 1dk\frac{1}{\sqrt{d_k}} is crucial. For large values of dkd_k, the dot products grow large in magnitude, pushing the softmax function into regions with extremely small gradients. Scaling by dk\sqrt{d_k} counteracts this effect.

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

equation

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

Let's perform a deterministic toy calculation. Suppose we have:

  • Q=(1.00.0)Q = \begin{pmatrix} 1.0 & 0.0 \end{pmatrix} (shape: 1×21 \times 2)
  • K=(1.00.00.01.0)K = \begin{pmatrix} 1.0 & 0.0 \\ 0.0 & 1.0 \end{pmatrix} (shape: 2×22 \times 2)
  • V=(2.03.04.05.0)V = \begin{pmatrix} 2.0 & 3.0 \\ 4.0 & 5.0 \end{pmatrix} (shape: 2×22 \times 2)
  • dk=2d_k = 2, so dk=21.41421356\sqrt{d_k} = \sqrt{2} \approx 1.41421356
  1. Compute QKTQ K^T: QKT=(1.00.0)(1.00.00.01.0)=(1.00.0)Q K^T = \begin{pmatrix} 1.0 & 0.0 \end{pmatrix} \begin{pmatrix} 1.0 & 0.0 \\ 0.0 & 1.0 \end{pmatrix} = \begin{pmatrix} 1.0 & 0.0 \end{pmatrix}

  2. Scale by dk\sqrt{d_k}: QKTdk=(1.020.02)(0.707106780.0)\frac{Q K^T}{\sqrt{d_k}} = \begin{pmatrix} \frac{1.0}{\sqrt{2}} & \frac{0.0}{\sqrt{2}} \end{pmatrix} \approx \begin{pmatrix} 0.70710678 & 0.0 \end{pmatrix}

  3. Apply Softmax: softmax((0.707106780.0))=(e0.70710678e0.70710678+e0e0e0.70710678+e0)(0.669741490.33025851)\text{softmax}(\begin{pmatrix} 0.70710678 & 0.0 \end{pmatrix}) = \begin{pmatrix} \frac{e^{0.70710678}}{e^{0.70710678} + e^0} & \frac{e^0}{e^{0.70710678} + e^0} \end{pmatrix} \approx \begin{pmatrix} 0.66974149 & 0.33025851 \end{pmatrix}

  4. Multiply by VV: Attention(Q,K,V)=(0.669741490.33025851)(2.03.04.05.0)(2.660517023.66051702)\text{Attention}(Q,K,V) = \begin{pmatrix} 0.66974149 & 0.33025851 \end{pmatrix} \begin{pmatrix} 2.0 & 3.0 \\ 4.0 & 5.0 \end{pmatrix} \approx \begin{pmatrix} 2.66051702 & 3.66051702 \end{pmatrix}

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

equation

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 · (n_q, d_k)
KK
Key matrix · (n_k, d_k)
VV
Value matrix · (n_k, d_v)
dkd_k
Dimensionality of keys · scalar
AttentionAttention
Attention output matrix · (n_q, d_v)