Why scale attention scores?
Dot-product variance, softmax concentration, and the limits of the argument
This page is an advanced deep dive into scaled dot-product attention. It develops a question the main lesson covers in one sentence: why divide the scores by \(\sqrt{d_k}\), and how far does that justification hold once its assumptions are made explicit?
Practice: Attention exercises, including the score-variance derivation.
Dot-product scale under Gaussian assumptions
Under the simplifying assumption that all query and key components are independent, zero-mean, and unit-variance, their dot product has variance \(\sum_{i=1}^{d_k} \operatorname{Var}(q_i k_i) = d_k\) and standard deviation \(\sqrt{d_k}\): for \(d_k=64\), it is 8. Large differences between scores in a row can concentrate softmax and reduce its gradients. Dividing by \(\sqrt{d_k}\) reduces this risk in the simplified model; neither saturation nor its absence is guaranteed for learned projections.
Derive the variance, one step at a time
Write the dot product as \[ q\cdot k=\sum_{i=1}^{d_k}q_i k_i. \]
The assumptions give, for each dimension, \(\mathbb{E}[q_i]=\mathbb{E}[k_i]=0\), \(\mathbb{E}[q_i^2]=\mathbb{E}[k_i^2]=1\), and independence between \(q_i\) and \(k_i\). Therefore:
- Mean of one term: \(\mathbb{E}[q_i k_i]=\mathbb{E}[q_i]\mathbb{E}[k_i]=0\).
- Variance of one term: \(\operatorname{Var}(q_i k_i)=\mathbb{E}[q_i^2 k_i^2]=\mathbb{E}[q_i^2]\mathbb{E}[k_i^2]=1\).
- Variance of the sum: terms are independent across dimensions, so their covariances are zero and \(\operatorname{Var}(q\cdot k)=d_k\).
- After scaling: \(\operatorname{Var}((q\cdot k)/\sqrt{d_k})=d_k/d_k=1\).
The factor \(1/\sqrt{d_k}\) therefore stabilizes the variance scale in this model. It does not force every score to be close to zero or guarantee a particular softmax distribution.
| Dimension \(d_k\) | Raw theoretical variance | Raw standard deviation | Standard deviation after division by \(\sqrt{d_k}\) |
|---|---|---|---|
| 16 | 16 | 4 | 1 |
| 64 | 64 | 8 | 1 |
| 256 | 256 | 16 | 1 |
| 512 | 512 | \(\sqrt{512}\approx22.63\) | 1 |
Check before running the simulation: when the dimension is multiplied by four, is the raw standard deviation multiplied by four or by two? The table gives the theoretical prediction; the simulation below checks its scale on a finite sample.
NUMERICAL WORKBENCH · SCORE SCALE Python 3.12+ / NumPy
import numpy as np
### Simulate dot products for d_k = 64
np.random.seed(42)
d_k = 64
N = 10000
### Independent standard normal random vectors
q = np.random.randn(N, d_k)
k = np.random.randn(N, d_k)
### Raw dot products q . k
raw_dots = np.sum(q * k, axis=1)
### Compare raw vs scaled standard deviation
std_raw = np.std(raw_dots)
std_scaled = np.std(raw_dots / np.sqrt(d_k))
print(f"Raw std dev: {std_raw:.2f} (theoretical: {np.sqrt(d_k):.1f})")
print(f"Scaled std dev: {std_scaled:.2f} (theoretical: 1.0)")
assert np.isclose(std_scaled, 1.0, atol=0.05)What happens to the scaling factor as \(d_k\) grows?
Common Fallacy: “Increasing head dimension \(d_k\) automatically improves attention expressiveness with no drawbacks”
Limitation: Under the assumptions above and without scaling, increasing \(d_k\) from 64 to 512 multiplies score standard deviation by \(\sqrt{8} \approx 2.83\). Larger score differences can concentrate softmax and reduce its local derivatives; this does not prove inevitable saturation or a total halt to learning.
Connections to other concepts
- Scaled dot-product attention — the full scores → scaling → softmax → aggregation computation, of which this page develops a single step.
- Probabilities — softmax properties, in particular how sensitive the weights are to the spread of scores within a row.
References
- Vaswani et al. (2017), Attention Is All You Need — introduces the \(1/\sqrt{d_k}\) scaling factor and compares dot-product with additive attention.