An encoder gives hidden states h1=(1,0), h2=(0,1) and h3=(1,1). The decoder state is s=(2,1). Using Luong dot-product attention, compute the attention weights and the context vector. How does Bahdanau (additive) attention differ?

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

What's being asked

Whether you can compute an attention step by hand and distinguish the two classic score functions and where each uses the decoder state.

Key idea

Score ej=s⊤hj, weights α=softmax(e), context c=∑jαjhj. Bahdanau scores use v⊤tanh⁡(Wss+Whhj) instead.

Steps

  1. Scores: e1=2, e2=1, e3=2+1=3.
  2. Softmax: e2=7.389, e1=2.718, e3=20.086, sum 30.193. So α=(0.2447, 0.0900, 0.6652).
  3. Context c=0.2447(1,0)+0.0900(0,1)+0.6652(1,1)=(0.910, 0.755) (using unrounded weights, (0.9100, 0.7553)). It is close to h3, which matches the query best.
  4. Luong variants: dot s⊤hj, general s⊤Whj, concat v⊤tanh⁡(W[s;hj]). Luong uses the current decoder state st and then forms s~t=tanh⁡(Wc[ct;st]) to predict.
  5. Bahdanau uses the previous decoder state st−1 to get ct, which then feeds into the RNN (recurrent neural network) to produce st. Its score is the additive form v⊤tanh⁡(Wsst−1+Whhj), with a bidirectional encoder.
  6. Additive attention has extra parameters Ws,Wh,v and works well when dimensions differ. Dot-product has none and is faster.

Common mistakes

How long your answer should be

Half a page; 4–5 minutes.

Read this

Previous in Sequence models and transformers: A transformer layer-norm sees the feature vector x=(1,3,5,7) for one token, with γ=2, β=1 for every feature and ϵ=0. What is the output? How does the statistics axis of layer norm differ from batch norm?
Next in Sequence models and transformers: A model gives next-token probabilities (0.50, 0.20, 0.15, 0.10, 0.05) for tokens A–E. What is the sampling distribution under top-k with k=2, and under nucleus (top-p) sampling with p=0.8? Why is nucleus sampling often preferred to a fixed k?