Query
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.
Build a DeepSeek Model (From Scratch)
A query vector is an attention representation that specifies what information a token seeks from other tokens.
- How it is used
- Uses queries to select token information for context vectors and motivate efficient autoregressive inference.
- How deep it goes
- Works through projection shapes, attention scores, causal weights, and new-token computation, including a compressed key-value variant.
- Examples
- Traces “The next day is” through a four-token projection and computes attention for the newly appended token “bright.”
- What you can do after
- Understand how to trace a query from an embedding projection through attention scores to a context vector.
Read how Build a DeepSeek Model (From Scratch) explains query →
Timeless Algorithms, The Foundational Ideas
In an attention mechanism, a projected representation of an element that is compared with keys to determine which information to attend to.
- How it is used
- Uses queries to construct context through weighted relationships between tokens.
- How deep it goes
- Works through projections, dot products, scaling, softmax, and weighted values, while simplifying the behavior of a complete transformer.
- Examples
- Illustrates queries with pronoun resolution, semantic association, factual recall, and autoregressive generation.
- What you can do after
- Evaluate how a query affects attention weights and the resulting contextual representation in a simplified example.
Read how Timeless Algorithms, The Foundational Ideas explains query →
Transformers in Action
In attention, a query is a vector representation that specifies the information for which relevance is being evaluated.
- How it is used
- Uses queries to determine which input positions contribute to each attention output.
- How deep it goes
- Explains query roles and scaled dot-product computation, with simplified single-head and multihead implementations.
- Examples
- Illustrates a query with “The movie was not bad” and computes attention over four toy embeddings.
- What you can do after
- Understand how query–key comparisons determine the weights used to combine values.
Build a Text-to-Image Generator (from Scratch)
A query is an attention vector representing the information whose relevance to sequence elements is being evaluated.
- How it is used
- Uses queries to evaluate relationships among sequence elements in translation, vision transformers, and diffusion-model attention.
- How deep it goes
- Explains projections, scaled dot products, softmax, heads, and value aggregation, then stops short of training details.
- Examples
- Uses German-to-English translation, vision features such as texture and edges, and attention inside a denoising U-Net.
- What you can do after
- Build it from scratch in PyTorch by projecting inputs into queries, computing attention scores, and aggregating values.
Read how Build a Text-to-Image Generator (from Scratch) explains query →
Sutskever's List
An attention projection representing the information a sequence position seeks from other positions.
- How it is used
- Guides cross-attention by selecting which encoded input representations should influence each decoding step.
- How deep it goes
- Explains the query’s functional role in scoring relevance, without deriving its numerical construction.
- Examples
- Uses decoder cross-attention, encoded input representations, relevance scores, and context-guided token generation.
- What you can do after
- Evaluate how decoder positions select relevant encoded information during Transformer cross-attention.
Rearchitecting LLMs
A query vector is an attention representation used to determine how strongly a position should attend to available keys.
- How it is used
- Uses a token’s query to determine how strongly it attends to information represented by other tokens’ keys and values.
- How deep it goes
- Works through query-key dot products and softmax-weighted values without detailing the query projection’s implementation.
- Examples
- Uses the query for “spring” searching for contextual clues, including alignment with the key for “mountain.”
- What you can do after
- Understand how query-key scores determine the contributions of values to a token’s enriched embedding.
Learn Generative AI with PyTorch
- How it is used
- Uses a position’s query to score its relevance to sequence elements and weight their values in self-attention.
- 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 you can do after
- Build self-attention by projecting input embeddings into query, key, and value vectors and calculating their weighted output.
CUDA for LLMs
A query is an attention representation compared with keys to determine attention scores.
- How it is used
- Determines how strongly each token position attends to others by scoring its query against keys.
- How deep it goes
- Traces query projection through scaled dot-product attention and the resulting value-weighted output.
- Examples
- Uses a PyTorch attention head with query and key projections, causal masking, and a 16 × 64 × 128 model configuration.
- What you can do after
- Apply it via PyTorch to compute query-key attention scores and produce value-weighted outputs.
Build AI Drug Discovery Pipelines
A query vector is an attention representation that specifies the information sought from a set of keys and values.
- How it is used
- Guides a decoder toward encoder information relevant to its current prediction step.
- How deep it goes
- Explains query-key scoring, softmax weighting, and value aggregation, without detailing a specific scoring formula.
- Examples
- Uses decoder steps and a cross-attention diagram for protein-sequence information retrieval.
- What you can do after
- Understand how a decoder selects targeted encoder information through query-key comparisons and weighted value aggregation.
Applied Reinforcement Learning
A query is an attention vector representing the information an input element seeks from other elements.
- How it is used
- Represents the information a zone seeks from other zones when coordinating policy decisions.
- How deep it goes
- Defines queries as transformed zone representations and explains their dot-product comparison with keys, without detailing vector construction.
- Examples
- Uses zones seeking relevant information from other zones to illustrate query-key comparisons.
- What you can do after
- Understand how query-key similarity determines which zones contribute information to a zone’s output.
Deep Learning with PyTorch, Second Edition
A learned projection of an input position used to score how relevant other positions are in attention.
- How it is used
- Uses queries to identify which sequence positions deserve focus during self-attention and contextual text generation.
- How deep it goes
- Explains query projections, query-key dot products, softmax weighting, and value aggregation, without detailing training or parameter learning.
- Examples
- Demonstrates query projections and attention scoring with a two-position, three-dimensional PyTorch tensor.
- What you can do after
- Build it from scratch by projecting inputs into queries and using them to compute normalized attention-weighted outputs.
Read how Deep Learning with PyTorch, Second Edition explains query →
Machines that Think
A query is an attention vector representing the relationship sought by a sequence element when retrieving information from other elements.
- How it is used
- Represents what a token seeks from its sequence context when attention retrieves relevant information.
- How deep it goes
- Traces learned projections and query-key dot-product comparisons, without detailing numerical calculations or projection training.
- Examples
- Uses “rises” seeking useful context from “sun” and a decoder seeking “east” from the input representations.
- What you can do after
- Understand how queries match keys to identify relevant information in self-attention and cross-attention.
Deep Learning with Python, Third Edition
- How it is used
- Uses a target-position representation to score source keys and select information from associated values during decoding.
- How deep it goes
- Explains query-key scoring, value aggregation, learned projections, and a photo-search analogy, with code for parameterized attention.
- Examples
- Uses photo search to illustrate queries, keys, and values, and relates queries to target words during decoding.
- What you can do after
- Build it from scratch using Dense projections, dot-product scores, softmax weighting, and value aggregation.
Read how Deep Learning with Python, Third Edition explains query →
How Large Language Models Work
An attention query is a vector representing the information sought when an attention operation compares sequence elements.
- How it is used
- Uses a query to identify relevant information during attention in transformer layers.
- How deep it goes
- Defines query vectors and traces fuzzy matching, but stops short of deriving the attention equation.
- Examples
- Uses a Python dictionary analogy and next-token prediction with the current token as the query.
- What you can do after
- Evaluate how attention selects relevant preceding information using approximate query-key matches.
Also covered in
- Build a Large Language Model (From Scratch)
- MongoDB 8.0 in Action, Third Edition
- Understanding AIIn early access (MEAP)
- Grokking AI Algorithms, Second Edition
- LLMs in Production
What to know first
- transformer architectureA transformer is a neural-network architecture that uses attention-based interactions and position-wise transformations to model relationships among elements of sequences or other structured inputs.
- embeddingAn embedding is a numerical vector representation of an item in a feature space, encoding selected properties or relationships for computational tasks such as comparison or retrieval.
- softmaxSoftmax is a function that exponentiates each value in a score vector and normalizes the results to produce positive values summing to one, forming a probability distribution.
- linear layerA linear layer is a neural-network layer that applies a learned affine transformation to its input, typically using weights and a bias.
Related concepts
- self-attentionused by · Self-attention is an attention mechanism that relates positions within a sequence, allowing each position to incorporate information from other positions in that sequence.
- attentionused by · Attention is a neural-network mechanism that computes input-dependent relevance weights to combine information from representations for a context-dependent computation.
- inner productuses · An inner product is a scalar-valued operation on two vectors that generalizes the dot product, which for real coordinate vectors is the sum of products of corresponding components.
- Rotary Position Embeddingused by · Rotary position embedding is a positional encoding method that incorporates position information by applying position-dependent rotations to components of attention query and key representations.
- scaled-dot product attentionused by · Scaled dot-product attention computes query–key dot products, scales them by the square root of the key dimension, applies softmax to obtain weights, and uses those weights to combine value vectors.
- attention headproduced by · An attention head is an independently parameterized attention computation that uses queries, keys, and values to weight and combine information across input elements.
Questions about query
What is query?
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.
Which Manning books explain query?
Build a DeepSeek Model (From Scratch), Timeless Algorithms, The Foundational Ideas, Transformers in Action, Build a Text-to-Image Generator (from Scratch), Sutskever's List, Rearchitecting LLMs, Learn Generative AI with PyTorch, CUDA for LLMs and 6 more.
What should I know before learning query?
Manning books that teach query usually introduce transformer architecture, embedding, softmax and linear layer first.
What is query also called?
Query is also called Q.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.













