Bahdanau attention (jise additive attention bhi kehte hain) aur Luong attention (jise multiplicative attention bhi kehte hain) — yeh do pioneering mechanisms hain jo neural sequence models ko allow karte hain ki woh har output token generate karte waqt input ke alag-alag parts par selectively focus karein.
Fundamental difference: YEH ki woh decoder aur encoder states ke beech alignment scores KAISE compute karte hain.
Encoder hidden states: h1,h2,…,hT (har ek ∈Rdh)
Step t par decoder state: st−1∈Rds
Humein output yt generate karna hai
Step 1: Alignment Score Function
Concatenate kyun? Hum decoder state aur har encoder state ke beech interaction capture karna chahte hain. Simple dot product (Luong) assume karta hai ki spaces already aligned hain; Bahdanau ek alignment function seekhta hai.
eti=vaTtanh(Wast−1+Uahi)
JAHAN:
Wa∈Rda×ds decoder state ko project karta hai
Ua∈Rda×dh encoder state ko project karta hai
va∈Rda alignment weight vector hai
da attention dimension hai (hyperparameter, aksar 128-512)
Yeh form kyun?
Dono states ko ek common space mein project karo (da)
tanh non-linearity provide karta hai (complex alignments seekhne deta hai)
vaT scalar score tak reduce karta hai
Yeh da hidden units wala ek single-layer feedforward network hai.
Step 2: Attention Weights
Scores ko probability distribution mein convert karo (ZAROORI hai ki sum 1 ho, sab positive):
αti=∑j=1Texp(etj)exp(eti)
Softmax kyun? Ensure karta hai ki ∑iαti=1 (proper probability distribution) aur high-scoring positions ko emphasize karta hai (exponential amplification).
Step 3: Context Vector
Saare encoder states ka weighted average:
ct=∑i=1Tαtihi
Yeh input ka "summary" hai jo yt predict karne ke liye relevant hai.
Step 4: Decoder Update
Bahdanau RNN step se PEHLE context concatenate karta hai:
y~t−1=[yt−1;ct]
st=GRU(y~t−1,st−1)
p(yt∣y<t,x)=softmax(Wost)
Pehle concatenate kyun karte hain? Yeh input-feeding hai: context RNN state update ko directly influence karta hai, jisse attention information ka hidden state evolution par zyada direct control hota hai.
Yeh better kyun hai? Encoder space ko decoder space mein map karna seekhta hai. ds=dh ko handle karta hai. Phir bhi computationally sasta hai (single matrix multiply).
3. Concat (Bahdanau se milta-julta):
score(st,hi)=vaTtanh(Wa[st;hi])
Pehle attention ke bina decoder state compute karo.
Step 2: Alignment Scores
eti=stTWahi
Step 3: Attention Weights
αti=∑j=1Texp(etj)exp(eti)
Step 4: Context Vector
ct=∑i=1Tαtihi
Step 5: Attentional Hidden State
Key innovation — decoder state aur context ko combine karo:
s~t=tanh(Wc[st;ct])
JAHAN Wc∈Rds×(ds+dh).
Kyun? Yeh learned combination decide karta hai ki context par kitna rely karna hai versus decoder state par. tanh output ko bound karta hai, explosion rokta hai.
Teacher forcing: Training ke dauran, model kuch aur predict kare tab bhi ground-truth yt−1 use karo. Yeh early training stabilize karta hai lekin test time par exposure bias cause kar sakta hai.
Recall 12 saal ke bachche ko samjhao
Imagine karo tum English se Spanish mein ek sentence translate kar rahe ho, word by word. Purana tarika yeh tha: poori English sentence ek baar padho, kitaab band karo, aur phir sab yaad rakhne ki koshish karte hue Spanish likhna shuru karo. Agar sentence lamba ho, toh zaroori parts bhool jaoge!
Attention matlab hai ki English kitaab khuli rakhne ki permission milna. Har Spanish word likhte waqt, tum English sentence par nazar daal sakte ho aur us part par focus kar sakte ho jo abhi sabse helpful hai.
Bahdanau matlab hai kitaab dekhna PEHLE, phir agla Spanish word ke baare mein sochna. Pehle English check karo, phir socho.
Luong matlab hai pehle sochna ki agla Spanish word kya ho sakta hai, PHIR English kitaab check karo ki sahi track par ho.
Dono bahut acche kaam karte hain! Luong thoda faster hai kyunki woh English kitaab ke kuch hisson ko pehle se padh sakta hai, jabki Bahdanau har baar fresh padhta hai.
"Attention weights" highlighting jaisi hain: tum English words ko highlight karte ho jo current Spanish word ke liye sabse zyada matter karte hain. Kabhi ek word bright highlight hota hai (90% attention), kabhi kuch words par spread hota hai.
4.2.3-Beam-Search: Attention weights decoding mein beam search guide karte hain
5.1.2-Word-Embeddings: Attention embedding space mein operate karta hai
#flashcards/ai-ml
Bahdanau aur Luong attention timing ka key difference kya hai? :: Bahdanau attention decoder RNN step se PEHLE compute karta hai (uses st−1), jabki Luong attention decoder RNN step ke BAAD compute karta hai (uses st).
Bahdanau attention score function kya hai?
eti=vaTtanh(Wast−1+Uahi) — ek additive/concat mechanism jo tanh nonlinearity wale feedforward network use karta hai.
Luong general attention score function kya hai?
eti=stTWahi — ek bilinear/multiplicative mechanism jo encoder space ko decoder space mein map karna seekhta hai.
Bahdanau attention mein input feeding kya hai?
Context vector ct ko input embedding ke saath concatenate karke decoder RNN ko feed kiya jaata hai: st=RNN([yt−1;ct],st−1). Isse attention hidden state evolution ko directly influence kar sakti hai.
Luong attention context ko decoder state ke saath kaise combine karta hai?
Yeh ek attentional hidden state banata hai: s~t=tanh(Wc[st;ct]) jo decoder state aur context vector ko ek learned projection ke through combine karta hai.
Teen Luong attention scoring variants kya hain?
1) Dot: stThi (koi parameters nahi, same dims zaroori), 2) General: stTWahi (sabse common), 3) Concat: vaTtanh(Wa[st;hi]) (Bahdanau se milta-julta).
Luong attention typically Bahdanau se faster kyun hota hai?
Luong Wahi ko saare encoder positions ke liye ek baar precompute kar sakta hai, kyunki yeh decoder state se independent hai. Bahdanau ko har decoder step par har encoder position ke liye additive function alag se compute karna padta hai.
Alignment scores se attention weights ka formula kya hai?
αti=∑j=1Texp(etj)exp(eti) — softmax normalization se ek probability distribution banta hai jo sum 1 hoti hai.
Dono mechanisms mein context vector ka formula kya hai?
ct=∑i=1Tαtihi — attention weights use karke saare encoder hidden states ka weighted sum.
Attention compute karne se pehle padding positions ko kyun mask karna zaroori hai?
Unmasked padding ko small but non-zero attention weights milte hain, probability mass waste hoti hai aur context vector meaningless information se corrupt hoti hai. Softmax se pehle −∞ se mask karne se padding positions ko exactly 0 weight milta hai.
Local attention (Luong extension) kya hai?
Ek optimization jo predicted alignment position pt ke aaspaas sirf ek window [pt−D,pt+D] ko attend karta hai, complexity O(T) se O(D) per decoder step tak kam kar deta hai.
Attention mechanisms ka key architectural motivation kya hai?
Seq2seq models mein fixed-length bottleneck problem solve karna — poore input ko ek vector mein compress karne ki jagah, attention decoder ko dynamically saare encoder states access karne deta hai.
Teen: Wa∈Rda×ds decoder projection ke liye, Ua∈Rda×dh encoder projection ke liye, aur va∈Rda alignment vector ke liye. Total parameters: da(ds+dh+1).
Luong general attention mein kitni weight matrices hoti hain?
Do: Wa∈Rds×dh scoring ke liye (parameters: ds⋅dh) aur Wc∈Rds×(ds+dh) context aur decoder state combine karne ke liye (parameters: ds(ds+dh)).
Bahdanau score function mein tanh nonlinearity kyun use hoti hai?
Alignment function ko decoder aur encoder states ke beech complex, non-linear relationships seekhne dene ke liye. Nonlinearity ke bina yeh simple linear transformation tak collapse ho jaata.