Attention Intuition › How Attention Integrates with the Decoder
Language AI HandbookAll chapters Attention Intuition › How Attention Integrates with the DecoderOne section · Sections in this chapter5 / 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
Understanding attention in isolation is one thing; understanding how it connects to the full decoding process is another. Let's trace through the entire decoder step in detail, seeing exactly how the context vector feeds into the generation of each output token.
At each decoding step , the decoder maintains a hidden state and must produce two things: the next output token and the updated hidden state for the next step. The attention mechanism intervenes between these two operations, enriching the decoder's information before it makes the prediction.
The Decoder with Attention
The standard decoder with attention follows this sequence at each step :
First, the decoder receives two inputs: the embedding of the previously generated token and the previous context vector . These are concatenated and fed into the RNN cell together with the previous hidden state :
Next, the new decoder state is used to compute attention over the encoder states, producing a fresh context vector tailored to the current generation step:
Finally, and are combined to predict the output token. A common approach is to concatenate them, apply a linear transformation to produce a pre-output vector , and then apply softmax over the vocabulary:
where and are learned weight matrices. This is the architecture used in Luong attention, which we will study in detail in the Luong Attention chapter.
There is a noteworthy design choice in how the decoder state is computed. Some implementations, including Bahdanau's original formulation, compute attention using (the previous decoder state) rather than (the current one). Using for attention means the model selects its focus before integrating the previous output token, while using means the focus is informed by the current token's embedding. Both approaches work in practice, with Bahdanau using and Luong using .
Feedback Through the Decoder
The full decoder loop creates an interesting feedback structure. The context vector influences what token is generated. That generated token becomes the input for the next step, which influences the new decoder state , which in turn shapes the next attention distribution. Attention is not a one-shot computation; it participates in a dynamic, step-by-step dialogue between the decoder and the encoder states.
This feedback loop is why attention is so powerful for generation tasks. When the decoder generates accurate translations early in the output by focusing well, it creates a good hidden state for the next step. That good hidden state produces a better query for attention, which produces a better context vector, and so on. Conversely, when the decoder makes an error, that error can propagate forward through the hidden state, making subsequent attention queries less accurate. This is one reason why beam search (maintaining multiple hypotheses in parallel) is useful: it hedges against early errors by not committing to a single generation path.