AI Research Scientist · Attention Mechanisms & Kernels
Self-attention mechanism (QKV)
Self-attention mechanism (QKV)
Query, Key, and Value vectors
For each token, three separate learned weight matrices , , project its embedding into a query vector , a key vector , and a value vector — three distinct roles computed from the same input.Computing Raw Attention Scores
- 1Take the queryStart with the query vector for the position asking what it should attend to.
- 2Dot against every keyCompute for every key vector in the sequence, including itself.
- 3Stack into a score vectorCollect the results into , one raw score per key position.
- 4Read scores as relevanceLarger dot products mean higher raw relevance between that key and the query, before any scaling or normalization is applied.
- 1Dot product: , .WhyMeasures raw alignment between the query and each key vector.
- 2Scale by , giving scores and .WhyKeeps score variance stable so softmax doesn’t saturate as dimension grows.
- 3Softmax: , , weights and .WhyConverts the scaled scores into a normalized probability distribution usable as attention weights.
Recall check from the same lesson
If the sqrt(d_k) scaling step is skipped, larger key dimensions make raw dot-product scores spread out more, pushing softmax toward a near one-hot output and shrinking the gradients that flow back through the attention weights.
Review the explanation
Answer: True. Without dividing by sqrt(d_k), dot-product magnitude grows with dimension, widening the raw score spread. Softmax over widely spread scores concentrates almost all probability mass on one key, and in that saturated regime softmax's gradients become nearly zero, starving the learning signal — exactly why the scaling step exists.
Sources
One sitting · 20–30 minutes
A focused session on your AI Research Scientist interview
LearnBench starts from what you already know — skip what you have, master what you’re missing.
Start now