Attention Intuition › The Mathematics of Attention

Michael BrenndoerferMay 16, 20259 min read
Language AI HandbookAll chapters
Part 12 of 67Sequence-to-Sequence
Chapter 86Attention Intuition › The Mathematics of Attention
Reading section 4 of 20 · The Mathematics of Attention · 9 minSection 4 of 20 · 9 min

Book contents

Section 4 of 20 · 9 min read

Attention Intuition › The Mathematics of AttentionOne section · Sections in this chapter4 / 20 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Now that we have the intuition, let's translate it into precise mathematics. The goal is to build a mechanism that answers one question at each decoding step: "Which parts of the input should I focus on right now?"

Imagine you're the decoder, trying to generate the next word in a translation. You have your current state sts_t, which encodes what you've generated so far and what you're trying to produce next. Meanwhile, the encoder has processed the entire input sentence and produced a sequence of hidden states h1,h2,…,hTh_1, h_2, \ldots, h_T, one for each of the TT input words. Each encoder state hjh_j captures the meaning of word jj in context.

The challenge is clear: you need to selectively combine information from all these encoder states, giving more weight to positions that are relevant to your current generation task. Attention solves this in three steps.

Step 1: Measuring Relevance with Alignment Scores

The first question attention must answer is: "How relevant is each input position to what I'm currently generating?" We need a way to compare the decoder's current state with each encoder state and produce a relevance score.

This comparison happens through a scoring function:

etj=score(st,hj)e_{tj} = \text{score}(s_t, h_j)

where:

  • etje_{tj}: the alignment score, a single number indicating how relevant encoder position jj is when generating at decoder position tt
  • sts_t: the decoder's hidden state at step tt, encoding the generation context
  • hjh_j: the encoder's hidden state at position jj, encoding information about input word jj
  • score(⋅,⋅)\text{score}(\cdot, \cdot): any function that takes two vectors and returns a scalar measuring their compatibility

Think of this as the decoder asking each encoder position: "How useful are you for what I'm trying to do right now?" A high score means "very useful," while a low or negative score means "not relevant."

What should this scoring function look like? The simplest choice is the dot product: etj=st⊤hje_{tj} = s_t^\top h_j, which measures how aligned the two vectors are in the embedding space. Vectors pointing in similar directions yield high scores; orthogonal vectors yield zero. We'll explore more sophisticated scoring functions in the section on attention formulations below, and cover them in full detail in the Bahdanau Attention and Luong Attention chapters.

