In an RNN encoder–decoder for translation, what is the bottleneck problem, and how does attention fix it? What are the equations for the context vector at decoder step t, and what is the context vector for scores [2,0,1] over three encoder states with (scalar) values h=[1,2,3]?

Sequence models and transformers, Sequence-to-sequence models
Level L3 Intermediate. Length Medium. Nature Th+Mat Both.

What's being asked

Whether you can write Bahdanau or Luong attention as score, softmax and weighted sum, and connect it to query–key–value attention.

Key idea

Instead of one fixed vector for the whole source, each decoder step builds its own context ct=∑jαtjhj. Here hj are the encoder states, αtj=exp⁡(etj)∑kexp⁡(etk) are the attention weights, and etj=score⁡(st−1,hj) are scores against the decoder state st−1.

Steps

  1. Bottleneck: the plain encoder squeezes the whole source into its final state hn, so quality drops sharply on long sentences.
  2. Scores: additive (Bahdanau) etj=v⊤tanh⁡(Wsst−1+Whhj), with learned v,Ws,Wh; or dot product (Luong) etj=st⊤hj.
  3. Weights and context: αtj=softmax⁡j(etj) and ct=∑jαtjhj. The decoder uses ct in its state update and in the output, e.g. p(yt∣⋅)=softmax⁡(Wo[st;ct]).
  4. Worked example: α=[e2,e0,e1]e2+1+e=[7.389,1,2.718]11.107≈[0.665,0.090,0.245]. Then ct=0.665⋅1+0.090⋅2+0.245⋅3≈1.58.
  5. Link to transformers: the decoder state is the query, and the encoder states are the keys and values. The cost is O(nm) score computations for source length n and target length m. The α matrix gives a soft alignment between source and target words that you can plot.

Common mistakes

How long your answer should be

About 3 minutes; three equations and the example.

Last checked on 4 December 2026.

Read this

Previous in Sequence models and transformers: What are the LSTM cell equations, and why does the cell state ease the vanishing-gradient problem of a vanilla RNN? How does a GRU differ, and how many parameters does each have for input size 100 and hidden size 128?
Next in Sequence models and transformers: What is the KV cache in autoregressive transformer decoding? How does it change the cost of generating each token, and how much memory does it need for a 32-layer model with dmodel=4096, a context of 4096 tokens, and fp16 values?