The attention mechanism
How a word decides which other words matter to it: a query scored against keys, softmax turning scores into shares, and a weighted blend of values. One tiny example, every number worked.
In 30 seconds
Attention lets each word in a text gather information from the others. The word sends out a query; every word offers a key to be matched against it and a value to hand over.
Matching the query with each key gives a score. Softmax turns the scores into weights that add up to 1, and the output is the values blended by those weights.
Because every word is scored against every other directly, a word far back counts as easily as a neighbour. That is why attention handles long-range context well.
The attention mechanism, step by step
Real transformers use vectors with hundreds of numbers, divide the scores by √d before softmax, run several attention “heads” side by side, and add position information separately.
- Step 1: A word needs its context. Read “The cat sat on the mat. It purred.” The word “It” means nothing alone: a model has to pull in meaning from elsewhere in the text, and from the right place. Attention is the mechanism that decides, for each word, how much to take from every other word.
- Step 2: Query, key and value. Each word is given three small lists of numbers. The asking word uses its query. Every word it might look at offers a key, to be matched against the query, and a value, the content it will pass on if chosen. It works like a library search: the query is your request, keys are the labels, values are the books.
- Step 3: Score each key. To score a word, multiply the query and that word’s key position by position and add the results (a dot product). A key pointing the same way as the query scores high; one pointing elsewhere scores low. Here “cat” scores 2, “mat” 1 and “sat” 0.
- Step 4: Softmax: scores to shares. Raw scores are turned into shares of attention with softmax: raise e to each score, then divide by the total. Every weight comes out positive and together they add up to 1. Because of the exponent, a lead of one point in score becomes about 2.7 times the weight: “cat” gets 67%, “mat” 24%, “sat” 9%.
- Step 5: Blend the values. The output is a weighted sum: each value is scaled by its word’s weight, and the results are added. The new vector for “It” is two-thirds cat, with a little mat and sat mixed in. Nothing was looked up or picked outright; every word contributes in proportion to its weight.
- Step 6: Self-attention. In self-attention the queries, keys and values all come from the same sentence, and every word runs the calculation at the same time, each with its own query. A word’s own key is among the candidates too. Learned weight matrices produce the three vectors, so training decides what each word looks for.
- Step 7: Any distance, one hop. The score depends only on how well a query matches a key, not on how far apart the two words are. A word 21 places back is reached as directly as a neighbour, where a recurrent network would pass the information through every step between. The price is one score per pair of words, so the work grows with the square of the length.
Try it yourself
Learn this first
- VectorsArrows with a length and a direction: components, adding, scaling and unit vectors. Beginner
- Neural networksFollow numbers through one tiny network by hand, and see why the bend matters. Beginner
- Recurrent neural networks and LSTMsA network with a loop remembers a sequence, forgets too fast, and gains gates to fix it. Intermediate
Spotted a mistake on this page?