Scaled dot-product attention
Each token scores every other token, turns the scores into weights that add up to 1, and takes the weighted average of their value vectors.
Read it step by step, with tokens:
- is an table: how well each token's query matches each token's key. (In cross-attention the keys come from a different sequence of length , and the table is .)
- Divide by so the scores stay in a range where softmax still responds to differences.
- Add the mask to block positions a token must not see.
- Softmax each row, so each token's weights add up to 1.
- Multiply by : each token gets a weighted blend of the value vectors.
Where the course uses it
- 6.2 Scaled Dot-Product Attention, by Hand · Module 6, Attentionfirst use
- 6.5 Attention Cannot See Word Order · Module 6, Attention
- 6.6 Multi-Head Attention · Module 6, Attention
- 8.4 Decoder-Only: Predicting the Next Token · Module 8, Three Families: Encoder, Encoder-Decoder, Decoder
- 26.1 Connecting Vision to Language · Module 26, Vision-Language Models
Related terms
- Query, key, valueThree learned versions of each token's vector. The query is what the token is looking for, the key is what it offers to others, and the value is what it passes on. W^Q is read "W Q", the matrix that makes queries.
- SoftmaxRead "softmax of z". It turns any list of numbers into probabilities: every output is positive and they add up to 1. Bigger inputs get bigger shares.
- The 1/√d_k scalingRead "one over root d k". Attention divides its scores by the square root of the head width so they keep a spread of about 1, however wide the head is.
- M — the attention maskA matrix of 0s and −∞ (minus infinity) added to the attention scores before softmax. A 0 leaves a position allowed; −∞ blocks it.