Part 4 · The Maths II: Inside the Transformer
Why the 1/√dk is not cosmetic
The scaling factor is the single most-copied and least-explained line in the architecture. It has a short derivation, and the derivation predicts exactly what the widget below shows.
Independence lets the expectation factor; each factor is zero.
Variances of independent terms add. So the logits have standard deviation √dk — they grow as the head gets wider.
It saturates. Softmax is scale-sensitive: multiply all logits by c > 1 and the distribution sharpens; as c → ∞ it becomes one-hot. With spread √dk, a dk = 128 head has logits several times larger than a dk = 8 head, purely from dimensionality.
p the softmax output · z its input logits · δij the Kronecker delta, 1 when i = j and 0 otherwise
When p is one-hot, every entry of that Jacobian is ~0: pi ≈ 0 kills most terms and pi ≈ 1 gives p(1−p) ≈ 0. No gradient flows back through a saturated softmax, so the attention pattern cannot be learned.
Dividing by √dk holds the entering variance at 1 for every head width, so the softmax operates in the same regime whether the head is 8 or 128 wide. This is exactly the justification given in the original paper.
This is not a reconstruction after the fact. The original paper gives the same argument in a footnote, suspecting that “for large values of dk, the dot products grow large in magnitude, pushing the softmax function into regions where it has extremely small gradients” — steps 2 through 4 above, in one sentence.