4.2.6 · HinglishTokenization & Language Modeling

Causal language modeling objective

2,344 words11 min readRead in English

4.2.6 · AI-ML › Tokenization & Language Modeling

Yeh Kaunsi Problem Solve Karta Hai?

Language mein structure hota hai: "love" word "I" ke baad zyada likely hai, "quantum" ke baad nahi. Causal LM sequences par yeh conditional probability distribution seekhta hai:

Is tarah factorize kyun karte hain? Chain rule of probability ke according, koi bhi joint distribution conditionals ke product ke roop mein likhi ja sakti hai. Causal constraint ka matlab hai ki har ek forward-looking prediction hai, jo bilkul waise match karta hai jaise hum naturally language produce karte hain.

Figure — Causal language modeling objective

First Principles Se Derivation

Step 1: Maximum Likelihood Se Shuru Karo

Hum chahte hain ek model jo real text ko high probability assign kare. Sequences ke ek dataset ke liye, maximize karo:

Product kyun? Training examples ke beech independence assume karte hain.

Step 2: Har Sequence Par Chain Rule Apply Karo

Ek sequence ke liye:

Yeh factorization kyun? Probability ka chain rule: , variables tak extend kiya. Causal ordering ( pehle, phir given , etc.) arbitrary hai lekin text ke left-to-right flow se match karta hai.

Step 3: Optimization Ke Liye Negative Log Lo

Product maximize karna = negative log-likelihood minimize karna:

Negative log kyun?

  1. Numerical stability: Chhoti probabilities ke products underflow ho jaate hain; logs ke sums nahi hote.
  2. Additivity: Ek batch par loss sirf per-example losses ka sum hota hai.
  3. Convexity: Log-loss exponential families ke liye convex hoti hai (jaise softmax output).

Step 4: Dataset Par Average Karo

Yeh true next-token distribution (one-hot) aur model ki predicted distribution ke beech cross-entropy loss hai.

Worked Examples

Example 1: Tiny Sequence

Sequence: "I love ML" Tokens:

Step-by-step loss:

  1. Position 1: Context nahi kyun? pehla token hai; model ek start-of-sequence token se predict karta hai ya unconditional distribution seekhta hai.

  2. Position 2: Sirf kyun? Causal constraint: model "I" dekhta hai, "love" predict karta hai.

  3. Position 3: dono kyun? Saara previous context available hota hai.

Total loss:

Concrete numbers: Maano model yeh output karta hai:

Total:

Yeh number kyun? Kam loss = model true sequence ko zyada probability assign karta hai. Perfect prediction () se loss milta hai.

Example 2: Generation vs. Training

Training:

  • Input: "The cat sat on the"
  • Targets: "cat sat on the mat" (1 se shift hua)
  • Loss:

Generation (autoregressive sampling):

  • Start: "The"
  • sample karo → "cat" milta hai
  • sample karo → "sat" milta hai
  • End-of-sequence token tak continue karo

Dono ke liye same objective kyun kaam karta hai? Training har position par seekhta hai. Generation repeatedly is seekhe hue conditional ko sequence extend karne ke liye apply karta hai.

Causal vs. Other Objectives Kyun?

Objective Prediction Mask Use Case
Causal LM Next token Future positions Generation (GPT, GPT-2, GPT-3)
Masked LM Masked tokens Random positions Understanding (BERT)
Prefix LM Next token Future in suffix Hybrid (encoder-decoder)

Generation ke liye causal kyun? Inference par, tumhare paas sirf past tokens hote hain. Training objective ko test-time reality se match karna chahiye (future available nahi hai).

Masked LM generate kyun nahi kar sakta? BERT bidirectional context dekhta hai, lekin generation left-to-right hoti hai. Training aur inference ke beech mismatch hai.

Recall Ek 12-Saal-Ke Bacche Ko Samjhao

Socho tum ek story likh rahe ho, ek word at a time. Tum jo likh chuke ho woh dekh sakte ho, lekin aage kya aayega yeh nahi dekh sakte (kyunki tumne abhi likha hi nahi!).

