1.3.19 · D3 · HinglishProbability & Statistics

Worked examplesCross-entropy concept

3,096 words14 min read↑ Read in English

1.3.19 · D3 · AI-ML › Probability & Statistics › Cross-entropy concept

Yeh page parent note ki "brute-force" companion hai. Wahan humne definition banai thi. Yahan hum har tarah ka input dhundte hain jo cross-entropy face kar sakti hai, aur har ek ko poora work karte hain — taaki koi bhi case aisa na ho jo tumne pehle na dekha ho.

Shuru karne se pehle, ek reminder us machine ka jisme hum numbers daal rahe hain, aur do supporting symbols jo hum baar baar use karenge.

Neeche sab kuch yeh hai: chuno, chuno, surprise ko reality se weight karo, add up karo. Bar-chart figures dikhate hain ki (blue) aur (orange) actually side by side kaisi dikhti hain.


Scenario matrix

Cross-entropy do probability vectors leti hai. "Scenarios" is baat se aate hain ki woh vectors kaisi dikh sakti hain — hard vs soft truth, good vs bad guess, aur woh dangerous edges jahan probability ya ho.

# Cell (case class) Kya special hai Example
A Soft truth, soft guess, close Dono distributions spread out, model near-correct Ex 1
B Soft truth, soft guess, mismatched Model galat direction mein confident Ex 2
C Hard (one-hot) truth, decent guess True vector jaisa , model reasonable Ex 3
D Hard truth, confidently WRONG guess Huge-penalty wala case Ex 4
E Degenerate: jahan , blow-up Ex 5
F Degenerate: term — kya yeh vanish ho jaata hai? Ex 5
G Perfect match (lower bound) Cross-entropy apne floor tak pahunchti hai Ex 6
H Real-world word problem Ek story ko mein translate karna Ex 7
I Exam twist: label smoothing Truth deliberately soften ki gayi hai Ex 8
J Soft truth, HARD (one-hot) guess Model ek class pe commit karta hai; finite hai jab tak woh ek class pe commit na kare Ex 8b
K Ek single term ka limiting behaviour kaise badhta hai jab ya Ex 9 (figure)

Ab hum har row sweep karte hain.


Figure — Cross-entropy concept

Bar chart dekho: blue truth bars aur orange guess bars almost overlap kar rahe hain — yahi visual near-coincidence hai isliye hum expect karte hain ki loss floor se barely upar baithega.

Step 1 — Surprises likhо. , . Yeh step kyun? Surprise per-outcome hai aur model ki probability use karta hai — yeh woh "code length" hai jise humne commit kiya tha.

Step 2 — Reality se weight karo aur sum karo. Yeh step kyun? Reality decide karti hai ki har surprise actually kitni baar hoti hai, isliye hum se nahi, se weight karte hain.

Verify. True entropy . Kyunki hamesha hota hai ( rule definition se), aur ✓. Gap nats bahut chhota hai — "close guess" se match karta hai. Units: nats ✓.


Figure — Cross-entropy concept

Bar chart mein orange guess ab blue truth ke relative flipped hai — wahan tall hai jahan truth chhoti hai. Yeh crossing mismatched guess ka visual signature hai aur warn karta hai ki loss bada hoga.

Step 1 — Surprises. , . Yeh step kyun? Same machine; sirf change hua.

Step 2 — True se weight karo. Yeh step kyun? Barish 70% time hoti hai, aur har rainy day model bahut surprised hota hai — us badi surprise par heavy weight loss ko inflate karta hai.

Verify. Ex 1 se ✓. nats — bada mismatch cost, jaisa expect tha. Phir bhi ✓.


Figure — Cross-entropy concept

Chart mein notice karo: blue truth "cat" par ek single full bar hai (yahi one-hot dikhta hai). Har orange bar jo zero blue bar ke upar baitha hai woh multiply away ho jayega.

Step 1 — Zero-weight terms hatao. Yeh step kyun? , toh woh surprises actually kabhi hoti nahi — unka contribution hai .

Step 2 — Ek survivor evaluate karo.

Verify. One-hot truth ke liye, (ek known cat ke baare mein koi uncertainty nahi), isliye — poora loss hi mismatch hai. Sanity: perfect deta ✓. Yeh exactly negative-log-likelihood shortcut hai aur MLE se link karta hai.


Step 1 — Sirf true class matter karti hai. Yeh step kyun? One-hot truth phir se dog/bird terms ko zero kar deti hai — cross-entropy sirf woh probability padhti hai jo model ne jo actually hua usse di thi.

Step 2 — Evaluate karo.

Verify. Ex 3 () vs yahan () compare karo: true-class probability ko se tak drop karne se loss ~8× multiply ho gaya. Confident wrongness ke liye yeh steep penalty hi wajah hai ki hum MSE par cross-entropy prefer karte hain. Note karo ki model ki "dog" mein confidence () kabhi enter nahi hui — cross-entropy galat cheez ke baare mein sure hone ko reward nahi karta ✓.


Sub-case E — zero jahan positive hai. Step 1. . Yeh step kyun? True class probability se hoti hai, lekin model ne swear kiya ki yeh impossible hai. Step 2. . Infinite loss. Yeh kyun matter karta hai: ek model jo reality ko rule out karta hai infinitely surprised hota hai. Isliye real code ek tiny floor add karta hai (e.g. ) — ya Label Smoothing use karta hai taaki koi probability exactly kabhi na ho.

