Bahdanau attention (also called additive attention) and Luong attention (also called multiplicative attention) are two pionering mechanisms that allow neural sequence models to selectively focus on different parts of the input when generating each output token.
The fundamental difference: HOW they compute alignment scores between decoder and encoder states.
WHY concatenate? We want to capture the interaction between decoder state and each encoder state. A simple dot product (Luong) assumes the spaces are already aligned; Bahdanau learns an alignment function.
eti=vaTtanh(Wast−1+Uahi)
WHERE:
Wa∈Rda×ds projects decoder state
Ua∈Rda×dh projects encoder state
va∈Rda is the alignment weight vector
da is the attention dimension (hyperparameter, often 128-512)
This is a single-layer feedforward network with da hidden units.
Step 2: Attention Weights
Convert scores to probability distribution (MUST sum to 1, all positive):
αti=∑j=1Texp(etj)exp(eti)
WHY softmax? Ensures ∑iαti=1 (proper probability distribution) and emphasizes high-scoring positions (exponential amplification).
Step 3: Context Vector
Weighted average of all encoder states:
ct=∑i=1Tαtihi
This is the "summary" of input that's relevant for predicting yt.
Step 4: Decoder Update
Bahdanau concatenates context BEFORE the RNN step:
y~t−1=[yt−1;ct]
st=GRU(y~t−1,st−1)
p(yt∣y<t,x)=softmax(Wost)
Why concatenate before? This is input-feeding: the context influences the RNN state update itself, giving attention information more direct control over the hidden state evolution.
Teacher forcing: During training, use ground-truth yt−1 even if model predicted something else. This stabilizes early training but can cause exposure bias at test time.
Recall Explain to a 12-year-old
Imagine you're translating a sentence from English to Spanish, word by word. The old way was like reading the entire English sentence once, closing the book, and trying to remember everything while you write in Spanish. If the sentence was long, you'd forget important parts!
Attention is like being allowed to keep the English book open. When you're writing each Spanish word, you can look back at the English sentence and focus on the part that's most helpful right now.
Bahdanau is like looking at the book BEFORE you start thinking about the next Spanish word. You check the English, then think.
Luong is like thinking first about what Spanish word might come next, THEN checking the English book to make sure you're on track.
Both work great! Luong is a bit faster because it can pre-read parts of the English book, while Bahdanau reads it fresh each time.
The "attention weights" are like highlighting: you highlight the English words that matter most for the current Spanish word. Sometimes one word is highlighted bright (90% attention), sometimes it's spread across few words.
4.2.3-Beam-Search: Attention weights guide beam search in decoding
5.1.2-Word-Embeddings: Attention operates in embedding space
#flashcards/ai-ml
What is the key difference between Bahdanau and Luong attention timing? :: Bahdanau computes attention BEFORE the decoder RNN step (uses st−1), while Luong computes attention AFTER the decoder RNN step (uses st).
What is the Bahdanau attention score function?
eti=vaTtanh(Wast−1+Uahi) — an additive/concat mechanism using a feedforward network with tanh nonlinearity.
What is the Luong general attention score function?
eti=stTWahi — a bilinear/multiplicative mechanism that learns to map encoder space to decoder space.
What is input feeding in Bahdanau attention?
The context vector ct is concatenated with the input embedding and fed to the decoder RNN: st=RNN([yt−1;ct],st−1). This allows attention to directly influence hidden state evolution.
How does Luong attention combine context with decoder state?
It creates an attentional hidden state: s~t=tanh(Wc[st;ct]) which combines the decoder state and context vector through a learned projection.
What are the three Luong attention scoring variants?
1) Dot: stThi (no parameters, requires same dims), 2) General: stTWahi (most common), 3) Concat: vaTtanh(Wa[st;hi]) (similar to Bahdanau).
Why is Luong attention typically faster than Bahdanau?
Luong can precompute Wahi for all encoder positions once, since it's independent of the decoder state. Bahdanau must compute the additive function separately for each encoder position at every decoder step.
What is the formula for attention weights from alignment scores?
αti=∑j=1Texp(etj)exp(eti) — softmax normalization to create a probability distribution that sums to 1.
What is the context vector formula in both mechanisms?
ct=∑i=1Tαtihi — a weighted sum of all encoder hidden states using attention weights.
Why must padding positions be masked before computing attention?
Unmasked padding gets small but non-zero attention weights, wasting probability mass and corrupting the context vector with meaningless information. Masking with −∞ before softmax ensures padding positions get exactly 0 weight.
What is local attention (Luong extension)?
An optimization that attends to only a window [pt−D,pt+D] around a predicted alignment position pt, reducing complexity from O(T) to O(D) per decoder step.
What is the key architectural motivation for attention mechanisms?
To solve the fixed-length bottleneck problem in seq2seq models — instead of compressing the entire input into one vector, attention allows the decoder to dynamically access all encoder states.
How many weight matrices does Bahdanau attention require?
Three: Wa∈Rda×ds for decoder projection, Ua∈Rda×dh for encoder projection, and va∈Rda for the alignment vector. Total parameters: da(ds+dh+1).
How many weight matrices does Luong general attention require?
Two: Wa∈Rds×dh for scoring (parameters: ds⋅dh) and Wc∈Rds×(ds+dh) for combining context and decoder state (parameters: ds(ds+dh)).
Why does Bahdanau use tanh nonlinearity in the score function?
To allow the alignment function to learn complex, non-linear relationships between decoder and encoder states. Without nonlinearity, it would collapse to a simple linear transformation.
Chalo ek simple baat se shuru karte hain. Socho tumhe pura ek book padhkar uska sirf ek chhoti si line mein summary banana hai, aur phir usi ek line ke basis pe review likhna hai. Bahut saari important details toh gayab ho jayengi na? Yahi problem thi purane seq2seq models mein — woh pure input sequence ko ek fixed-size vector mein daba dete the, aur long sentences ke liye yeh "bottleneck" bahut information loss karta tha. Attention ka core idea yahi hai ki decoder ko pura book khula rakhne do — har output word banate waqt woh saare encoder states ko dekh sake aur dynamically decide kare ki abhi kaun sa part sabse relevant hai. Bilkul jaise review likhte waqt tum zaroorat ke hisaab se pages palat lo.
Ab Bahdanau aur Luong attention dono isi kaam ko karte hain, bas farak yeh hai ki alignment score kaise calculate karte hain. Bahdanau (additive attention) ek chhota feedforward network use karta hai — decoder state aur encoder state dono ko ek common space mein project karta hai, tanh se non-linearity add karta hai, phir score nikaalta hai. Iska fayda yeh hai ki model khud "seekh" leta hai ki alignment kaise karna hai. Luong (multiplicative) simple dot product use karta hai, jo maanke chalta hai ki spaces already aligned hain. Dono cases mein score nikalne ke baad softmax lagta hai (taaki weights probability ban jaayein aur sum 1 ho), phir un weights se encoder states ka weighted sum lete hain — yeh banta hai context vector, jo current word predict karne ke liye relevant summary hota hai.
Yeh samajhna kyun zaroori hai? Kyunki attention modern AI ki backbone hai — Google Translate se lekar ChatGPT tak, sab jagah yahi concept scale hua hai. Transformers, jo aaj sabse powerful models hain, seedhe isi attention idea ka advanced version hain. Toh agar tum yeh core intuition — "fixed bottleneck se bacho, dynamically focus karo" — aur alignment score ka basic maths samajh loge, toh tumhare paas poore modern deep learning ki foundation aa jayegi. Chhote se concept se badi cheezein banti hain, bas basics clear hone chahiye!