liveBook · Manning

Query

As explained in Learn Generative AI with PyTorch

In an attention mechanism, a query is a vector representation of a sequence position that is compared with keys to determine relevance and weight associated values.

A query is a learned vector representation of a sequence position. In self-attention, it is compared with keys to score how relevant other positions are; the resulting attention weights combine their values. section 9.1.1 section 9.5

For an input embedding X, a neural layer produces Q. Comparing Q with K gives scaled attention scores; softmax turns these into weights, which are applied to V. This is scaled dot-product attention (SDPA). section 9.1.1 section 9.5

A library search

Searching for “machine learning in finance” supplies the query. Book titles and descriptions act as keys; books with closer matches receive higher scores and are recommended as 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.

Trace how input embeddings become queries, keys, and values, then how scores and weights produce attention. section 9.1.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Works through query-key scoring, scaling, softmax, and value weighting, then situates the vectors in multihead attention.
Examples
Uses a library search query and English-to-French Transformer translation to illustrate retrieval and sequence attention.
What it assumes
Assumes input embeddings as attention inputs and introduces tokenization, embeddings, and positional encoding in the sequence-modeling context.
What you can do after
Build self-attention by projecting input embeddings into query, key, and value vectors and calculating their weighted output.
How it connects
Connects queries to keys, values, self-attention, scaled dot-product attention, and Transformers.

Also explained in

Compare how 14 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.