2.6.11 · HinglishModel Evaluation & Selection

Log-loss and calibration

2,753 words13 min readRead in English

2.6.11 · AI-ML › Model Evaluation & Selection

Overview

Log-loss (logarithmic loss, cross-entropy loss) measure karta hai ki predicted probabilities true outcomes se kitni acchi tarah match karti hain. Calibration measure karta hai ki predicted probabilities true frequencies ko reflect karti hain ya nahi: agar aap 100 events ke liye "70% confident" kehte hain, toh roughly 70 hone chahiye.

Yeh kyun important hai: Ek model ki accuracy high ho sakti hai lekin probabilities bilkul bekar ho sakti hain. Log-loss + calibration batate hain ki aapki uncertainties trustworthy hain ya nahi—yeh medical diagnosis, finance, aur uncertainty ke saath kisi bhi decision ke liye critical hai.


Core Intuition


Log-loss: Math Scratch Se

Binary Classification

Aapke paas true label hai aur class 1 ke liye predicted probability hai.

Step 1: Likelihood define karo. Agar hai, toh aapne jo probability assign ki woh thi. Agar hai, toh aapne jo probability assign ki woh thi. Combined:

Yeh step kyun? Yeh Bernoulli likelihood hai—aapki prediction given outcome observe karne ki basic probability.

Step 2: Negative log lo. Hum likelihood maximize karna chahte hain = negative log-likelihood minimize karna chahte hain:

Negative log kyun?

  1. Numerical stability: 0 ke paas probabilities underflow karti hain; logs numbers ko manageable rakhte hain.
  2. Additive: logs multiple samples par products ko sums mein convert kar dete hain.
  3. Convex: easier optimization (gradient descent cleanly kaam karta hai).

Step 3: Dataset par average karo. samples ke liye:

Multi-class Classification

True label: one-hot vector jahan exactly ek hai. Predicted probabilities: with .

Derivation: True class observe karne ki likelihood hai (sirf true class contribute karta hai kyunki baaki ke liye hai). Negative log-likelihood:

Yeh step kyun? par sum ek single term (true class) par collapse ho jaata hai kyunki saare incorrect classes ke liye hota hai.


Calibration: The Math

Definition

Calibration Measure Karna: Expected Calibration Error (ECE)

Step 1: Predictions bin karo. ko bins mein divide karo (typically 10-20): .

Step 2: Har bin ke liye accuracy aur confidence compute karo. Bin ke liye:

  • Average predicted probability:
  • Actual accuracy:

Step 3: Bin size se weight karo aur average karo.

Yeh step kyun? Hum bin size se weight karte hain kyunki 2 samples waale bin ko 200 samples waale bin par dominate nahi karna chahiye. Absolute difference calibration gap hai.

Reliability Diagrams

Predicted confidence (x-axis) vs. observed accuracy (y-axis) plot karo. Perfect calibration = diagonal line .

Common patterns:

  • Overconfident: curve diagonal se neeche (aap 80% kehte hain, reality 60% hai)
  • Underconfident: curve diagonal se upar (aap 60% kehte hain, reality 80% hai)

Worked Examples


Common Mistakes


Calibration Fixes

Temperature Scaling

Problem: Neural net outputs (logits). Softmax overconfident hai.

Solution: Temperature add karo:

kaise find karein: Model weights freeze karo. NLL ya ECE minimize karne ke liye validation set par tune karo. Typically .

Yeh kyun kaam karta hai: Logits ko se divide karna distribution ko "soften" karta hai—probabilities 0 aur 1 se door uniform ki taraf move karti hain. Yeh retraining ke bina overconfidence correct karta hai.

Platt Scaling (Binary)

Model scores ke upar ek logistic regression fit karo: jahan model ka output hai aur validation data par seekhe jaate hain.

Yeh kyun kaam karta hai: Arbitrary scores ko calibrated probabilities mein map karne ke liye ek affine transformation seekhta hai.

Isotonic Regression (Non-parametric)

Scores se probabilities tak ek piecewise-constant, monotonically increasing function fit karo. Flexible hai lekin chhote validation sets par overfit ho sakta hai.


Connections to Other Concepts

  • Cross-Entropy Loss: Log-loss true distribution (one-hot) aur predicted distribution ke beech cross-entropy hai.
  • Information Theory: Log-loss "surprise" measure karta hai—predictions given outcomes encode karne ke liye average bits.
  • Brier Score: Log-loss ka alternative: . Confident errors ke liye kam sensitive, lekin nicely decompose nahi karta.
  • ROC-AUC: Ranking quality (discrimination) measure karta hai. Ek model ki perfect AUC ho sakti hai lekin terrible calibration.
  • Focal Loss: Log-loss ko hard examples par focus karne ke liye modify karta hai: . Imbalanced settings mein use hota hai.
  • Bayesian Neural Networks: Uncertainty estimates produce karte hain. Calibration check karta hai ki woh uncertainties honest hain ya nahi.
  • Proper Scoring Rules: Log-loss "proper" hai—maximize hota hai jab predictions = true probabilities. Honest forecasting encourage karta hai.
  • Temperature Scaling: Post-hoc calibration method. Logits ko scale karta hai overconfidence fix karne ke liye.
  • Reliability Diagrams: Calibration ke liye visual tool. Predicted vs. observed frequencies plot karo.