Sub-case F — Ek term ke liye zero hai. Yahan dog term hai . Kyunki ek finite number hai (), product genuinely hai. Yeh step kyun? Danger tabhi aata hai jab wahan bhi ho. Jab tak hai, zero weight term ko cleanly delete karta hai. (Convention ke anusaar , iska limit.)

Verify. E: ✓. F: exactly ✓. Toh sirf dangerous zero hai true class par.


Step 1 — Plug in karo. Yeh step kyun? Jab hota hai toh code lengths pehle se reality ke liye optimal hain — zero wasted bits.

Step 2 — Evaluate karo. , .

Verify. Yeh construction se Shannon Entropy ke equal hai, isliye ✓. Aur ek lower bound hai: koi bhi isse beat nahi kar sakta ( rule phir se). Yeh floor isliye hai ki hum classification loss ko tak drive nahi kar sakte jab tak truth one-hot na ho ().


Step 1 — Story ko vectors mein translate karo. Truth ; guess . Yahan (spam / ham). Yeh step kyun? "30% truly spam hain" frequency hai — yeh hai. "Model hamesha 50-50 kehta hai" hai.

Step 2 — Surprises equal hain. dono classes ke liye. Yeh step kyun? Ek uniform guess har jagah same code length assign karta hai.

Step 3 — Weighted sum. Yeh step kyun? Jab outcomes par uniform ho, toh har surprise hoti hai, aur -weights sum to hote hain, isliye regardless of (yahan ).

Verify. ✓. Compare karo . Wasted bits nats — commit karne se inkaar karne ki price. Ek smarter model jo output kare exactly itna save kar leta.


Step 1 — Note karo ki ab har term count karti hai. Yeh step kyun? Yeh exactly parent ka Pitfall 1 hai: soft ka matlab hai ki hum terms drop nahi kar sakte. Dekho Label Smoothing.

Step 2 — Surprises. , (do baar). Yeh step kyun? Har surprise "is outcome ka mujhe kitne nats cost honge" model ke se padha jaata hai — aur kyunki chhota hai, uski surprise () badi hai, confident bar ki surprise se das guna. Bar jitna chhota, surprise utni badi; yeh inverse relationship hi ki poori geometry hai.

Step 3 — Weighted sum. Yeh step kyun? Other classes par do chhote weights ab model ko wahan thodi probability rakhne ke liye nudge karte hain — yahi label smoothing kharidta hai (calibration, kam overconfidence).

Verify. Same par one-hot loss hota . Smoothed loss ✓ — extra do side terms se aata hai. Floor check: for , aur ✓.


Step 1 — Near-one-hot guess, finite case. Surprises: , (do baar). Yeh step kyun? Hard-ish guess "galat" surprises huge banata hai (), lekin truth unhe sirf each weight karti hai — toh woh contribute karte hain lekin explode nahi karte.

Step 2 — Weighted sum. Yeh step kyun? Kyunki phir bhi har side class par rakhta hai, over-confidence penalised hoti hai — ek soft truth one-hot guesses se ladti hai. Yahi exactly wajah hai ki label smoothing arrogant outputs discourage karta hai.

Step 3 — Degenerate hard-wrong guess. Agar ho, toh cat term hai , toh . Yeh step kyun? Same rule jaisa Cell E: ek positive truth weight () times blow up karta hai. Ek one-hot guess tabhi safe hai jab woh exactly aise classes par land kare jis par truth bhi care karti hai.

Verify. Finite case Ex 8 se (arrogance mild guess se zyada cost karti hai) ✓, aur ✓. Degenerate case: ✓.


Figure — Cross-entropy concept

Yeh figure kaise padhen. Horizontal axis hai, woh probability jo tumhara model us class ko assign karta hai jo actually hua — yeh (model ne swear kiya ki impossible tha) se (model fully certain aur correct tha) tak jaati hai. Vertical axis resulting cross-entropy loss nats mein hai. Blue curve ko left-to-right follow karo: yeh top-left () se right edge par tak plunge karta hai. Teen coloured dots neeche ke checkpoints mark karte hain.

Step 1 — Endpoints padho. : (perfectly confident aur correct, koi surprise nahi). : (confident aur impossibly wrong — Ex 5 ka blow-up). Yeh step kyun? Yeh woh extreme corners hain jinke beech har classifier rehta hai.

Step 2 — Middle checkpoints padho. ; ; . Yeh step kyun? Notice karo ki curve convex hai aur left par steep hai — slope hai , isliye par gradient magnitude hai, versus par . Yahi "confidently wrong hone par strong gradient" property hai parent ke MSE comparison se.

Verify. Slope : par yeh hai, par ✓. Convexity () guarantee karta hai par single minimum ✓. Yeh geometric root hai isliye cross-entropy fast train karta hai — dekho Logistic Regression.


Recall Quick self-check

One-hot truth , model — loss kya hai? ::: nats (sirf true-class term bachta hai). Kaun sa single zero cross-entropy ko infinite banata hai? ::: us class par jahan ho (model reality ko rule out kar deta hai). Agar ho, toh cross-entropy kiske equal hoti hai? ::: Shannon entropy — iska minimum, ke saath. Uniform guess kyun deta hai regardless of ? ::: Har surprise equal hoti hai , aur -weights sum to karte hain.

Related deep concepts: KL Divergence, Categorical Cross-Entropy, Mutual Information.