An encoder gives states h1=(1,0), h2=(0,1) and h3=(−1,1). The decoder state is s=(1,1). Bahdanau (additive) attention scores each state as ej=v⊤tanh⁡(Wss+Whhj), here with Ws=Wh=I and v=(1,−1). Compute the attention weights and the context vector, and say when additive attention is preferred to dot-product scoring?

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

What's being asked

Whether you can carry out additive attention by hand and explain how its scoring function differs from dot-product attention.

Key idea

Additive attention scores with a small one-hidden-layer network, ej=v⊤tanh⁡(Wss+Whhj). Then αj=softmaxj(ej) and the context is c=∑jαjhj.

Steps

  1. Form s+hj for each j: (2,1), (1,2) and (0,2).
  2. Apply tanh⁡: (0.9640,0.7616), (0.7616,0.9640) and (0,0.9640).
  3. Score with v=(1,−1): e1=0.9640−0.7616=0.2024, e2=0.7616−0.9640=−0.2024 and e3=0−0.9640=−0.9640.
  4. Softmax: e0.2024=1.2244, e−0.2024=0.8167 and e−0.9640=0.3814, total 2.4225. Dividing by 2.4225 gives α=(0.505,0.337,0.157) (to 3 decimal places, summing to 1 within rounding).
  5. Context: c=0.5054(1,0)+0.3372(0,1)+0.1574(−1,1)=(0.348, 0.495).
  6. When to prefer it. Additive attention works when s and hj have different sizes (the matrices Ws,Wh map both into a common space) and it was found to beat unscaled dot-product attention for large dimensions. Dot-product attention is cheaper, since it is one matrix multiplication, which is why transformers use the scaled version.

Common mistakes

How long your answer should be

Half a page with the numbers; 3–4 minutes.

Read this

Previous in Sequence models and transformers: A mixture-of-experts layer has 4 experts, each a feed-forward block with dmodel=1024 and hidden width 4096 (two weight matrices, no biases), and a router that picks the top 2 experts per token. For router logits (2,1,0.5,−1) over the four experts, what are the gate weights of the top-2 experts? Compare the total and active parameters of the layer with one dense feed-forward block, and compute the load-balancing loss N∑ifiPi for f=(0.4,0.3,0.2,0.1) and P=(0.35,0.30,0.20,0.15) for these N=4 experts?
Next in Sequence models and transformers: In an encoder–decoder transformer with dmodel=512 and 8 heads, a decoder layer cross-attends to an encoder output of length S=50 while it has T=20 target tokens. What are the shapes of Q, K and V and of the score matrix, which projections cost the most, and why does cross-attention need no causal mask?