A key is the attention input that represents searchable features for matching against a query. In self-attention, the model projects input embeddings through a neural layer with weights WK to produce key vectors K. section 9.1.1
Attention compares query and key vectors with a dot product. Larger matching scores produce larger attention weights after scaling and softmax, while the value vectors provide the information combined into the attention output. section 9.1.1 section 1.3.1
Within scaled dot-product attention, the key participates in this sequence:
- Project the input embedding through the WK neural layer to obtain K. section 9.1.1
- Compare Q with K using a dot product, then divide by the square root of the key dimension, dk. section 9.1.1
- Apply softmax to the scaled scores to obtain attention weights that describe relationships across sequence elements. section 9.1.1 section 1.3.1
Library search analogy
For a search such as “machine learning in finance,” the query is the search phrase, while book titles and descriptions act as keys. Items whose keys match the query receive higher scores; the retrieved books correspond to values. section 1.3.1
Figure 9.1 A diagram of the self-attention mechanism. To calculate attention, the input embedding X is first passed through three neural layers with weights, WQ, WK, and WV, respectively. The outputs are query Q, key K, and value V. The scaled attention score is the product of Q and K divided by the square root of the dimension of K, dk. We apply the softmax function on the scaled attention score to obtain the attention weight. The attention is the product of the attention weight and value V.
Use the diagram to trace how input embedding X becomes query Q, key K, and value V, then produces scaled scores, attention weights, and the final attention output. section 9.1.1







