4.4.9 · AI-ML › Alignment, Prompting & RAG
Intuition Ek-sentence wali idea
In-context learning (ICL) tab hota hai jab ek frozen language model apne prompt mein rakhe examples se ek naya task seekhta hai , bina kisi weight update ke — "learning" poori tarah forward pass ke andar, context pe condition karke hoti hai.
Definition In-context learning (ICL)
Ek pretrained LLM ek task perform karta hai ek aisi prompt pe condition karke jisme demonstrations (input–output pairs) aur ek naya query hota hai. Formally, diye gaye demos ( x 1 , y 1 ) , … , ( x k , y k ) aur ek test input x k + 1 ke liye, model output karta hai:
y ^ = arg max y P θ ( y ∣ x 1 , y 1 , … , x k , y k , x k + 1 )
Weights θ fixed hain. Kuch bhi train nahi hota. Ise k-shot learning kehte hain (k = 0 matlab zero-shot, k = 1 matlab one-shot).
YEH EXIST KYUN KARTA HAI? Pretraining ke dauran model itna zyada text dekhta hai jahan document mein pehle establish hua pattern baad ke tokens ko predict karta hai (lists, tables, translations, Q&A threads). Next-token loss minimize karne ke liye usse majburan "context mein pattern dhundho, phir use continue karo" mein achha banna padta hai. ICL wahi skill hai jo inference time pe reuse hoti hai.
Intuition Story 1 — Task location (Bayesian view)
Pretraining ek aisa model banata hai jo behave karta hai jaise kai latent tasks/concepts ka mixture p ( y ∣ x ) = ∑ θ ∗ p ( y ∣ x , θ ∗ ) p ( θ ∗ ) . Demonstrations koi nayi function sikhate nahi; woh posterior ko shift karti hain ki model kon sa latent task kar raha hai. Zyada/clearer demos ⇒ posterior sahi task pe collapse hoti hai ⇒ behtar answers.
Intuition Story 2 — Implicit optimization ("forward pass ke andar learning")
Attention layers ek update rule implement kar sakti hain. Demos padhna aur residual stream adjust karna equivalent hai ek internal task pe gradient descent ka ek chhota step chalane ke. Prompt "training data" hai, forward pass "training loop" hai, lekin koi bahari weights nahi hilte.
Dono stories compatible hain: model ke andar already algorithm hai; context use select aur steer karta hai.
Demos evidence accumulation se madad karte hain, re-training se nahi. Jab posterior already collapse ho chuki hoti hai, extra demos diminishing returns dete hain (KL "budget" khatam ho jaata hai) — yeh empirical plateau se match karta hai.
Worked example Example 1 — Sentiment (few-shot)
Review: "Loved every minute." → Positive
Review: "Total waste of money." → Negative
Review: "The plot dragged on." → ???
Kyun kaam karta hai: do demos p ( c = sentiment classification ) badhate hain; label format ("→ Positive/Negative") model ko output space batata hai. Model marginalizes → Negative predict karta hai.
Worked example Example 2 — Label format correctness se zyada matter karta hai
Demos do jisme randomly flipped labels hon (aadhe galat). Surprisingly, accuracy aksar zyada rehti hai.
Kyun? Demos mainly convey karte hain (a) input distribution , (b) label space , (c) format — matlab woh task locate karte hain. Exact ground-truth mapping partly pretraining priors se recover hoti hai. Steel-man: yahi reason hai ki "labels matter nahi" wale claims aate hain — lekin harder/novel tasks ke liye correct labels ZAROOR matter karte hain, kyunki model kisi prior pe fall back nahi kar sakta.
Worked example Example 3 — Recency & ordering
A→1, B→2, C→3, then B→?
Models recent demos aur majority label ki taraf biased hote hain. Demos ko reorder karna predictions flip kar sakta hai — yeh symptom hai ki ICL conditioning hai, clean learning nahi.
Common mistake "Model examples se apne weights update karta hai."
Kyun sahi lagta hai: woh behave karta hai jaise usne seekha. Fix: weights frozen hain; demos sirf context mein tokens ki tarah enter hote hain. "Adaptation" transient activations mein rehti hai aur prompt khatam hone pe gayab ho jaati hai.
Common mistake "Zyada shots hamesha help karte hain."
Kyun sahi lagta hai: early demos bade gains dete hain. Fix: gains p ( c ∗ ∣ D k ) → 1 exponentially follow karte hain, isliye woh saturate ho jaate hain; uske baad mostly cost, context-length pressure, aur recency noise badhta hai.
Common mistake "Demo labels sab sahi hone chahiye."
Kyun sahi lagta hai: supervised learning mein hone chahiye. Fix: ICL mein, format aur label-space aksar dominate karte hain; label correctness sabse zyada novel/hard tasks ke liye matter karta hai jise prior solve nahi kar sakta.
Common mistake "ICL aur fine-tuning same hain."
Fix: fine-tuning θ permanently change karta hai; ICL per-prompt aur reversible hai. ICL ≈ Bayesian conditioning / ek virtual GD step andar ek forward pass ke.
Recall Feynman: ek 12-saal ke bachhe ko explain karo
Socho ek bahut smart tota hai jisne poora internet padha hai. Tum usse dobara train nahi karte. Bas pehle kuch examples dikhate ho: "cat→animal, rose→plant, dog→?" Kyunki usne pehle lakho chhote lists aise dekhe hain, woh samajh jaata hai ki tum konsa game khel rahe ho aur pattern complete karta hai: "animal". Usne koi test ke liye padhaa nahi — usne bas woh rule notice kiya jo tum usके saamne hint kar rahe the. Yahi noticing-and-continuing in-context learning hai.
"LOCATE" se yaad rakho
L atent task • O rdering matters • C ondition (no weight change) • A ttention ≈ one GD step • T ask posterior shots ke saath collapse hoti hai • E vidence, format & label-space > raw labels.
In-context learning kya hai? Ek frozen LLM jo prompt mein demonstrations pe condition karke ek naya task perform karta hai, bina kisi weight update ke; "learning" forward pass mein hoti hai.
ICL mein model ke parameters mein kya badlta hai? Kuch nahi — θ fixed rehta hai; sirf transient activations context pe depend karti hain.
k-shot ICL objective state karo. y ^ = arg max y P θ ( y ∣ x 1 , y 1 , … , x k , y k , x k + 1 ) .
Bayesian view: demonstrations kya karte hain? Woh latent tasks ke upar posterior p ( c ∣ D k ) shift karte hain, use true task pe concentrate karte hue.
Zyada shots ke saath accuracy saturate kyun hoti hai? Log-posterior-ratio ≈ k ⋅ KL badhta hai, isliye p ( c ∗ ∣ D k ) → 1 exponentially; extra demos diminishing returns dete hain.
Ek GD step linear attention ke barabar kyun hai? W 0 = 0 se GD deta hai y ^ = η ∑ i y i ( x i ⊤ x k + 1 ) , same form jaise attention with keys x i , values y i , query x k + 1 .
Flipped labels ke baad bhi ICL accuracy achhi kyun ho sakti hai? Demos mainly input distribution, label space, aur format convey karte hain (task location); pretraining priors mapping supply karti hai — jab tak task novel/hard na ho.
Zero-shot vs one-shot vs few-shot? k=0,1,kai demonstrations respectively.
Do biases jo batate hain ki ICL conditioning hai clean learning nahi? Recency bias (baad ke demos zyada weighted) aur majority-label bias.
ICL vs fine-tuning ek line mein? Fine-tuning permanently weights edit karta hai; ICL per-prompt, reversible conditioning hai.
Transformers and Attention — attention woh substrate hai jo ICL implement karta hai.
Prompt Engineering — demo count, order, aur format ICL ke levers hain.
Chain-of-Thought Prompting — ICL extended with reasoning steps in the demos.
Retrieval-Augmented Generation (RAG) — retrieved passages context ki tarah act karte hain jo same conditioning mechanism ko steer karta hai.
Bayesian Inference — task-posterior derivation.
Gradient Descent — implicit-optimization interpretation.
Fine-tuning vs Prompting — kab weights change karein vs context se steer karein.
Pretraining on pattern-rich text
Frozen weights theta fixed
k demonstrations plus query
Story 1 task location Bayesian
Story 2 implicit optimization
Posterior over latent task
Attention as gradient descent