Name the scalar that QK^T is divided by inside scaled dot-product attention.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
sqrt(d_k), the per-head key dimension. Not sqrt(d_model) and not sqrt(num_heads). It keeps pre-softmax score variance near 1.
Picture a dart competition where each round you sum up scores from many dartboards at once. The more boards you add, the wider the range of total scores swings, just from random luck. To keep the leaderboard meaningful round to round, you divide the total by something that scales with how many boards you summed over, so a typical total stays around the same size. Attention does the same thing: the dot product between query and key is a sum over d_k terms, and dividing by the square root of d_k keeps the typical magnitude steady regardless of how big you make a single head.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
State the divisor sqrt(d_k), derive it from the variance of a d_k-term dot product, contrast with wrong candidates (1, d_k, sqrt(d_model)), and explain why the scaling is invariant across head sizes.
| Candidate divisor | Effect on score variance | Softmax behavior |
|---|---|---|
| 1 (no scaling) | Variance grows as d_k | Saturates at large d_k, near one-hot, gradient vanishes off-peak |
| sqrt(d_k) | Variance ≈ 1 regardless of d_k | Stable across head sizes; the canonical choice |
| d_k | Variance ≈ 1/d_k | Over-corrects; softmax never sharpens, attention is near-uniform |
| sqrt(d_model) | Variance ≈ d_k / d_model (wrong scale) | Wrong magnitude per head; under-corrects for multi-head |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Writing sqrt(d_model) instead of sqrt(d_k). The scaling is per-head, so d_k = hidden_size / num_heads is the right value.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.