Key
In an attention mechanism, a key is a vector representation of an input element that is compared with a query to determine the relevance of its associated value.
Build a DeepSeek Model (From Scratch)
A key is an attention representation against which a query is compared to determine the relevance of a sequence position.
- How it is used
- Uses keys to score token relevance and motivate reuse during autoregressive inference.
- How deep it goes
- Derives key projections, score matrix shapes, and cache requirements, then outlines grouped and latent alternatives.
- Examples
- Traces four-token matrix dimensions and the new-token query for “bright.”
- What you can do after
- Evaluate and understand how key reuse and sharing reduce repeated computation or cached matrices.
Read how Build a DeepSeek Model (From Scratch) explains key →
Timeless Algorithms, The Foundational Ideas
In an attention mechanism, a projected representation of an element that is compared with queries to determine relevance.
- How it is used
- Uses keys to identify relevant tokens when self-attention constructs contextual representations.
- How deep it goes
- Works through projection, dot products, scaling, softmax, and weighted values numerically, while simplifying the behavior of a complete transformer.
- Examples
- Uses pronoun resolution with a trophy and box, alongside semantic association, factual recall, and autoregressive generation.
- What you can do after
- Evaluate how query–key alignment affects attention weights and contextual representations in a simplified transformer.
Read how Timeless Algorithms, The Foundational Ideas explains key →
Transformers in Action
In attention, a key is a vector representation whose similarity to a query determines the relevance of its associated value.
- How it is used
- Uses keys to determine which values contribute to an attention output for a given query.
- How deep it goes
- Explains key projections and scaled dot-product scoring, with simplified attention and multihead attention implementations.
- Examples
- Uses a sentence about a movie and a four-embedding NumPy attention calculation.
- What you can do after
- Builds a simplified scaled dot-product attention calculation from scratch using projected query, key, and value matrices.
Build a Text-to-Image Generator (from Scratch)
A key is an attention vector representing information against which a query is compared to determine relevance.
- How it is used
- Uses keys to compare sequence elements and determine which values receive attention.
- How deep it goes
- Explains key projections, dot-product scoring, scaling, softmax, dimensions, and multi-head implementation.
- Examples
- Uses German-to-English translation, 48-dimensional vision tokens split into four heads, and a PyTorch attention-head listing.
- What you can do after
- Builds key projections and scaled query–key attention with PyTorch linear layers and softmax.
Read how Build a Text-to-Image Generator (from Scratch) explains key →
Sutskever's List
An attention projection representing the information a sequence position offers for comparison with queries.
- How it is used
- Represents encoder-side information that decoder queries compare against during cross-attention.
- How deep it goes
- Defines keys through their attention role and illustrates their interaction with queries, without deriving projection internals.
- Examples
- Uses a Transformer architecture schematic showing decoder queries interacting with encoder keys and values.
- What you can do after
- Understand how keys support relevance scoring and context construction during Transformer cross-attention.
Rearchitecting LLMs
A key vector is an attention representation matched against queries to determine attention weights.
- How it is used
- Matches each token’s key against a query to produce scores used as attention weights over values.
- How deep it goes
- Traces query-key dot products through softmax weighting of values and contrasts independent with shared key-value sets.
- Examples
- Uses “spring” in water-and-mountain and flowers-and-season contexts to illustrate contextual weighting.
- What you can do after
- Understand how query-key scores allocate value contributions and how grouped-query attention shares keys across query heads.
Deep Learning with R, Third Edition
A key vector is an attention representation used to compute compatibility with a query.
- How it is used
- Uses key vectors to score source information against queries and to cache past context during causal text generation.
- How deep it goes
- Explains key vectors through lookup, projection, dot-product scoring, causal masking, and caching, without deriving attention mathematically.
- Examples
- Illustrates key vectors through photo-tag retrieval, parameterized attention code, and cached key-value vectors in mini-GPT generation.
- What you can do after
- Apply key vectors via dot-product attention and implement key-value caching for efficient causal generation.
CUDA for LLMs
An attention key is a vector compared with a query to determine the relevance of an associated value.
- How it is used
- Determines how strongly each position’s value contributes by comparing its key with a query.
- How deep it goes
- Traces learned key projections into query-key dot products and value weighting, but does not detail how key vectors are formed.
- Examples
- Shows query-key dot products followed by value weighting in a transformer attention head.
- What you can do after
- Understand how a key contributes to attention scores and the weighted value output.
Learn Generative AI with PyTorch
The attention input that represents the searchable features used to match against a query.
- How it is used
- Uses searchable features to determine how strongly each sequence element relates to a query.
- How deep it goes
- Explains key construction, scaled query-key scoring, softmax weighting, and value combination without deriving the underlying neural layer.
- Examples
- Uses library search, English-to-French translation, token embeddings, and attention diagrams to illustrate keys.
- What you can do after
- Build it from scratch by implementing query-key-value attention for an English-to-French Transformer.
Build AI Drug Discovery Pipelines
A key vector is an attention representation compared with a query to determine the relevance of an associated value.
- How it is used
- Explains how cross-attention retrieves input-sequence information needed at each decoder step.
- How deep it goes
- Defines keys operationally through query comparisons and attention weights, but does not derive their mathematical scoring function.
- Examples
- Uses protein structure prediction, output position j, and nearby or distant three-dimensional interactions as attention examples.
- What you can do after
- Evaluate/understand how decoder states select relevant encoder positions through query-key comparisons.
Deep Learning with PyTorch, Second Edition
The attention component that represents what a position contains and can be matched against a query.
- How it is used
- Matches each position’s contents against a query so attention can select relevant information.
- How deep it goes
- Explains key vectors, dot-product scores, softmax weights, weighted values, and a matrix-based implementation.
- Examples
- Uses next-character prediction, search-result retrieval, and a PyTorch tensor example with shape [2, 3].
- What you can do after
- Apply it via dot-product attention to route more computation toward relevant positions.
Read how Deep Learning with PyTorch, Second Edition explains key →
Build a Large Language Model (From Scratch)
The attention-side vector associated with an input token and compared with a query to determine relevance.
- How it is used
- Uses keys in self-attention to compare transformed input tokens with a query and determine relevance.
- How deep it goes
- Traces key projection, tensor shapes, dot-product scoring, scaling, softmax, and value aggregation.
- Examples
- Uses a six-token sentence, a second-token query, W_key projection, and [6, 2] key tensors.
- What you can do after
- Build key tensors in PyTorch and use them in scaled dot-product attention computations.
Read how Build a Large Language Model (From Scratch) explains key →
Machines that Think
A key is an attention vector representing the properties by which a sequence element can be matched to a query.
- How it is used
- Uses keys to score which sequence tokens are relevant to a query within Transformer attention.
- How deep it goes
- Defines keys as learned projections and explains query-key dot-product scoring, but does not detail how the projections are trained.
- Examples
- Illustrates query-key matching with “rises” and gives “sun” a stronger match than “the.”
- What you can do after
- Understand how a query compares with keys to assign token relevance before values contribute information.
How Large Language Models Work
An attention key is a vector representing the attributes against which an attention query is matched.
- How it is used
- Matches query vectors against possible answers within transformer attention.
- How deep it goes
- Defines keys, relates them to values and queries, and stops short of detailed vector calculations.
- Examples
- Uses Python dictionary lookup and preceding words in next-token prediction as concrete examples.
- What you can do after
- Understand how attention uses similarity-weighted keys to select contextual information.
Understanding AI
A key vector is an attention vector that represents the information a token offers for matching against queries.
- How it is used
- Matches token queries against the information other tokens offer during attention.
- How deep it goes
- Explains key-query matching through dot products, softmax weights, and value aggregation.
- Examples
- Uses pronoun resolution in “The performance was unbelievable—the crowd couldn’t believe it” and an attention-weight figure.
- What you can do after
- Evaluate/understand how attention identifies relevant tokens by comparing queries with keys.
Also covered in
- Applied Reinforcement LearningIn 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.
- 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.
- recurrent neural networkA recurrent neural network is a neural network that processes ordered inputs while carrying and updating an internal state across successive steps.
- 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.
Related concepts
- 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.
- grouped-query attentionused by · Grouped-query attention is an attention mechanism in which multiple query heads share key and value heads or projections.
- 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.
- key up-projectorproduced by · A key up-projector is a learned linear transformation that reconstructs attention key vectors from a latent key-value representation.
- key-value cachepart of · A key-value cache stores attention keys and values computed for earlier tokens so they can be reused during subsequent autoregressive generation.
Questions about key
What is key?
In an attention mechanism, a key is a vector representation of an input element that is compared with a query to determine the relevance of its associated value.
Which Manning books explain key?
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, Deep Learning with R, Third Edition, CUDA for LLMs and 7 more.
What should I know before learning key?
Manning books that teach key usually introduce transformer architecture, softmax, recurrent neural network and embedding first.
What is key part of?
Key is part of key-value cache. A key-value cache stores attention keys and values computed for earlier tokens so they can be reused during subsequent autoregressive generation.
What is key also called?
Key is also called K.
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.














