liveBook · Manning

Dot-product attention

As explained in Natural Language Processing in Action, Second Edition

Dot-product attention scores queries and keys with scalar products and uses those scores to weight values.

Dot-product attention turns relationships between sequence positions into weighted values. It projects embeddings into query, key, and value matrices, then compares queries with keys using scalar products. section 9.1.2

The computation proceeds in a compact matrix pipeline:

  • Create Q, K, and V through linear projections of the input embeddings. section 9.1.2
  • Multiply Q by K to score connections between words, scaling by 1/√dₖ for numerical stability. section 9.1.2
  • Apply softmax so scores are positive and sum to 1, then multiply them by V to produce weighted values. section 9.1.2

Attention as word connections

The Q × K product forms a square attention matrix that can be read as connections between words in an input sequence. section 9.1.2

Equation 9.1 Self-attention outputs
figure

Use the self-attention equation to inspect how scaled scores become weighted outputs. section 9.1.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains projections, scaling, softmax normalization, and weighted values, while stopping before multi-head attention details.
Examples
Uses a toy attention matrix to represent connections between words in an input sequence.
What it assumes
Assumes embedding vectors and matrix multiplication, while treating linear projection as a dot product or matrix multiplication.
What you can do after
Evaluate how scaled query-key scores become normalized weights and weighted value outputs.
How it connects
Connects linear projections, self-attention, softmax, embeddings, RNNs, and multi-head self-attention.

Also explained in

Compare how 2 books explain dot-product attention →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.