Skip to main content
Open In Colab Attention divides its scores by dk\sqrt{d_k}, where dkd_k is the dimension of the query and key vectors of one head. In standard equal-width multi-head attention dk=d/Hd_k = d/H, not the model width dd. The reason is the variance of a dot product. Assume the components of a query qq and a key kk are independent, with mean 0 and variance 1, an idealized initialization model, not a guarantee for learned projections. Each product qℓkℓq_\ell k_\ell then has mean 0 and variance 1, and the score is a sum of dkd_k such terms: s=q⋅k=∑ℓ=1dkqℓkℓ,E[s]=0,Var(s)=dk.s = q \cdot k = \sum_{\ell=1}^{d_k} q_\ell k_\ell, \qquad \mathbb{E}[s] = 0, \qquad \mathrm{Var}(s) = d_k . The scores spread out in proportion to dk\sqrt{d_k}. Dividing by dk\sqrt{d_k} brings the variance back to 1 for any head size: Var ⁣(q⋅kdk)=1.\mathrm{Var}\!\left(\frac{q \cdot k}{\sqrt{d_k}}\right) = 1 . More generally, if the independent zero-mean components have variances σq2\sigma_q^2 and σk2\sigma_k^2, then Var⁡(qℓkℓ)=E[qℓ2]E[kℓ2]=σq2σk2\operatorname{Var}(q_\ell k_\ell)=\mathbb E[q_\ell^2]\mathbb E[k_\ell^2]=\sigma_q^2\sigma_k^2. Assuming independence across components, Var⁡(q⋅k)=dkσq2σk2\operatorname{Var}(q\cdot k)=d_k\sigma_q^2\sigma_k^2. For equal variance σ2\sigma^2, this is dkσ4d_k\sigma^4. Scaling removes the factor dkd_k, not the dependence on component variance. Correlated components or queries and keys need not obey this formula exactly. The next cell checks both variances by sampling 10,000 query and key pairs for each head size.
Output from cell 2 Large score differences can make softmax concentrate on one key. Its Jacobian is J=diag⁡(a)−aa⊤J=\operatorname{diag}(a)-aa^\top, where aa is the attention-weight vector. When aa approaches a one-hot vector, this Jacobian approaches zero. This can weaken gradients through the query-key matching path, but does not imply that all model gradients vanish. The experiment below uses one query and 16 keys with dk=64d_k = 64. It multiplies the scaled scores by a factor cc and tracks the largest attention weight and the norm of the Jacobian. Unscaled scores are dk\sqrt{d_k} times larger than scaled ones, so c=1c = 1 is scaled attention and c=dk=8c = \sqrt{d_k} = 8 is unscaled attention.
Output from cell 4 Output from cell 5 The Jacobian norm peaks at a moderate spread of the weights, between c=3c = 3 and c=4c = 4, and is small at both ends, for different reasons. When the weights are nearly equal (small cc), each entry of JJ is of order 1/T1/T, so the norm stays near 0.24 but does not vanish. Past the peak the norm falls fast as one weight approaches 1: about 0.25 at c=8c = 8, 0.05 at c=16c = 16, and close to zero beyond c=32c = 32. Unscaled scores correspond to c=dkc = \sqrt{d_k}, so these three points are unscaled heads with dk=64d_k = 64, 256 and 1024. The larger the head, the further unscaled attention sits in the region where the gradient through the softmax vanishes. Scaling keeps c=1c = 1 for every head size.

PyTorch reference

Reference: Vaswani et al., Attention Is All You Need, Section 3.2.1.
Found an error on this page? Report an issue.