Language has structure: the word "love" is more likely after "I" than after "quantum". Causal LM learns this conditional probability distribution over sequences:
Why factorize this way? By the chain rule of probability, any joint distribution can be written as a product of conditionals. The causal constraint means each P(wt∣w<t) is a forward-looking prediction, matching how we naturally produce language.
Why this factorization? The chain rule of probability: P(A,B)=P(A)P(B∣A), extended to T variables. The causal ordering (x1 first, then x2 given x1, etc.) is arbitrary but matches text's left-to-right flow.
Sequence: "I love ML" Tokens:x1=I,x2=love,x3=ML
Step-by-step loss:
Position 1:L1=−logPθ(x1=I∣∅)Why no context?x1 is the first token; model predicts from a start-of-sequence token or learns unconditional distribution.
Position 2:L2=−logPθ(x2=love∣x1=I)Why only x1? Causal constraint: model sees "I", predicts "love".
Position 3:L3=−logPθ(x3=ML∣x1=I,x2=love)Why both x1,x2? All previous context available.
Total loss:LCLM=L1+L2+L3
Concrete numbers: Suppose model outputs:
Pθ(I∣∅)=0.1 → L1=−log(0.1)≈2.30
Pθ(love∣I)=0.4 → L2=−log(0.4)≈0.92
Pθ(ML∣I love)=0.05 → L3=−log(0.05)≈3.00
Total:2.30+0.92+3.00=6.22
Why this number? Lower loss = model assigns higher probability to the true sequence. Perfect prediction (P=1) gives loss 0.
Why same objective works for both? Training learns Pθ(xt∣x<t) at every position. Generation repeatedly applies this learned conditional to extend the sequence.
Why causal for generation? At inference, you only have past tokens. Training objective must match test-time reality (no future available).
Why masked LM can't generate? BERT sees bidirectional context, but generation is left-to-right. Mismatch between training and inference.
Recall Explain to a 12-Year-Old
Imagine you're writing a story, one word at a time. You can look back at what you've already written, but you can't peek ahead at what comes next (because you haven't written it yet!).
Causal language modeling is teaching a robot to play this same game. The robot reads "Once upon a", then guesses the next word might be "time". If the real next word is "time", the robot gets a good score. If it guessed "banana", it gets a bad score and learns to do better.
The robot practices on millions of stories, learning patterns like:
"Once upon a ___" → usually "time"
"I feel ___" → often "happy", "sad", "excited"
"The dog ___" → probably a verb like "ran", "barked"
The trick is the robot never cheats by looking ahead. It only sees what it's already written, just like you. That's why it's called "causal"—like cause and effect, the past causes the future, not the other way around!
After enough practice, the robot becomes so good at guessing next words that it can write entire stories on its own, one word at a time.
Exposure Bias — Training-inference mismatch in autoregressive models
Sampling Strategies — How to generate from Pθ(xt∣x<t) (greedy, beam search, nucleus)
GPT Family — Models trained with causal LM objective
#flashcards/ai-ml
What is the causal language modeling objective? :: Minimize negative log-likelihood −∑t=1TlogPθ(xt∣x<t), predicting each token from only previous tokens (no future access).
Why is it called "causal"? :: Because prediction at position t can only depend on positions before t (cause → effect), preserving time's forward direction.
How does CLM factorize sequence probability?
By the chain rule: P(x)=∏t=1TP(xt∣x<t), decomposing joint probability into conditional next-token predictions.
What enforces causality in transformers?
Attention masking: positions >t get scores of −∞ before softmax, zeroing their contribution to position t's representation.
What is the loss for a single position t?
Lt=−logPθ(xt∣x<t), the negative log-probability assigned to the true token xt given past context.
Why use negative log-likelihood instead of probability product?
Numerical stability (logs avoid underflow), additivity (easy to sum), and convexity (better optimization properties).
What is the relationship between CLM and cross-entropy?
CLM is cross-entropy between the true next-token distribution (one-hot at correct token) and model's predicted softmax distribution.
How is causal LM used for generation?
Autoregressively sample xt∼Pθ(⋅∣x<t), then condition on sampled xt to predict xt+1, repeating until end-of-sequence.
What is perplexity in context of CLM?
PPL=exp(LCLM), the exponentiated average negative log-likelihood; lower perplexity = better predictions.
Why does CLM train on shifted targets?
Input at position t−1 predicts target at position t (input: "The cat sat", targets: "cat sat on"), implementing P(xt∣x<t).
What is exposure bias in causal LM?
Training uses true tokens as context (teacher forcing), but generation uses model's own predictions; errors compound since model never practiced recovering from mistakes.
Why can't BERT generate text like GPT?
BERT uses masked LM with bidirectional context, but generation is left-to-right (only past available); training-inference mismatch prevents autoregressive sampling.
Dekho yaar, causal language modeling ka core idea bahut simple hai - model ko sirf yeh sikhana hai ki ek sentence mein "next word" kya aayega, wo bhi sirf pichle words ko dekh ke, future ko bilkul dekhe bina. Jaise aap ek sentence left-to-right padh rahe ho aur har agle word ko guess kar rahe ho - "I..." ke baad "love" aane ki probability zyada hai, "quantum" ki kam. Yeh "causal" isliye kehte hain kyunki position t ki prediction sirf uske pehle wale tokens (x<t) pe depend kar sakti hai, matlab cause pehle aata hai aur effect baad mein, time ki direction preserve hoti hai.
Ab maths ki taraf dekho toh yeh chain rule of probability se aata hai. Kisi bhi poore sentence ki probability ko hum chote-chote conditional probabilities ke product mein tod sakte hain - pehle P(w1), phir P(w2∣w1), aur aise aage. Model ko train karne ke liye hum negative log-likelihood minimize karte hain, jo actually cross-entropy loss ban jaata hai. Log lene ke teen faayde hain: chote probabilities multiply karne se numbers underflow ho jaate hain (log se yeh problem nahi hoti), loss additive ban jaata hai (batch ka total loss bas sum ho jaata hai), aur optimization convex ho jaati hai. Practically, transformer har position pe logits deta hai, softmax se woh probabilities banti hain, aur attention mein "causal masking" lagti hai taaki model cheating karke future tokens na dekh sake.
Yeh matter isliye karta hai kyunki yahi objective aaj ke saare bade text-generation models - jaise GPT family - ke peeche ka fundamental hai. Jab model yeh forward-looking prediction achhe se seekh leta hai, toh wo naya coherent text generate kar sakta hai, ek-ek token karke. Toh agar aap language models ko deeply samajhna chahte ho, yeh objective aapka foundation hai - baaki sab isi ke upar build hota hai.