Before you look at a single query-key-value equation, it helps to understand attention as the answer to a specific, concrete problem: how do you let a model look back at the relevant part of a long input instead of forcing it through a single fixed-size bottleneck?
The problem attention was invented to fix
Early sequence-to-sequence translation models compressed an entire input sentence into one fixed-length vector, then tried to generate the translation from that single vector alone. This works passably for short sentences and falls apart for long ones — you're asking one vector to hold everything about a 40-word sentence, and by the time the decoder is generating word 30 of the translation, whatever detail it needed from word 3 of the input has been diluted past usefulness.
Attention's fix: instead of compressing everything into one vector and discarding the rest, keep a representation of every input position around, and let the decoder look back at all of them at each generation step, weighting each one by how relevant it is to what's being generated right now.
The weighting is the whole idea
At each decoding step, attention computes a relevance score between "what I'm trying to generate now" and "each part of the input," turns those scores into weights that sum to one (via softmax), and takes a weighted average of the input representations using those weights. When translating "the black cat" into French and generating the word for "cat," the mechanism should — and empirically does — assign most of its weight to the input position holding "cat," with small residual weight elsewhere.
def attention_weights(query, keys):
scores = keys @ query # relevance of each key to the query
weights = softmax(scores)
return weights # sums to 1, one weight per input position
def softmax(x):
e = np.exp(x - np.max(x))
return e / e.sum()That's genuinely most of the concept. The query is "what am I looking for right now," the keys are "what does each position offer," and the resulting weights tell you how much to borrow from each position's value when building the current output.
Self-attention is the same idea turned inward
Once you accept that a decoder can attend over an encoder's positions, the next step is realizing there's no reason attention has to point from one sequence to another — a sequence can attend to itself. Self-attention lets each token in a sentence weigh every other token in the same sentence when building its own updated representation. This is how a model resolves something like pronoun reference: when processing "it" in "the trophy didn't fit in the suitcase because it was too big," self-attention lets "it" assign high weight to "trophy" (or "suitcase," depending on context), directly incorporating that information into it's representation, rather than relying on distance or recurrence to carry the connection.
Why "attention" is a reasonable name, not just branding
The mechanism genuinely mirrors what the word suggests: a limited, weighted focus on the currently-relevant subset of a larger context, recomputed fresh at every step rather than fixed in advance. That's different from, say, a convolution's fixed local window — attention's "window" is dynamic and content-dependent, decided by what the query actually needs at that moment, not by a fixed geometric neighborhood.
Where the intuition still holds once you add the machinery
| Concept | Plain-language meaning |
|---|---|
| Query | What am I looking for right now |
| Key | What does this position have to offer |
| Value | The actual content to retrieve if relevant |
| Attention weight | How relevant this position is to the current query |
| Multi-head | Multiple independent "what am I looking for" questions asked in parallel |
Everything transformers add on top of this — scaling, multiple heads, positional encoding, masking — is refinement of this one mechanism, not a replacement for it. If the query-key-value framing above makes sense, the rest of the architecture is mostly bookkeeping around making that core idea fast, stable to train, and aware of sequence order.