3.5.14 · HinglishSequence Models

Beam search decoding

2,489 words11 min readRead in English

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.

Figure — Beam search decoding

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 = {, Bonjour, Salut, }, beam width , .

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

  • Bonjour se:
    • [<START>, Bonjour, <END>]:
    • [<START>, Bonjour, Salut]:
  • Salut se:
    • [<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?
Numerical underflow avoid karne ke liye (chhoti numbers ka product floating point mein zero ho jaata hai) aur kyunki products ko sums mein convert karta hai, jo compute aur compare karna aasan hai.
Beam search mein length normalization kya hai aur yeh kyun zaroori hai?
Cumulative log-probability ko se divide karna, jahan generated tokens ki sankhya 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?
Greedy decoding—sirf single highest-probability sequence har step pe rakhi jaati 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.

Beam search globally optimal sequence dhundhne ki guarantee kyun nahi deta?
Yeh search tree prune karta hai—agar true best sequence ka ek token early pe low-probability hai jo top se bahar gir jaata hai, toh woh path discard ho jaata hai aur kabhi explore nahi hota, chahe baad ke tokens usse optimal banate.
Production translation systems mein beam width ki typical range kya hai?
to . Zyada values ke diminishing returns hain aur length bias amplify ho sakti hai.
Tum beam search ki jagah sampling methods kab use karoge?
Creative generation tasks (stories, dialogue, diverse responses) ke liye jahan variety aur personality chahiye, single most probable (often generic) output nahi.
Length penalty hyperparameter kya control karta hai?
Hum sequence length se kitni strongly normalize karte hain. : koi normalization nahi (short sequences prefer karta hai). : full normalization (average per token). : common compromise.

Concept Map

requires

solved by

picks local max

fails at

motivates

keeps top k

expand by V tokens

uses

avoids underflow

penalizes long seqs

controlled by

width k=1

Find best sequence y*

Product of conditional probs

Greedy decoding

Myopic choices

Not globally optimal

Beam search

Partial sequences beam

Score k times V candidates

Sum of log-probs

Numerical stability

Length normalization

Alpha exponent