Query, Key, Value › Summary

Michael BrenndoerferUpdated June 4, 20252 min read
Language AI HandbookAll chapters
Part 13 of 67Self-Attention
Chapter 91Query, Key, Value › Summary
Reading section 10 of 11 · Summary · 2 minSection 10 of 11 · 2 min

Book contents

Section 10 of 11 · 2 min read

Query, Key, Value › SummaryOne section · Sections in this chapter10 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Query, Key, Value projections transform self-attention from a fixed similarity computation into a learned matching mechanism. By giving each token separate representations for different roles, the model can learn arbitrary attention patterns that capture linguistic relationships.

Key takeaways from this chapter:

  • Three roles, one mechanism: Queries express what a token seeks, keys advertise what a token offers, and values provide what gets aggregated. This separation enables flexible, learned attention patterns.

  • Projection matrices are learned: WQ\mathbf{W}_Q, WK\mathbf{W}_K, and WV\mathbf{W}_V are the trainable parameters. During training, they discover which aspects of embeddings matter for matching and aggregation.

  • Query-key matching determines attention: The dot product QKT\mathbf{Q}\mathbf{K}^T computes compatibility scores. High scores mean a query matches a key well, leading to high attention weight.

  • Scaling prevents gradient issues: Dividing by dk\sqrt{d_k} keeps score magnitudes stable regardless of dimension. This ensures softmax operates in a regime with healthy gradients.

  • Values carry the content: Attention weights determine how much each position contributes, but value vectors determine what content flows. A token can have a key that attracts attention while its value transmits different information.

  • Dimensions have meaning: Query and key dimensions must match (dkd_k) for dot product compatibility. Value dimension (dvd_v) determines output size. In practice, dk=dvd_k = d_v for simplicity.

In the next chapter, we'll explore multi-head attention, which runs multiple QKV attention operations in parallel. This allows the model to capture different types of relationships simultaneously, dramatically increasing the expressiveness of the attention mechanism.