An encoder gives states , and . The decoder state is . Bahdanau (additive) attention scores each state as , here with and . 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, . Then and the context is .
Steps
- Form for each : , and .
- Apply : , and .
- Score with : , and .
- Softmax: , and , total . Dividing by gives (to 3 decimal places, summing to within rounding).
- Context: .
- When to prefer it. Additive attention works when and have different sizes (the matrices 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
- Applying the softmax over the hidden dimension instead of over the source positions .
- Dropping the , which would make the score linear in and , with no interaction between them.
- Using weights that do not sum to because of a rounding or arithmetic slip. Always check the sum.
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 and hidden width (two weight matrices, no biases), and a router that picks the top 2 experts per token. For router logits 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 for and for these experts?
Next in Sequence models and transformers: In an encoder–decoder transformer with and 8 heads, a decoder layer cross-attends to an encoder output of length while it has target tokens. What are the shapes of , and and of the score matrix, which projections cost the most, and why does cross-attention need no causal mask?