liveBook · Manning

Key

As explained in Deep Learning with PyTorch, Second Edition

The attention component that represents what a position contains and can be matched against a query.

A key represents what each position contains that might match a query. Attention compares queries with keys to decide which values deserve more weight, much like matching search text against indexed pages. section 9.5

In dot-product self-attention, learned transformations produce query and key vectors. Their dot products become attention scores; softmax normalizes those scores into weights that determine how strongly each value contributes to the output. section 9.5.1

Computing attention from query and key vectors

For an input x with shape [2, 3], create query, key, and value matrices with linear transformations. Compute q @ k.T, apply softmax across the scores, and multiply the resulting weights by v. Positions whose queries match keys more closely contribute more strongly through their values. section 9.5.1

Figure 9.7 Self attention, step-by-step from input to output
figure

Follow the step-by-step flow from input positions through queries, keys, attention weights, and the output. section 9.5.1

How Deep Learning with PyTorch, Second Edition covers it

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 it assumes
Assumes familiarity with queries and values only at the level of their attention roles, while defining all three components together.
What you can do after
Apply it via dot-product attention to route more computation toward relevant positions.
How it connects
Connects keys with queries, values, attention, self-attention, cross-attention, dot products, and softmax.

Also explained in

Compare how 8 books explain key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.