In self-attention, a query is a learned projection of an input embedding for a sequence position. Compared with keys, it produces scores that determine how strongly that position attends to the values. section 9.1.1
Library search
Searching for “machine learning in finance” is the query; book titles and descriptions are keys, and recommended books are values. In attention, query-key similarity determines the weights applied 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.
Follow the diagram from input embedding X through the query, key, and value projections to attention scores, weights, and the resulting attention. section 9.1.1






