Beam search decoding
3.5.14· AI-ML › Sequence Models
Greedy decoding ki problem
Jab hum sequences generate karte hain (translation, captioning, summarization), hum chahte hain:
jahan aur .
Greedy decoding har step pe pick karta hai. Yeh kyun fail hota hai: locally-optimal choices ka product globally optimal NAHI hota.
Example: "Je suis étudiant" ka English mein translation.
- Greedy pick kar sakta hai: "I" (0.6) → "am" (0.7) → "student" (0.3) →
- Better path: "I" (0.6) → "am" (0.65) → "a" (0.9) → "student" (0.8) →
Greedy path ne "a" miss kar diya kyunki step 2 mein "am" akela achha lag raha tha—lekin baad mein "a" include karna kaafi zyada natural phrase banata hai.

Derivation: Beam search sequences ko kaise score karta hai
Step 1: Objective function
Hum log-probability maximize karna chahte hain (raw probability se numerically zyada stable):
Log kyun? Chhoti probabilities ka product underflow karta hai; logs ka sum range mein rehta hai. Aur: kyunki monotonic hai.
Step 2: Length normalization
Raw log-probability longer sequences ko penalize karta hai (sum mein zyada terms, har ek negative). Ek 10-word sentence almost hamesha 3-word sentence se kam score karega, chahe woh better ho.
Fix: Length se divide karo (smoothing ke saath):
Yahan generated tokens ki sankhya hai (matlab, hum <START> token count nahi karte, kyunki uski koi predicted probability nahi hoti). Neeche diye worked example mein, ek completed sequence jaise [<START>, Bonjour, <END>] ke generated tokens hain (Bonjour aur <END>).
- : full normalization (average log-prob per token)
- : no normalization (raw sum)
- : practice mein common hai (longer ke liye slight preference, kam penalty)
kyun? Bahut short sequences ki average probabilities deceptively high ho sakti hain (jaise, "I." ki avg prob high hai but incomplete hai). Partial normalization balance banata hai.
Step 3: Algorithm
Key insight: Har step pe hum candidates generate karte hain lekin sirf rakhte hain. Yeh exponential search space ( total sequences) ko prune karta hai, operations mein.
Worked example: "Hello" ka French mein translation
Setup: Vocabulary = {
Step 0: Initialize karo
- Beams:
[([<START>], 0.0)]
Step 1: <START> se expand karo
- Candidates:
[<START>, Bonjour]:[<START>, Salut]:[<START>, <END>]:
- Top 2 rakho:
Bonjour(-0.51),Salut(-1.05)
Yeh step kyun? Hum saare possible first words try karte hain, unhe model ki predicted probabilities se score karte hain, best 2 rakhte hain.
Step 2: Dono beams expand karo
Bonjourse:[<START>, Bonjour, <END>]:[<START>, Bonjour, Salut]:
Salutse:[<START>, Salut, <END>]:[<START>, Salut, Bonjour]:
Normalization ( generated tokens ki sankhya len(seq) - 1 se divide karo; yahan har completed sequence mein 2 generated tokens hain, toh ):
Bonjour <END>:Salut <END>:
Top 2 rakho: [<START>, Bonjour, <END>] (normalized -0.31), [<START>, Salut, <END>] (normalized -0.605).
Yeh step kyun? Dono sequences end ho gayi, toh hum unke length-normalized scores compare karte hain. "Bonjour" jeetta hai kyunki model ne ise zyada probability assign ki.
Final output: "Bonjour" (score -0.31)
Common hyperparameters
| Parameter | Typical range | Effect |
|---|---|---|
| Beam width | 4-10 | Zyada = better quality, slower. ~10 ke baad diminishing returns. |
| Length penalty | 0.6-1.0 | Zyada = longer sentences prefer karo. 0 = koi penalty nahi. |
| Min length | Task-dependent | Decoder ko kam se kam tokens generate karne par force karo (premature <END> se bachne ke liye). |
| N-gram blocking | 2-4 | Same -gram ko repeat hone se rokta hai (summarization mein useful). |
Doosre decoding methods ke saath comparison
| Method | Search space | Quality | Diversity Speed |
|---|---|---|---|
| Greedy | 1 path | Low | None |
| Beam search | paths | High | Low |
| Sampling (top-, temp) | Stochastic | Variable | High |
| Exhaustive | All | Optimal | N/A |
Beam search kab use karein:
- Machine translation, summarization, captioning (single best output chahiye)
- Jab fluency aur correctness creativity se zyada matter karti ho
Beam search kab use NA karein:
- Story generation, dialogue (diversity chahiye; beam search generic responses produce karta hai)
- Iske bajaye temperature ya nucleus sampling use karo
Connections
- Seq2seq models — Beam search seq2seq ke liye standard decoding method hai
- Attention mechanism — encoder states pe attention use karke compute karo
- Temperature sampling — Creative generation ke liye beam search ka alternative
- BLEU score — Translation mein beam search outputs evaluate karne ke liye use hone wala metric
Recall 12 saal ke bacche ko samjhao
Imagine karo tum ek word game khel rahe ho jahan tum ek sentence ek word ek baar mein banate ho, aur har word ka ek "goodness score" hota hai (kitna likely lagta hai).
Greedy waise hai jaise sirf apna current sentence yaad rakho aur hamesha best next word pick karo. Lekin kabhi-kabhi ek word jo "okay" lagta hai abhi, baad mein kaafi better sentence lead kar sakta hai! Jaise agar tum "I want to eat pizza" bana rahe ho, aur tum "I want cookies" pick karte ho kyunki "cookies" ne 90 score kiya vs "to" ne 80. Lekin "to eat pizza" ne 90 × 95 = 8550 total score kiya hota, "cookies" (90) se kaafi better jo wahan khatam ho gaya.
Beam search waise hai jaise tumhare top 3 sentences ke liye sticky notes rakho har step pe. Tum teeno mein words add karne ki koshish karte ho, 3 × 26 = 78 nayi sentences milti hain, phir sirf best 3 rakhte ho. Toh tum multiple "what if I said THIS instead" paths explore kar rahe ho, lekin itne nahi ki tumhara brain explode ho jaaye.
"Beam width" yeh hai ki tumhare paas kitne sticky notes hain. 1 note = greedy. 100 notes = tumhara desk ek mess hai aur 10 notes ke baad zyada benefit nahi mil raha.
#flashcards/ai-ml
Greedy decoding aur beam search ke beech key difference kya hai? :: Greedy har step pe single highest-probability token pick karta hai (myopic). Beam search candidate sequences maintain karta hai aur multiple paths explore karta hai, global sequence quality improve karta hai.
Beam search mein hum raw probabilities ki jagah log-probabilities kyun use karte hain?
Beam search mein length normalization kya hai aur yeh kyun zaroori hai?
<START> exclude karke). Iske bina, longer sequences hamesha lower score karte hain (zyada negative terms), toh model short, incomplete outputs prefer karta.Agar beam width ho, toh beam search kya ban jaata hai?
Length , vocabulary size , aur beam width ke saath beam search ki computational complexity kya hai? :: — steps mein se har ek par, hum sequences ko tokens consider karke expand karte hain.