Why do we scale the dot product attention by √dₖ in the Transformer architecture?
Scale the dot product attention by √dₖ in the Transformer architecture explains why Transformer attention divides the query key dot product by the square root of the key dimension $\sqrt{d_k}$. This keeps logits at a stable scale so softmax gradients behave and training is stable across different head sizes. Learn the variance argument, a worked numeric example, and practical implications for multi head design and initialization.

TL;DR
- Scaling the dot product attention by keeps the logits from growing with the key dimension , which stabilizes the softmax and its gradients.
- Without scaling, larger makes logits numerically large and softmax becomes extremely peaky, hurting learning.
- Dividing by normalizes the variance of the dot product so heads behave consistently across sizes. Key tradeoffs: smaller logits improve gradient flow and stability but change the effective temperature of attention; choose head dimension and scaling together.
In this question, we will learn why the Transformer architecture divides the raw query key dot product by before applying softmax. We will show the statistical reason, a concrete numeric example, and how this ties into multi head design and initialization.
We will cover the following:
- The intuition
- How it actually works (variance argument plus a worked example)
- Relation to multi head attention and initialization
- Tradeoffs and failure modes
- Questions the interviewer might ask
Direct answer: We scale by to keep the dot-product logits at a consistent variance so the softmax is not driven into saturation and gradients remain well behaved. This normalization makes attention heads stable across different and simplifies optimization.
The intuition (an analogy that makes it click)
Imagine you are comparing similarity scores from longer and shorter lists. If longer lists produce sums of many small contributions, their totals will typically be larger just because there are more terms. The dot product of two dimensional vectors is the sum of componentwise products. As grows the raw sum tends to grow in magnitude, so we rescale by to restore a fair comparison. That makes the softmax see scores on a similar scale whether you have small or large head dimensions.
How it actually works (the real mechanics, with one concrete worked example)
Assume each component of query and key vectors is independent with mean zero and variance . The dot product is a sum of such products. For zero mean and independent components the expected mean is zero and the variance grows roughly linearly with .
A compact expression when components have variance is:
If we divide the dot product by the variance becomes , independent of . That keeps the distribution of logits stable as we change head size.
Worked numeric example. Suppose a head produces raw dot products (logits) for three keys as [16, 8, 4]. Those are already somewhat large relative to 1, so softmax will be very peaky. Now scale by , assume , to get [2, 1, 0.5]. Compare their softmax probabilities.
| score | softmax prob (unscaled) | score/\sqrt{d_k} | softmax prob (scaled) |
|---|---|---|---|
| 16 | 0.99966 | 2 | 0.628 |
| 8 | 0.00033 | 1 | 0.231 |
| 4 | 0.00001 | 0.5 | 0.140 |
The unscaled logits produce an almost one-hot distribution that kills gradient flow to the lower-scoring keys. After scaling we get a smoother distribution that allows learning to adjust weights for multiple keys.
Relation to multi head attention and initialization
Multi head attention splits the model dimension into multiple heads, each with dimension . If we did not scale, heads with larger would produce logits with larger variance. Scaling by makes head outputs comparable and avoids having some heads be always dominant because of dimension alone.
Scaling also interacts with parameter initialization. Standard initializations aim to keep activations and gradients roughly constant with depth. The factor is consistent with that goal: it normalizes the attention logits so the softmax output variance does not blow up as changes.
Tradeoffs and failure modes
Scaling by is simple and effective, but it implicitly sets an attention temperature. Smaller effective temperature leads to sharper attention; larger temperature gives smoother attention. If you change or add explicit temperature parameters you must be careful to maintain the intended sharpness of attention.
Questions the interviewer might ask
Some follow-up questions you might get:
Why assume zero mean and independence for components? Those assumptions are a simplifying model that captures the main effect: sums of many small independent terms have variance that grows with count. Real activations are not independent, but the scaling still empirically helps.
Could we learn the temperature instead of fixing ? Yes, some variants learn a scalar temperature or add a learned scale per head. That adds flexibility but also more parameters and potential instability without careful regularization.
Why use not or another factor? The dot product variance grows linearly in , so the standard deviation grows with . Dividing by would shrink too fast; dividing by normalizes variance to a constant.
Does layer normalization change the need for scaling? Layer normalization helps stabilize activations but does not remove the variance growth of the dot product between independent vectors. Scaling remains useful even with normalization layers.
How does scaling affect attention heads when d_k is small? When is small scaling is less dramatic, but the factor still keeps behavior consistent across designs. With very small the division can increase noise sensitivity, so design choices should balance head count and head size.
Some things to note:
- The factor is a variance normalization choice, not a magic constant. It follows from basic statistics of sums.
- Empirically this scaling improves training stability across many Transformer models.
What the interviewer is really testing
They want to know you understand why raw dot products change scale with vector dimension and how that affects the softmax and gradients. They also want to see you can connect a statistical argument to practical architectural choices like multi head design and initialization. A concise variance argument plus a numerical example shows you grasp both theory and practice.
Related questions
How would you rate the quality of this article?
Prepare for your AI engineering interview
This is one of many detailed questions and explainers on StudyAIDesign. Browse the full set, work through the curriculum, and walk into your interview ready.