Luong Attention › Attention Score Functions
Language AI HandbookAll chapters Luong Attention › Attention Score FunctionsOne section · Sections in this chapter2 / 11 ↓
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
At the heart of any attention mechanism lies a fundamental question: how do we measure the relevance of each encoder state to the current decoding step? This measurement, called the alignment score or compatibility score, determines which parts of the input sequence the decoder should focus on when generating each output token.
Think of it this way: when translating "The cat sat on the mat" to French, and you're about to generate the word "chat" (cat), you need a way to identify that the English word "cat" is the most relevant source word. The score function quantifies this relevance for every source position, producing a set of numbers that attention will convert into a probability distribution.
Bahdanau attention introduced an additive score function with learned parameters. Luong et al. asked: can we achieve similar results with simpler approaches? Their answer was three alternative score functions, each representing a different trade-off between simplicity, expressiveness, and computational cost.
Dot Product Attention
The simplest approach to measuring relevance is to ask: how similar are the decoder and encoder representations? If the encoder has learned to represent "cat" in a particular way, and the decoder has learned to look for that same pattern when generating "chat", then similar representations should yield high scores.
The dot product provides exactly this similarity measure. Given a decoder hidden state and an encoder hidden state , the dot product attention score is:
where:
- : the decoder hidden state at timestep , encoding what the decoder is "looking for"
- : the encoder hidden state at source position , encoding what that position "contains"
- : the inner product of the two vectors, yielding a scalar score
- : the hidden dimension, which must be equal for both encoder and decoder
Why does the dot product capture similarity? Consider what happens geometrically. When two vectors point in the same direction, their dot product is large and positive, indicating high compatibility. When they point in opposite directions, the dot product is large and negative. Orthogonal vectors, sharing no common direction, yield zero.
This geometric interpretation has a powerful implication: the encoder and decoder can learn complementary representations. The encoder learns to place semantically similar words in similar directions in the hidden space. The decoder learns to "query" for specific semantic content by producing hidden states that point toward the relevant encoder representations.
The elegance of dot product attention lies in its simplicity. No learned parameters are needed for the score function itself. All the learning happens in the encoder and decoder, which discover representations where raw similarity is a good proxy for alignment. This makes dot product attention computationally efficient and easy to implement.
However, this simplicity comes with a constraint: the decoder and encoder hidden states must have the same dimensionality. If and , the dot product is well-defined. If they have different dimensions, you cannot compute a dot product directly, which motivates our next score function.
General (Bilinear) Attention
What if the encoder and decoder have different hidden dimensions? Or what if raw similarity isn't the right measure, and we want the model to learn a more specific notion of relevance?
The general score function addresses both concerns by introducing a learnable weight matrix that transforms the encoder states before computing similarity:
where:
- : the decoder hidden state at timestep
- : the encoder hidden state at source position
- : a learned weight matrix that projects encoder states into the decoder's space
- : a scalar score computed by first applying to , then taking the dot product with
To understand what does, trace through the computation step by step. First, converts the encoder state into a -dimensional vector. Then takes the dot product with this transformed vector. The matrix effectively learns which aspects of the encoder representation are most relevant for each dimension of the decoder state.
This formulation is called bilinear attention because the score is a bilinear function of the two input vectors: linear in when is fixed, and linear in when is fixed. The weight matrix serves two purposes:
- Dimension matching: When encoder and decoder have different hidden dimensions, bridges the gap by projecting encoder states into the decoder's space
- Learned similarity: Rather than relying on raw vector similarity, the matrix learns what aspects of encoder states are most relevant for alignment, potentially discovering non-obvious relationships
General attention is more expressive than dot product attention but requires additional parameters. For encoder and decoder dimensions of 512, adds parameters. This is modest compared to the total model size but can matter for smaller models or when memory is constrained.
Concat Attention
The most expressive score function takes a fundamentally different approach. Rather than computing a form of similarity between two vectors, it asks: given both the decoder state and an encoder state, what score should a neural network assign?
The concat score function concatenates the two states and passes them through a small neural network:
Let's unpack this formula piece by piece:
-
Concatenation : We stack the decoder and encoder states into a single vector of dimension . This gives the network access to all information from both states.
-
Linear projection : The weight matrix projects the concatenated vector to an intermediate space of dimension . This allows the network to learn arbitrary linear combinations of the input features.
-
Nonlinearity : The hyperbolic tangent activation introduces nonlinearity, enabling the network to learn complex, non-linear relationships between decoder and encoder states.
-
Scalar projection : Finally, the weight vector projects the intermediate representation to a single scalar score.
where:
- : the decoder hidden state at timestep
- : the encoder hidden state at source position
- : the concatenated vector of both states
- : a learned projection matrix
- : a learned weight vector that collapses the intermediate representation to a scalar
- : the intermediate dimension, a hyperparameter typically set equal to the hidden dimension
This formulation is similar to Bahdanau attention, with one key difference: Bahdanau uses the previous decoder state , while Luong's concat uses the current decoder state . This seemingly small change has clear implications for the overall architecture, which we'll explore in the section on attention placement.

Computational Comparison
The three score functions differ significantly in their computational requirements. For a sequence of length with hidden dimension :
| Score Function | Time Complexity | Parameters | Notes |
|---|---|---|---|
| Dot Product | 0 | Fastest, requires | |
| General | Matrix multiplication dominates | ||
| Concat | Two-step transformation |
As shown in Table score-functions, dot product attention is most efficient but requires matching dimensions. General attention adds a single matrix, while concat attention requires two sets of parameters ( and ).


For modern hardware with efficient matrix operations, dot product attention is significantly faster because it can be fully parallelized as a single matrix multiplication across all encoder states simultaneously. This efficiency advantage is why transformers adopt scaled dot-product attention as their core mechanism.