Cross-entropy concept
Overview
Cross-entropy measures the difference between two probability distributions by quantifying how many bits (on average) we need to encode samples from one distribution if we wrongly assume another distribution. It's the workhorse loss function for classification in machine learning because it directly penalizes confident wrong predictions.
Why it matters: Cross-entropy bridges information theory and machine learning. It tells us "how surprised" we are when reality doesn't match our model's beliefs.
[!intuition] The Core Idea
Imagine you're a weather forecaster. Every day you predict rain probability, and nature reveals the truth.
- If you say 90% rain and it rains → small surprise (you were confident and correct)
- If you say 10% rain and it rains → huge surprise (you were confident and wrong)
Cross-entropy is the average surprise your predictions cause, weighted by what actually happens. The better your predictions match reality, the lower the cross-entropy.
Key insight: Cross-entropy = "How many extra bits am I wasting by using the wrong probability distribution?"
[!definition] Mathematical Definition
Given:
- = true probability distribution (reality, ground truth)
- = predicted probability distribution (model's belief)
The cross-entropy from to is:
For continuous distributions:
Units: bits (if ) or nats (if ). In ML we use natural log.
[!formula] Derivation from First Principles
Step 1: Shannon Entropy (Self-Information)
Question: How surprised am I when event with probability occurs?
Answer: The surprise (self-information) is:
Why this formula?
- Rare events ( small) → large surprise
- Certain events () → zero surprise
- Independent events should have additive surprise: , which means we need
Expected surprise when sampling from :
This is the Shannon entropy of — the minimum average bits needed to encode samples from if we know perfectly.
Step 2: What If We Use the Wrong Code?
Suppose reality follows but we design our encoding assuming distribution .
- We assign code length to event (optimal for )
- But events occur with frequency
- Average code length =
This is cross-entropy — the average bits we actually use with the wrong distribution.
Why this step? Cross-entropy tells us the cost of being wrong. If , we use the minimum possible bits. Any deviation from wastes bits.
Step 3: Connection to KL Divergence
The extra bits wasted by using instead of :
This is the Kullback-Leibler divergence.
Key relationship:
- = irreducible uncertainty (entropy of truth)
- = extra cost from model mismatch
Why minimize cross-entropy in ML? Since is fixed (we can't change reality), minimizing is equivalent to minimizing — making our model match reality.
[!example] Example 1: Binary Classification (Coin Flip)
Setup: True distribution: coin lands heads with . Model predicts .
Truth: ,
Model: ,
Cross-entropy:
Why this step? We weight each outcome by its true probability , not the model's belief . Reality determines how often each surprise occurs.
Compare to entropy of truth:
Wasted bits: nats.
Interpretation: Our model is slightly overconfident in heads, causing small information loss.
[!example] Example 2: Multiclass Classification (3 classes)
Setup: Image classification — cat, dog, bird.
Truth (one-hot): (it's definitely a cat)
Model predicts:
Cross-entropy:
Why zero out the other terms? Because — these classes don't occur in this sample. We only care about the probability assigned to what actually happened.
What if model predicted (wrong class)?
Why the huge jump? Cross-entropy heavily penalizes confident wrong predictions. Model was 80% sure it was a dog, but it was a cat — massive surprise.

[!example] Example 3: Why Not Mean Squared Error?
Question: Why use cross-entropy for classification instead of MSE?
Setup: Binary classification, true label (positive class).
Model outputs: (predicts negative).
MSE:
Cross-entropy:
Why this difference matters:
If model improves to :
- MSE: → decrease of
- Cross-entropy: → decrease of
Why cross-entropy is better: It gives stronger gradient signal when the model is confidently wrong. MSE gradient is linear in error; cross-entropy gradient is inversely proportional to the predicted probability, so wrong predictions get corrected faster.
[!mistake] Common Pitfalls
Mistake 1: "Cross-entropy is just negative log probability"
Why it feels right: In practice, we write loss = -log(p_correct_class), which looks like we're ignoring the sum.
Why it's incomplete: That's only true when the true distribution is one-hot (hard labels). The full definition sums over all outcomes weighted by .
The fix:
- One-hot → (all other terms vanish)
- Soft labels → (all terms matter)
When it matters: Label smoothing, knowledge distillation, multi-label classification.
Mistake 2: "Minimizing cross-entropy = minimizing entropy"
Why it feels right: Both have "entropy" in the name and use .
Why it's wrong:
- = entropy of true distribution (fixed, doesn't depend on model)
- = cross-entropy (depends on model's )
The fix: You minimize by adjusting to match . Since and is constant, you're really minimizing .
Mistake 3: "Cross-entropy is symmetric"
Why it feels right: Both and appear in the formula.
Why it's wrong: in general.
Proof:
Different weights, different sums.
The fix: Cross-entropy measures "surprise when reality is but we assume ". Swapping them asks a different question. Always keep as ground truth, as model.
[!recall]- Feynman Explanation (ELI12)
Imagine you're playing a guessing game. Every round, I secretly pick a colored ball from a bag (maybe 70% red, 30% blue). You have to guess the color before I show you.
Now, you get points based on how confident you were:
- If you say "90% sure it's red" and it IS red → you get lots of points (you were right and confident!)
- If you say "90% sure it's red" and it's BLUE → you lose TONS of points (you were wrong and super confident)
Cross-entropy is like keeping track of your average point loss over many rounds. If your guesses match the true bag contents (70% red), you lose very few points. If you always guess 50-50 when the bag is actually 70-30, you lose more points because you're surprised more often.
In machine learning, the model is you guessing, and reality is the bag. We train the model to minimize cross-entropy = minimize average surprise = make better guesses that match reality!
[!mnemonic] Memory Aid
CROSS = Compare Reality's Occurrence to System's Speculation
- Compare: Two distributions
- Reality: True (ground truth)
- Occurrence: Weighted by true probabilities
- System: Model's predicted
- Surprise: (information content)
Formula mnemonic: "People Love Quality" → summed with a negative sign.
Connections
- Shannon Entropy — cross-entropy generalizes self-entropy when
- KL Divergence —
- Softmax Function — converts logits to probabilities for cross-entropy loss
- Logistic Regression — binary cross-entropy is its loss function
- Categorical Cross-Entropy — multiclass extension used in neural networks
- Label Smoothing — regularization technique that modifies to prevent overconfidence
- Maximum Likelihood Estimation — minimizing cross-entropy = maximizing likelihood
- Mutual Information — related measure of shared information between variables
Flashcards
What does cross-entropy measure?
Formula for cross-entropy ?
Why use in cross-entropy?
Relationship between cross-entropy and KL divergence?
Why minimize cross-entropy in classification?
Cross-entropy for one-hot true label and predicted probabilities ?
Why is cross-entropy better than MSE for classification?
Is cross-entropy symmetric?
What happens to cross-entropy as approaches ?
Why does cross-entropy explode when for where ?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Cross-entropy samajhne ke liye ek simple example hai. Maan lo aap ek weather forecaster ho. Har din aap predict karte ho ki baarish hogi ya nahi, probability ke sath. Agar aap kehte ho "90% chance hai baarish ka" aur sach mein baarish hoti hai, toh aapka prediction acha tha - kam surprise hua. Lekin agar aap kehte ho "10% chance hai" aur baarish ho jati hai, toh bahut bada surprise hua - aap confident the galat direction mein!
Cross-entropy basically yahi measure karta hai: average surprise kitna hai jab reality (true distribution p) aapke belief (predicted distribution q) se match nahi karta. Formula hai H(p,q) = -Σ p(x) log(q(x)). Matlab true probability p(x) se weight karke, har outcome ka predicted probability q(x) ka log lete hain. Jitna zyada match hoga p aur q mein, utna kam cross-entropy.
Machine learning mein, classification tasks ke liye cross-entropy sabse popular loss function hai. Kyon? Kyunki yeh confident galat predictions ko bahut heavily penalize karta hai. Agar aapka neural network 95% confident hai ki image mein dog hai, lekin actually cat hai, toh cross-entropy loss bahut high hoga - model ko strong sign