EasyDeepLearn

Why is attention scaled by 1/sqrt(dk)1 / \mathrm{sqrt}(d_{k})?

hard

Answer

  • Assume q, k have entries with mean 0 and variance 1 (post-init or post-normalization).
  • Their dot product has variance dkd_{k}.
  • As dkd_{k} grows (say, 64+), un-scaled dot products become large in magnitude, pushing softmax into the saturated regime where one weight is ~1 and the rest ~0 → tiny gradients w.r.t. the losing keys.
  • Dividing by sqrt(dk)\mathrm{sqrt}(d_{k}) rescales the variance back to 1 and keeps softmax gradients healthy.
Check yourself — multiple choice
  • Scaling reduces expressiveness
  • Dot product has variance dkd_{k} under standard init → 1/√dkd_{k} rescales to variance 1 → healthy softmax gradients
  • Purely for numerical reasons
  • Any scale works

1/√dkd_{k} keeps dot-product variance ~1 → softmax stays in the linear regime.

#attention#transformers

Practise Deep Learning

214 interview questions in this topic.

Related questions