The decoder state sts_t deserves a closer look. In an RNN decoder, sts_t is the hidden state at generation step tt, after the decoder has already generated tokens y1,y2,…,yt−1y_1, y_2, \ldots, y_{t-1}. It carries information about both what has been generated and what the decoder is about to produce. This duality is why sts_t makes a good query: it simultaneously knows what context has already been established (so it doesn't ask the encoder to repeat what was already encoded in earlier context vectors) and what needs to be generated next (so it can seek out the relevant encoder information for the upcoming token).

Step 2: Converting Scores to a Probability Distribution

Raw alignment scores present a problem: they can be any real number, positive or negative, large or small. We need to convert them into something interpretable and usable, specifically a probability distribution over input positions.

The softmax function accomplishes this transformation. Given the alignment scores et1,et2,…,etTe_{t1}, e_{t2}, \ldots, e_{tT} for all TT input positions, the attention weight for position jj is:

αtj=exp⁡(etj)∑k=1Texp⁡(etk)\alpha_{tj} = \frac{\exp(e_{tj})}{\sum_{k=1}^{T} \exp(e_{tk})}

where:

  • αtj\alpha_{tj}: the attention weight for position jj, guaranteed to be between 0 and 1
  • exp⁡(etj)\exp(e_{tj}): the exponential function applied to the alignment score, converting any real number to a positive value
  • ∑k=1Texp⁡(etk)\sum_{k=1}^{T} \exp(e_{tk}): the sum of all exponentiated scores, serving as a normalizing constant

Why softmax? The exponential function maps any real number to a positive value. This ensures all weights are non-negative. Dividing by the sum of all exponentials guarantees that ∑j=1Tαtj=1\sum_{j=1}^{T} \alpha_{tj} = 1. The result is a valid probability distribution over input positions.

Softmax also has a useful amplification property. If one score is much larger than the others, its corresponding weight will dominate. For example, if et1=5e_{t1} = 5 and all other scores are near 0, then αt1\alpha_{t1} will be close to 1 while the other weights approach 0. This allows the model to focus sharply on a single position when appropriate, or spread attention across multiple positions when relevance is more evenly distributed.

The competition enforced by softmax is worth dwelling on. Because all weights must sum to 1, increasing attention to one position automatically decreases attention to others. This makes attention a zero-sum competition for relevance: every additional unit of focus on position jj is taken from other positions. As a result, training the model to focus correctly on position jj requires rewarding high scores for jj while learning to suppress all positions that are less relevant. This competitive dynamic helps attention produce sharp, interpretable patterns rather than diffuse or arbitrary distributions.

Out[3]:
Visualization
Bar chart showing uniform alignment scores and resulting uniform attention weights of 0.20 each.
Uniform alignment scores produce uniform attention weights (0.20 each), showing no preference among input positions.
Bar chart showing small score differences amplified by softmax into unequal weights.
Small score differences get amplified by softmax into clearer but still distributed preferences across positions.
Bar chart showing large score differences producing highly peaked attention on position P1.
Large score differences produce near one-hot attention, where almost all weight concentrates on the highest-scoring position.

The visualization above demonstrates this amplification effect. With uniform scores, softmax produces uniform weights (0.20 each). Small score differences get amplified into clearer preferences. Large differences produce nearly one-hot attention, where almost all weight concentrates on the highest-scoring position.

Step 3: Computing the Context Vector

With attention weights in hand, we can finally answer our original question: "What information from the input should I use?" The answer is a weighted combination of all encoder states:

ct=∑j=1Tαtjhjc_t = \sum_{j=1}^{T} \alpha_{tj} h_j

where:

  • ctc_t: the context vector at decoder step tt, a single vector summarizing the relevant input information
  • αtj\alpha_{tj}: the attention weight for encoder position jj at decoder step tt
  • hjh_j: the encoder hidden state at position jj (the "value" being retrieved)

This weighted sum is the heart of attention. Each encoder state contributes to the context vector in proportion to its attention weight. If αt3=0.7\alpha_{t3} = 0.7 and all other weights are small, then ctc_t will be dominated by h3h_3, the information at position 3. If weights are more evenly spread, the context vector blends information from multiple positions.

The context vector has the same dimension as the encoder states, making it easy to integrate with the rest of the model. The decoder uses ctc_t alongside its own state sts_t to predict the next output token, often by concatenating them and passing through a feed-forward layer. The standard approach concatenates them as [ct;st][c_t; s_t] and passes the combined vector through a learned transformation followed by a softmax over the vocabulary.

One subtle but important property of this formula is that the context vector is always the same dimension as the encoder states, regardless of TT, the number of input positions. Whether we are weighting 5 encoder states or 500, the result is always a single vector of dimension dhd_h. This is what gives attention its elegant scalability: adding more input tokens does not change the interface between attention and the rest of the model.

The Complete Picture

Let's trace through a concrete example. Suppose we're translating "The cat sat" and currently generating the French word "chat" (cat). The decoder state sts_t encodes that we've generated "Le" and are now producing the noun.

  1. Alignment scores: The scoring function compares sts_t with each encoder state. It finds high compatibility with h2h_2 (the representation of "cat") and lower compatibility with h1h_1 ("The") and h3h_3 ("sat").

  2. Attention weights: Softmax converts these scores into probabilities. Perhaps αt1=0.05\alpha_{t1} = 0.05, αt2=0.85\alpha_{t2} = 0.85, αt3=0.10\alpha_{t3} = 0.10.

  3. Context vector: The weighted sum ct=0.05⋅h1+0.85⋅h2+0.10⋅h3c_t = 0.05 \cdot h_1 + 0.85 \cdot h_2 + 0.10 \cdot h_3 produces a context vector dominated by the representation of "cat."

The decoder combines ctc_t with sts_t and predicts "chat" as the next word. At the next step, when generating "noir" (black), the attention mechanism shifts focus to the encoder state for "black." This dynamic, step-by-step focus is what makes attention effective. Rather than relying on a single compressed representation of the entire input, the model can look back at the original encoder states and select the information most relevant to each generation step.

Notice that this also handles an important linguistic phenomenon: reordering. In French, adjectives typically follow the noun, so "black cat" becomes "chat noir." Without attention, the decoder would have to somehow use a single compressed vector to generate words in a different order than the input. With attention, the decoder simply attends to "cat" when generating "chat" and to "black" when generating "noir," regardless of the fact that these words appeared in reverse order in the source. Attention handles word reordering naturally because it is agnostic to position, caring only about relevance.