1.3.19 · D4 · HinglishProbability & Statistics

ExercisesCross-entropy concept

2,157 words10 min read↑ Read in English

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

Yeh parent Cross-Entropy note ke liye ek self-testing worksheet hai. Har problem apna level batati hai (L1 Recognition → L5 Mastery). Pehle khud try karo, phir collapsible solution kholna. Poore worksheet mein natural log (, units = nats) use karo jab tak problem "bits" () na kahe.

Shuru karne se pehle, ek reminder un sabhi symbols ka jo hum use karenge, saral shabdon mein:

Poore page ke liye dimag mein rakhne wali picture — surprise as a curve:

Figure — Cross-entropy concept

Dhyan do: jaise predicted probability ki taraf slide hoti hai, surprise infinity ki taraf shoot karti hai. Yeh akela curve explain karta hai ki confident-wrong predictions ko itni buri tarah kyun punish kiya jaata hai — yahi poore worksheet ki theme hai.


Level 1 — Recognition

Exercise 1.1

relative to ki cross-entropy kaun sa formula hai? (a) (b) (c) (d)

Recall Solution

(b). Left to right padho: "reality se weight karo, guess ki surprise lo." Option (a) roles swap karta hai (woh hoga, ek alag number). Option (c) hai (galat sign, aur koi nahi). Option (d) mean squared error hai, cross-entropy nahi.

Exercise 1.2

True ya false: ordinary probability distributions ke liye cross-entropy negative ho sakti hai (jab sabhi ).

Recall Solution

False. Har hai, isliye , isliye . Non-negative surprises ka sum jo non-negative se weighted hai, woh hoga. (Discrete distributions ke liye yeh sirf tab pohonch sakta hai jab probability us ek outcome par daalta ho jiske baare mein certain hai.)


Level 2 — Application

Exercise 2.1

Ek biased coin heads par true probability ke saath girti hai. Tumhara model guess karta hai (perfect). nats mein compute karo aur confirm karo ki yeh ke barabar hai.

Recall Solution

Do outcomes: heads , tails . Kyunki hai, "log ke andar" aur "bahar" ke numbers identical hain, isliye yeh hi hai. Waste . Yeh floor hai: koi bhi guess perfect se behtar nahi ho sakta.

Exercise 2.2

Wahi true coin ( heads), lekin ab model heads guess karta hai. aur wasted bits compute karo.

Recall Solution

Waste: nats. Isko padhna: model heads ke baare mein over-confident hai. Jab tails aata hai (40% time) toh woh huge surprise pay karta hai, jo average ko floor se bahut upar kheench deta hai.

Exercise 2.3

Teen-class problem, one-hot truth (answer class 2 hai). Model predict karta hai . Loss compute karo.

Recall Solution

One-hot truth har term ko zero kar deta hai sirf true class ko chodkar: Sirf woh probability matter karti hai jo model ne correct class ko di. Dekho Categorical Cross-Entropy.


Level 3 — Analysis

Exercise 3.1

Soft (smoothed) labels. Truth one-hot nahi hai: (ek label-smoothed cat, dekho Label Smoothing). Model predict karta hai (exactly match). aur compute karo.

Recall Solution

Kyunki hai, . Key lesson: one-hot case ke unlike, yahan loss floor nahi hai — ek perfect model bhi nats pay karta hai kyunki labels khud uncertain hain. Yahi wajah hai ki label smoothing minimum achievable loss ko change karta hai.

Exercise 3.2

Numerically dikhao ki cross-entropy symmetric nahi hai: aur ke saath, dono aur compute karo aur confirm karo ki woh alag hain.

Recall Solution

Ruko — sign dhyan se: , , sum nats. , isliye . Dono sawaal "surprised jab reality ho lekin maine assume kiya" versus uska swap genuinely alag hain.

Exercise 3.3

aur fact use karte hue, prove karo ki fixed truth ke liye sabse chhoti possible cross-entropy hai, jo sirf tab achieve hoti hai jab .

Recall Solution

mein nahi hai, isliye jab hum model vary karte hain toh yeh ek constant hai. Tab Ek constant plus ek non-negative quantity tab minimize hoti hai jab non-negative part apne minimum par ho. Aur iff (Gibbs' inequality). Isliye at . Yahi wajah hai ki cross-entropy minimize karna model ko reality ki taraf kheenchta hai (equivalently, Maximum Likelihood Estimation ki taraf).


Level 4 — Synthesis

Exercise 4.1

Ek logistic-regression model (dekho Logistic Regression) positive class ke liye output karta hai, aur true label hai. Per-sample loss hai . derive karo aur dikhao ki yeh ke barabar hai, jahan aur .

Recall Solution

Chain rule, teen clearly dikhne wale steps mein. Pehla: , isliye . Doosra: . Combine: Kyun yeh beautiful hai: log se aane wala messy sigmoid derivative ke ko exactly cancel kar deta hai, aur clean prediction minus target bachi rahti hai. Woh cancellation hi asli wajah hai ki cross-entropy sigmoid/softmax ke saath itni naturally pair kyun hoti hai — jab model confidently galat ho tab koi vanishing gradient nahi.

Exercise 4.2

Gradient signal contrast karo. Maano true lekin (confidently galat). Cross-entropy gradient magnitude compute karo aur ise MSE gradient magnitude se compare karo, jahan loss same sigmoid ke saath hai, jo hai .

Recall Solution

Cross-entropy: . Ek strong push. MSE: . MSE gradient roughly hai, lagbhag kamzor, kyunki factor exactly tab ki taraf collapse karta hai jab model saturate hota hai. Conclusion: MSE confident mistakes par stall karta hai; cross-entropy push karta rehta hai. Yeh parent note ke Example 3 ka quantitative version hai.


Level 5 — Mastery

Exercise 5.1

Full loss decompose karo. Truth (soft label), model (maximally unsure). , , aur compute karo, aur identity verify karo.

Recall Solution

(Dono classes ko milta hai, isliye loss sirf hai chahe kuch bhi ho.) Identity check karo: Interpretation: model class 2 ki taraf commit karne se inkaar karke nats waste karta hai, upar se nats irreducible label uncertainty ke.

Exercise 5.2

Optimal constant predictor. Ek bade dataset par empirical class frequencies hain. Agar ek model ko har sample ke liye same output karne pe majboor kiya jaaye, toh average cross-entropy minimize karne wala kaun sa hai, aur minimum value kya hai?

Recall Solution

Exercise 3.3 se, average cross-entropy tab minimize hoti hai jab ho. Isliye . Minimum loss : Kyun yeh mastery insight hai: data par trained cross-entropy model ki output distribution ko empirical class frequencies ki taraf drive karti hai — yahi maximum-likelihood answer hai. Koi bhi ek positive add karta hai.

Exercise 5.3

Bits vs nats. Exercise 5.2 ke answer ko bits mein convert karo.

Recall Solution

Same information content, alag ruler. Nats base use karte hain; bits base use karte hain; nats ko bits mein convert karne ke liye se divide karo.


[!recall]- One-line self-check

Soft labels par ek perfect model ki cross-entropy barabar hoti hai
labels ki Shannon entropy ke, zero nahi.
ka gradient ek sigmoid ke through logit ke w.r.t. hai
.
Nats ko bits mein convert karne ke liye tum
se divide karo.