Attention Intuition › Comparing Attention Formulations
Language AI HandbookAll chapters Attention Intuition › Comparing Attention FormulationsOne section · Sections in this chapter14 / 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
We've established that attention requires a scoring function to measure relevance between decoder and encoder states. Different choices lead to different attention mechanisms, each with distinct trade-offs.
The fundamental question each scoring function answers is the same: "Given what I'm trying to generate (the decoder state) and what information is available (an encoder state), how compatible are they?" The answer is always a single number, the alignment score. But how we compute that number varies significantly.
Dot Product Attention
The most direct approach treats compatibility as geometric alignment. Two vectors pointing in similar directions have high compatibility; orthogonal vectors have zero compatibility. The dot product captures exactly this intuition:
where:
- : the decoder state at step , a vector of dimension
- : the encoder state at position , also dimension
- : the inner product, computed as
Geometrically, the dot product equals , where is the angle between the vectors. When both vectors point in the same direction (), the score is maximized. When they are perpendicular (), the score is zero.
This simplicity is both a strength and a limitation. Dot product attention requires no learnable parameters, making it computationally efficient. However, it requires the decoder and encoder to have the same hidden dimension, and it assumes compatibility can be measured purely through geometric alignment in the shared representation space. If the encoder and decoder use different architectures or different hidden sizes, dot product scoring cannot be applied without first projecting one into the other's space.
Additive Attention (Bahdanau)
What if simple geometric alignment isn't expressive enough? Perhaps compatibility depends on complex, nonlinear relationships between the decoder and encoder states. Additive attention addresses this by introducing a small neural network:
where:
- : a learnable weight matrix of dimension that projects the decoder state
- : a learnable weight matrix of dimension that projects the encoder state
- : a learnable weight vector of dimension that produces the final scalar score
- : the hyperbolic tangent, introducing nonlinearity
- : the attention hidden dimension, a hyperparameter
The formula works as follows: projects the decoder state into a new space of dimension . Similarly, projects the encoder state into the same space. Adding these projections combines information from both states. The activation introduces nonlinearity. Finally, projects the combined representation to a scalar score.
This approach has two key advantages. First, the learnable parameters allow the model to discover what "compatibility" means for the specific task. The network can learn to ignore certain dimensions of the encoder or decoder state, to focus on dimensions that are systematically predictive of relevance, and to capture interaction effects between specific encoder and decoder features. Second, since we project both states into a common space of dimension , the encoder and decoder can have different dimensions. We'll implement this fully in the Bahdanau Attention chapter.
The cost is additional parameters: (for ) plus (for ) plus (for ). For a typical setup with , , and , this adds about 66,000 parameters. Compared to the millions of parameters in the LSTM layers, this is modest. But the scoring function must also be evaluated at every encoder position for every decoder step, so its computational cost scales with sequence length.
Scaled Dot Product Attention
The transformer architecture revived dot product attention but added a scaling refinement. The problem with vanilla dot products becomes apparent in high dimensions: scores can grow very large, causing softmax to produce extremely peaked distributions.
To understand why, consider what happens when we compute for vectors with dimensions. If each component is independently drawn from a zero-mean, unit-variance distribution, the variance of the dot product is approximately . For , the standard deviation of scores is about 22. Scores this large cause softmax to assign nearly all probability mass to a single position, producing near-zero gradients for all other positions and slowing learning. The model gets "stuck" because the peaked softmax means nearly all gradient signal goes to the single highest-scoring position; the other positions receive almost no gradient and cannot improve.
Scaled dot product attention fixes this by normalizing:
where:
- : the dimension of the key vectors
- : the scaling factor that brings variance back to approximately 1 regardless of dimension


The visualization confirms the scaling problem. As dimension increases, the distribution of dot product scores spreads dramatically. At , scores routinely exceed , which would cause softmax to produce extremely peaked distributions. Scaling by normalizes this spread back to a manageable range, keeping the standard deviation near 1 regardless of embedding dimension. This simple fix makes scaled dot product attention stable across a wide range of hidden dimensions and is one reason why transformers could be scaled to very large sizes without requiring careful tuning of the attention mechanism.
Choosing a Scoring Function
Each approach represents a different trade-off:
| Scoring Function | Parameters | Computational Cost | Flexibility |
|---|---|---|---|
| Dot Product | None | Low (same dimension required) | |
| Additive | per position | High (different dimensions OK) | |
| Scaled Dot Product | None | Low (same dimension required) |
Dot product attention is fastest but requires matching encoder and decoder dimensions. Additive attention is most flexible and introduces learned parameters, at the cost of more computation per position. Scaled dot product combines the efficiency of dot products with numerical stability for high dimensions.
In modern practice, scaled dot product attention dominates. Its efficiency allows transformers to run multiple attention heads in parallel, each learning different aspects of relevance. The lack of learnable parameters in the score function is offset by the projection matrices that create queries, keys, and values. When you apply a linear projection to the encoder states before computing attention, you are effectively learning the alignment metric, just in a different, more modular way. We'll explore this architecture in the Self-Attention chapters.