1dk\frac{1}{\sqrt{d_k}}

The 1/√d_k scaling

Read "one over root d k". Attention divides its scores by the square root of the head width so they keep a spread of about 1, however wide the head is.

Suppose the entries of q\mathbf{q} and k\mathbf{k} are independent, with mean 0 and variance 1. Their dot product adds dkd_k such products, so its variance is dkd_k and its standard deviation is dk\sqrt{d_k}. At dk=64d_k = 64 that is 8. Scores that far apart push softmax to its extremes: one weight near 1 and the rest near 0. There its slope, of the form p(1−p)p(1-p), is close to zero, so learning stalls.

Dividing by dk\sqrt{d_k} brings the variance back to 1, keeping the weights spread out and the gradients useful.

Example

dk=64:Var⁡(q⊤k)=64  ⟹  Var⁡ ⁣(q⊤k8)=6464=1d_k = 64: \quad \operatorname{Var}(\mathbf{q}^\top \mathbf{k}) = 64 \;\Longrightarrow\; \operatorname{Var}\!\left(\tfrac{\mathbf{q}^\top \mathbf{k}}{8}\right) = \tfrac{64}{64} = 1

Dividing by √64 = 8 divides the variance by 8² = 64.

Where the course uses it

Related terms