Causal language modeling ek robot ko yahi game sikhana hai. Robot "Once upon a" padhta hai, phir guess karta hai agla word "time" ho sakta hai. Agar real agla word sach mein "time" hai, to robot ko achcha score milta hai. Agar usne "banana" guess kiya, to bura score milta hai aur woh better karna seekhta hai.

Robot laakhon stories par practice karta hai, aaise patterns seekhta hai jaise:

  • "Once upon a ___" → usually "time"
  • "I feel ___" → often "happy", "sad", "excited"
  • "The dog ___" → probably ek verb jaise "ran", "barked"

Trick yeh hai ki robot kabhi cheat nahi karta aage dekh ke. Woh sirf wahi dekhta hai jo already likh chuka hai, bilkul tumhari tarah. Isliye ise "causal" kehte hain — jaise cause aur effect, past future ko cause karta hai, na ki ulta!

Kaafi practice ke baad, robot next words guess karne mein itna achcha ho jaata hai ki woh khud poori stories likh sakta hai, ek word at a time.

Connections

  • Autoregressive Models — Causal LM text par apply kiya gaya autoregressive paradigm hai
  • Cross-Entropy Loss — CLM objective ka mathematical form
  • Transformer Decoder — Causal self-attention implement karne wala architecture
  • Teacher Forcing — Training technique jahan true tokens (predictions nahi) context provide karte hain
  • Perplexity — Evaluation metric: , jitna kam utna better
  • Masked Language Modeling — Bidirectional understanding ke liye contrasting objective (BERT)
  • Exposure Bias — Autoregressive models mein training-inference mismatch
  • Sampling Strategies se kaise generate karein (greedy, beam search, nucleus)
  • GPT Family — Causal LM objective se train kiye gaye models

#flashcards/ai-ml

Causal language modeling objective kya hai? :: Negative log-likelihood minimize karo, har token ko sirf previous tokens se predict karo (future access nahi).

Ise "causal" kyun kehte hain? :: Kyunki position par prediction sirf se pehle ki positions par depend kar sakti hai (cause → effect), time ki forward direction preserve hoti hai.

CLM sequence probability ko kaise factorize karta hai?
Chain rule se: , joint probability ko conditional next-token predictions mein decompose karta hai.
Transformers mein causality kaise enforce hoti hai?
Attention masking: positions ko softmax se pehle scores milte hain, position ki representation mein unka contribution zero ho jaata hai.
Ek single position ka loss kya hai?
, true token ko past context diye assign ki gayi negative log-probability.
Probability product ki jagah negative log-likelihood kyun use karte hain?
Numerical stability (logs underflow avoid karte hain), additivity (sum karna aasaan hai), aur convexity (better optimization properties).
CLM aur cross-entropy mein kya relation hai?
CLM, true next-token distribution (correct token par one-hot) aur model ki predicted softmax distribution ke beech cross-entropy hai.
Generation ke liye causal LM kaise use hota hai?
Autoregressively sample karo, phir sampled par condition karke predict karo, end-of-sequence tak repeat karo.
CLM ke context mein perplexity kya hai?
, exponentiated average negative log-likelihood; kam perplexity = better predictions.
CLM shifted targets par kyun train karta hai?
Position par input, position par target predict karta hai (input: "The cat sat", targets: "cat sat on"), implement karta hai.
Causal LM mein exposure bias kya hai?
Training mein context ke liye true tokens use hote hain (teacher forcing), lekin generation mein model ke apne predictions use hote hain; errors compound ho jaate hain kyunki model ne kabhi apni galtiyon se recover karna practice nahi kiya.
BERT GPT ki tarah text generate kyun nahi kar sakta?
BERT bidirectional context ke saath masked LM use karta hai, lekin generation left-to-right hai (sirf past available); training-inference mismatch autoregressive sampling prevent karta hai.

Concept Map

trains to

constrained by

enforced by

justifies

defines

maximizes

becomes

via negative log

objective is

enables

Causal Language Modeling

Predict next token

Never look ahead

Chain rule of probability

Product of conditionals

Maximum likelihood

Negative log-likelihood loss

Attention masks

Text generation

Numerical stability