liveBook · Manning

Query

As explained in Learn Generative AI with PyTorch

An attention query is a vector representation of the information sought by an input position, compared with key representations to determine which information to attend to.

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

How Learn Generative AI with PyTorch covers it

How deep it goes
Traces the query from an input-embedding projection through query-key scoring, softmax weights, and value combination.
Examples
Uses a library search for “machine learning in finance” to illustrate queries, keys, and values.
What it assumes
Introduces tokens, embeddings, and positional encoding before using input embeddings to form queries.
What you can do after
Understand how a query helps determine attention weights over values in a sequence.
How it connects
Connects queries to keys, values, self-attention, softmax, and Transformer models.

Also explained in

Compare how 7 books explain query →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.