XII.2AttentionPro

Scaled dot-product and multi-head attention

The transformer’s attention fits on one line, softmax(QKᵀ/√dₖ)V. Here is every symbol and every shape, the reason for the square root, how masks decide who may see whom, why there are many heads — and what it all costs.

50 min

After this lesson you can

  • Compute softmax(QKᵀ/√dₖ)V by hand on a small example and state the shape of every intermediate matrix
  • Derive why the variance of a dot product grows with dₖ and explain how that saturates the softmax
  • Apply causal and padding masks, and distinguish self-attention from cross-attention
  • Implement multi-head attention from scratch and count its parameters

In this lesson

  1. Why get rid of the recurrence?
  2. Three roles for every token
  3. The formula, decoded
  4. Why divide by the square root of the key size?
  5. Masks: who may see whom
  6. Self-attention and cross-attention
  7. Many heads
  8. The code
  9. What it costs

This lesson is part of Descent Pro

Descent Pro unlocks every lesson on the route, from base camp to the summit. Every plan starts with a free trial.

Descent Pro

Start your free trial to keep reading

This lesson, and every other lesson on the route, is part of Descent Pro. Every plan starts with a free trial, and students with a .edu email get longer. Cancel before the trial ends and you pay nothing.