Attention Mechanism (2015) › The Solution
History of Language AIAll chapters Attention Mechanism (2015) › The SolutionOne section · Sections in this chapter3 / 7 ↓
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
Bahdanau and his colleagues let the decoder access all encoder hidden states instead of only the final one. At each generation step, the model computed weights over those states and formed a step-specific context vector.
Different target words can depend on different source positions. The attention mechanism computed an alignment score between the decoder state and each encoder state, normalized the scores, and used them to form a weighted combination of encoder states.
Attention Computation
The attention mechanism worked by computing alignment scores between the decoder's current hidden state and each encoder hidden state. For each position in the source sentence and the current decoder step , the model computed an alignment score that measured how well the source word at position aligned with the target word being generated at step . These scores were computed using a small neural network, often called an alignment model, that took the decoder hidden state and encoder hidden state as inputs.
The alignment scores were then normalized using a softmax function to create attention weights. This normalization ensured that the weights summed to one and could be interpreted as a probability distribution over source positions. For decoder step , the attention weight for source position was computed as:
where is the length of the source sentence. These weights determined how much each encoder hidden state contributed to the context vector used for generating the current target word.
The context vector for decoder step was computed as a weighted sum of all encoder hidden states, where the weights came from the attention mechanism:
where represents the encoder hidden state at position . This context vector contained information from all source positions, weighted by their relevance to the current decoding step, and was combined with the decoder's hidden state to generate the next target word.
Alignment Model Variants
Bahdanau's original paper proposed an additive attention mechanism, where the alignment score was computed using a feedforward network with a single hidden layer. The score was calculated as:
where is the decoder hidden state at the previous step, is the encoder hidden state at position , and are weight matrices, is a learned vector, and is the activation function. This additive approach allowed the model to learn complex relationships between decoder and encoder states.
Later work, particularly by Minh-Thang Luong and colleagues, introduced a simpler dot-product attention that computed alignment scores directly from the inner product between transformed decoder and encoder states. This variant reduced computational complexity while maintaining similar performance:
where and are learned transformation matrices. The dot-product approach was simpler and faster to compute, making it attractive for practical applications while preserving the core attention mechanism's ability to learn dynamic alignments.
Integration with Decoder
At each decoding step, the model computed weights over the encoder positions and formed a weighted context vector. It combined that context with the decoder hidden state to predict the next target word.
The weights could be plotted as an alignment matrix between source and target positions. In the running example, high values might connect "chat" with "cat," "s'est assis" with "sat," and "tapis" with "mat." These alignments were learned from the translation objective rather than direct alignment labels.