Feynman Technique

Recall Ek 12-saal ke bacche ko samjhao

Socho tum ek weather forecaster ho. Tum kehte ho "70% chance of rain." Agar tum yeh 100 baar kehte ho, toh roughly 70 baar baarish honi chahiye. Agar sirf 50 baar baarish hoti hai, toh tumhari probabilities kharaab hain—tum overconfident ho. Calibration check karta hai ki tumhare percentages honest hain ya nahi.

Ab socho, jab bhi tum galat hote ho, tumhare points kate jaate hain. Lekin sabhi mistakes equal nahi hain. Agar tum "99% no rain" kehte ho aur baarish ho jaati hai, toh tum BAHUT saare points khote ho—tum super confident the aur super galat the. Agar tumne "60% no rain" kaha hota aur baarish ho jaati, toh kam points kate kyunki tum sure nahi the. Yahi log-loss hai: yeh cocky mistakes ko humble ones se bahut zyada punish karta hai.

Kyun? Kyunki real life mein (medicine, driving, investing), galat hone par overconfident hona dangerous hai. Log-loss tumhare AI ko yeh admit karana sakta hai jab woh unsure ho, aur calibration sure karti hai ki uska "main 80% sure hoon" actually kuch maayane rakhta hai.


Mnemonic


Active Recall Flashcards

#flashcards/ai-ml

Log-loss kya hai aur hum logarithms kyun use karte hain?
Log-loss (cross-entropy) hai . Hum logarithms isliye use karte hain kyunki: (1) probabilities multiply hoti hain, logs unhe add kara dete hain; (2) 0 ke paas numerical stability; (3) information-theoretic "surprise" measure karta hai; (4) optimization ke liye convex hai.
Binary log-loss formula
jahan , . Range: .
Multi-class log-loss formula
jahan one-hot hai, sum hokar 1 deta hai. Equivalent hai (true class).
Ek model ke calibrated hone ka matlab kya hai?
Ek model calibrated hota hai agar, un sabhi predictions mein jinki confidence hai, true positive rate ho. Jaise, un sabhi "70% confident" predictions mein se, 70% sahi hone chahiye. Perfect calibration: observed frequency = predicted probability.
Expected Calibration Error (ECE) formula
jahan predictions ke bins hain, bin mein actual accuracy hai, average predicted probability hai. Range: .
Ek model ki low log-loss lekin poor calibration kyun ho sakti hai?
Log-loss training distribution ke saath fit measure karta hai. Ek model training data par log-loss minimize kar sakta hai lekin test/OOD data par overconfident ho sakta hai, ya poorly extrapolate kar sakta hai. Hamesha calibration alag se ECE ya reliability diagrams se measure karo.
Softmax outputs automatically calibrated probabilities kyun nahi hote?
Softmax mein values produce karta hai jo 1 sum hoti hain, lekin logits ka scale arbitrary hai. Neural nets classify karne ke liye train hote hain (loss minimize karna), calibrated hone ke liye nahi. Woh often overconfident scores output karte hain. Fix: temperature scaling.
Temperature scaling kya hai aur yeh calibration kaise fix karta hai?
Temperature scaling ko se replace karta hai jahan ko validation data par tune kiya jaata hai. Logits ko se divide karna distribution ko "soften" karta hai, probabilities ko extremes (0 ya 1) se door le jaata hai, retraining ke bina overconfidence correct karta hai.
Log-loss vs. accuracy: kaun zyada informative hai?
Log-loss zyada informative hai kyunki yeh probability quality evaluate karta hai, sirf binary decisions nahi. Ek model jo sabhi positives ke liye 0.51 predict karta hai uski accuracy 100% hai lekin probabilities bekar hain. Log-loss high hoga, problem reveal karta hua.
Log-loss confident wrong predictions ko kaise penalize karta hai?
Log-loss hai jab sahi, hai jab galat. Jaise ek true positive ke liye, . Ek confident wrong prediction (jaise true class ke liye 0.01) massive loss (~4.6) deta hai, jabki ek timid wrong prediction (0.4) chhota loss (~0.92) deta hai. Exponential penalty.
Reliability diagram kya hota hai?
Predicted confidence (x-axis) vs. observed accuracy (y-axis) ka bins mein plot. Perfect calibration = diagonal line . Diagonal se neeche curve = overconfident. Upar = underconfident.
Log-loss ko "cross-entropy" kyun kehte hain?
Cross-entropy woh average bits measure karta hai jo distribution ke data ko ke liye optimized code use karke encode karne mein lagte hain. Classification ke liye, one-hot true label hai, predicted distribution hai. Binary log-loss exactly hai Bernoulli distributions ke liye.

Concept Map

derived from

take neg log

averaged over N

generalizes to

log turns products to sums

behavior

checks

together with

enables

enables

Log-loss cross-entropy

Calibration

Bernoulli likelihood

Negative log-likelihood

Binary log-loss

Multi-class log-loss

Information theory bits of surprise

Trustworthy uncertainties

Penalizes confident wrong preds

Predicted probs match true frequencies