Why Self-Attention Divides Dot Products by the Square Root of Key Dimension
Last updated: November 29, 2026
Quick Overview
In scaled dot-product attention, scores are computed as $QK^T / \sqrt{d_k}$. Explain mathematically why the scaling is there and what happens without it.
0
0
0 solved
In scaled dot-product attention, scores are computed as $QK^T / \sqrt{d_k}$. Explain mathematically why the scaling is there and what happens without it.
Google invented the Transformer, and interviewers for Gemini, Search, and Ads modeling roles often probe whether candidates understand the mechanics rather than just the API. This checks variance reasoning and how softmax saturation affects gradients.
What the Interviewer Expects
- Derive the variance of a dot product of random vectors
- Explain softmax saturation and vanishing gradients
- Connect the fix to keeping logits at unit scale
- Mention alternatives such as QK normalization or temperature
- Note interaction with initialization and dimension size
Key Topics to Cover
How to Approach This
- Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
- Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
- Feature engineering is often more impactful than model selection.
- Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
- Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
- Would the scaling still matter if Q and K were layer-normalized?
- Why not divide by $d_k$ instead of $\sqrt{d_k}$?
- How does attention logit growth cause instability in large-model training?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Explore ML Interview PrepSample Answer
Clarify
Assume query and key components are roughly independent with mean 0 and variance 1 at initialization, which is what standard init and layer norm aim for....
Approach
Compute the variance of a raw dot product as a function of , then show what large-variance logits do to softmax and its gradient....