liveBook · Manning

Scaled-dot product attention

As explained in Natural Language Processing in Action, Second Edition

Scaled dot-product attention divides query-key dot products by the square root of the key dimension before normalization.

Scaled-dot product attention turns relationships between input embeddings into weighted context vectors. It projects embeddings into query, key, and value matrices, divides query-key products by √dₖ, normalizes them with softmax, and uses the scores to combine values. section 9.1.2

The scaling step matters because:

  • High-dimensional query-key products can produce small softmax gradients; multiplying by 1/√dₖ improves numerical stability. section 9.1.2
  • The softmax produces positive scores that sum to one, creating weights for the value vectors. section 9.1.2
  • The resulting Q × K matrix represents connections between words and supports efficient matrix-multiplication implementations. section 9.1.2
Equation 9.1 Self-attention outputs
figure

Use the self-attention equation to trace the scaling, normalization, and weighted-value steps together. section 9.1.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains projections, scaling, softmax normalization, weighted values, and matrix interpretation without expanding into implementation code.
Examples
Uses a toy encoder attention matrix to illustrate connections between words.
What it assumes
Assumes familiarity with embedding vectors, dot products, matrix multiplication, and softmax normalization.
What you can do after
Build it from scratch by forming scaled query-key scores, normalizing them, and applying the resulting weights to values.
How it connects
Connects query, key, value matrices with self-attention, softmax, embeddings, transformers, and matrix multiplication.

Also explained in

Compare how 4 books explain scaled-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.