Even with gating, an RNN — short for Recurrent Neural Network — still processes a sequence strictly left to right, folding everything into one hidden state as it goes. Attention throws that constraint out: instead of compressing a sequence into one running summary, keep every token's vector around, and let a query look at all of them at once.
Three tokens sit at fixed points; a query (the ×) is what's currently "looking." Drag it around. The token whose vector points in a similar direction to the query gets more weight — dragging toward "cat" pulls almost all the attention onto "cat," dragging to the middle spreads it evenly across all three.
For a query and a set of token (key) vectors , attention computes a similarity score for each one, turns those scores into weights that sum to , and returns their weighted blend:
- — the query vector: what's currently "looking," trying to find relevant tokens.
- — the key vector for token , one of the fixed set of tokens being attended to.
- — the dimensionality of the query and key vectors, used to rescale the dot product.
- — the raw, scaled similarity between the query and token 's key.
- — the attention weight on token , the softmax-normalized score.
- context — the weighted blend of all the key vectors: the actual output of attention.
- Why scale by the square root of d
The divides out the fact that dot products grow with dimension — without it, scores in a high-dimensional model would blow up and softmax would saturate into an almost one-hot distribution regardless of which token actually matches best.
Watch the context vector — the actual output of attention — as you drag the query. It's always some blend of "the," "cat," and "sat," and it moves smoothly as the weights shift. This is the payoff: instead of one RNN hidden state trying to remember an entire sequence, attention produces a fresh, query-specific blend of all of it, recomputed on demand.
At query — aligned with "the" :
- Raw dot products
With "the", "cat", "sat":
- "the":
- "cat":
- "sat":
- Scale by the square root of d
Dividing each by :
- "the":
- "cat":
- "sat":
- Softmax into weights
Exponentiating each score and dividing by their sum :
- "the": , so
- "cat": , so
- "sat": , so
"The" dominates, but "sat" (whose vector partially overlaps) still gets a meaningful share, and "cat" (pointing in an unrelated direction) gets almost none.
Drag the query until “cat” receives more than 0.7 of the total attention weight.
This chapter used one query looking at a fixed set of tokens — the original attention mechanism, first built to let a decoder look back at every encoder state instead of just the last one. The next chapter asks what happens when every token in a sequence gets to be a query looking at every other token, all at once, and why that needs more than one attention computation running in parallel.