Luong Attention › Attention Placement: Input vs Output

Michael BrenndoerferMay 18, 20255 min read
Language AI HandbookAll chapters
Part 12 of 67Sequence-to-Sequence
Chapter 88Luong Attention › Attention Placement: Input vs Output
Reading section 4 of 11 · Attention Placement: Input vs Output · 5 minSection 4 of 11 · 5 min

Book contents

Section 4 of 11 · 5 min read

Luong Attention › Attention Placement: Input vs OutputOne section · Sections in this chapter4 / 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

A subtle but important difference between Bahdanau and Luong attention is where the attention mechanism fits into the decoder architecture. This choice affects both the information flow and the computational graph.

Bahdanau: Attention as Input

In Bahdanau attention, the context vector is computed using the previous decoder hidden state and then concatenated with the input embedding to form the input to the current decoder step. The computation proceeds as:

ct=Attention(ht−1,Hˉ)x~t=[xt;ct]ht=RNN(x~t,ht−1)\begin{aligned} c_t &= \text{Attention}(h_{t-1}, \bar{H}) \\ \tilde{x}_t &= [x_t; c_t] \\ h_t &= \text{RNN}(\tilde{x}_t, h_{t-1}) \end{aligned}

where:

  • ht−1h_{t-1}: the decoder hidden state from the previous timestep
  • Hˉ=[hˉ1,hˉ2,…,hˉS]\bar{H} = [\bar{h}_1, \bar{h}_2, \ldots, \bar{h}_S]: the matrix of all encoder hidden states
  • ctc_t: the context vector computed by attending over encoder states using ht−1h_{t-1}
  • xtx_t: the input embedding at timestep tt (typically the previous output token)
  • x~t\tilde{x}_t: the augmented input formed by concatenating xtx_t and ctc_t
  • hth_t: the new decoder hidden state

The context vector influences the RNN computation directly. This means the decoder can use information about which source positions are relevant when updating its hidden state.

Luong: Attention as Output

In Luong attention, the decoder RNN runs first, producing the current hidden state. Then attention is computed using this new state, and the results are combined. The computation proceeds as:

ht=RNN(xt,ht−1)ct=Attention(ht,Hˉ)h~t=tanh⁡(Wc[ct;ht])\begin{aligned} h_t &= \text{RNN}(x_t, h_{t-1}) \\ c_t &= \text{Attention}(h_t, \bar{H}) \\ \tilde{h}_t &= \tanh(W_c [c_t; h_t]) \end{aligned}

where:

  • xtx_t: the input embedding at timestep tt
  • ht−1h_{t-1}: the decoder hidden state from the previous timestep
  • hth_t: the new decoder hidden state after the RNN step
  • Hˉ=[hˉ1,hˉ2,…,hˉS]\bar{H} = [\bar{h}_1, \bar{h}_2, \ldots, \bar{h}_S]: the matrix of all encoder hidden states
  • ctc_t: the context vector computed by attending over encoder states using the current hth_t
  • [ct;ht][c_t; h_t]: the concatenation of context and hidden state
  • Wc∈Rdh×(dhˉ+dh)W_c \in \mathbb{R}^{d_h \times (d_{\bar{h}} + d_h)}: a learned weight matrix that combines context and hidden state
  • h~t\tilde{h}_t: the "attentional hidden state" used for prediction

The final output h~t\tilde{h}_t combines the context vector with the decoder state through a learned transformation. This attentional state is then used for prediction, typically by projecting it to vocabulary size and applying softmax.

Out[7]:
Visualization
Flow diagram showing Bahdanau attention with context computed before the RNN step using the previous hidden state.
Bahdanau attention computes context using the previous hidden state h(t-1), then feeds the context concatenated with the input to the RNN. This 'attention as input' approach means source context directly influences the state update.
Flow diagram showing Luong attention with context computed after the RNN step using the current hidden state.
Luong attention runs the RNN first to produce h(t), then computes context from this current state. The context and hidden state are combined via a learned layer to produce the attentional state used for output.

Implications of Placement

The placement choice has several practical implications:

Information flow: Bahdanau attention allows the context to influence the RNN state update, potentially enabling richer interactions. Luong attention keeps the RNN computation separate, which can be easier to reason about and debug.

Parallelization: Luong attention is slightly more amenable to parallelization during training because the RNN step doesn't depend on the attention computation. However, this advantage is minimal compared to the sequential nature of RNNs themselves.

Empirical performance: Luong et al. found their approach performed comparably or slightly better than Bahdanau attention on machine translation benchmarks. The simpler architecture and faster score functions (especially dot product) made it an attractive choice.