Lesson 6.7core2 h 30 min

Attention is born

A differentiable lookup that lets the decoder read the whole source

This is the hinge of the entire path. Bahdanau's 2015 attention mechanism removed the seq2seq bottleneck and, two years later, became the only mechanism in the transformer (7.1 Attention Is All You Need: the big picture). Derive it once as a soft dictionary lookup with queries, keys and values, and every later formula — scaled dot-product attention (7.2 Scaled dot-product and multi-head attention, fully derived), cross-attention in text-to-image models (8.10 Multimodal models), even dense retrieval in RAG (9.2 Embeddings deep dive) — becomes a variation on something you already understand.

You'll be able to

  • Write Bahdanau's additive attention equations and explain each term
  • Compare additive and multiplicative (Luong) scoring functions and explain why dot products won
  • Reformulate attention as a soft dictionary lookup over queries, keys and values and compute a numeric example
  • State what attention fixed in seq2seq models and what remained for the transformer to fix

Key ideas

  1. Attention computes, at each decoder step, a weighted average of all encoder states; the weights are a softmax of learned alignment scores.
  2. Bahdanau (additive): e_tj = v_aᵀ tanh(W_a s_{t−1} + U_a h_j); α_tj = softmax_j(e_tj); c_t = Σ_j α_tj h_j.
  3. Luong (multiplicative): dot, general and concat scores; the dot product is one matrix multiply and became the standard.
Show 4 more key ideas
  1. Attention = soft dictionary lookup: queries compared with keys, weights blend values; differentiable, so backprop learns what to look up.
  2. Cross-attention (decoder→encoder), self-attention (sequence→itself) and causal self-attention are the same formula with different Q/K/V sources.
  3. Dividing scores by √d_k keeps the softmax from saturating as dimensions grow — the 'scaled' in scaled dot-product attention.
  4. Attention removed the bottleneck but RNNs stayed sequential; dropping the recurrence entirely gives the transformer.

Suggested path through this lesson