Attention Mechanism (2015) › The Solution

Michael BrenndoerferNovember 2, 20253 min read
History of Language AIAll chapters
Part 4 of 9Deep Learning Arrives
Chapter 47Attention Mechanism (2015) › The Solution
Reading section 3 of 7 · The Solution · 3 minSection 3 of 7 · 3 min

Book contents

Section 3 of 7 · 3 min read

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 ii in the source sentence and the current decoder step jj, the model computed an alignment score eije_{ij} that measured how well the source word at position ii aligned with the target word being generated at step jj. 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 jj, the attention weight αij\alpha_{ij} for source position ii was computed as:

αij=exp⁡(eij)∑k=1Texp⁡(ekj)\alpha_{ij} = \frac{\exp(e_{ij})}{\sum_{k=1}^{T} \exp(e_{kj})}

where TT 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 jj was computed as a weighted sum of all encoder hidden states, where the weights came from the attention mechanism:

cj=∑i=1Tαijhic_j = \sum_{i=1}^{T} \alpha_{ij} h_i

where hih_i represents the encoder hidden state at position ii. 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 eije_{ij} was calculated as:

eij=vTtanh⁡(Wasj−1+Uahi)e_{ij} = v^T \tanh(W_a s_{j-1} + U_a h_i)

where sj−1s_{j-1} is the decoder hidden state at the previous step, hih_i is the encoder hidden state at position ii, WaW_a and UaU_a are weight matrices, vTv^T is a learned vector, and tanh⁡\tanh 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:

eij=(Wqsj)T(Wkhi)e_{ij} = (W_q s_j)^T (W_k h_i)

where WqW_q and WkW_k 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.