Information Theory: Entropy and Cross-Entropy
Why is the loss function for classification "cross-entropy"? Where does it come from? Information theory has the answer.
Every classification task uses cross-entropy loss. Why? Because of Shannon’s information theory.
Information = Surprise
Shannon (1948) asked: how do you quantify “information”?
His answer: information equals surprise.
- Telling you “the sun rose this morning” → 0 information (known)
- Telling you “it snowed in Singapore” → high information (surprising)
Mathematically:
- (certain) →
- (coin flip) → bit
- (rare) → bits
Lower probability event = more information bits.
Entropy: Average Information
Entropy is the expected information:
For a fair coin: bit. For a biased coin (99/1): bit (mostly predictable).
Maximum entropy = uniform distribution (most uncertainty).
Cross-Entropy: Distance Between Distributions
Suppose:
- True distribution: (label, one-hot like
[0, 0, 1, 0]) - Predicted distribution: (model output, like
[0.1, 0.2, 0.6, 0.1])
Cross-entropy is:
For our example (true class is index 2):
Lower when matches . Zero when prediction is perfectly confident on the right answer.
Why It’s the Right Loss
Cross-entropy has properties that make it ideal for classification:
1. Penalizes confident wrong answers
If true class is 2 and model predicts [0.99, 0.001, 0.005, 0.005]:
— huge penalty.
If it predicts [0.4, 0.3, 0.2, 0.1] (less confident):
— moderate penalty.
Confidence on wrong answers = big penalty.
2. Differentiable
Smooth, so gradient descent works. Compare to “accuracy” which is non-differentiable.
3. Maximum likelihood estimation
Minimizing cross-entropy ≡ maximizing — it’s the MLE objective.
4. KL divergence
Cross-entropy = entropy of P + KL divergence between P and Q:
Since is constant (true distribution doesn’t change), minimizing = minimizing — making Q close to P.
Binary Cross-Entropy
For binary classification (one output):
In PyTorch:
import torch.nn.functional as F
y = torch.tensor([1.0, 0.0, 1.0]) # true labels
y_hat = torch.tensor([0.9, 0.1, 0.4]) # predictions
loss = F.binary_cross_entropy(y_hat, y)
Categorical Cross-Entropy (Multi-Class)
For K classes:
In PyTorch (note: takes raw logits, applies softmax internally):
import torch.nn.functional as F
logits = torch.tensor([[2.0, 1.0, 0.1]]) # raw model output
labels = torch.tensor([0]) # class index
loss = F.cross_entropy(logits, labels)
Connection to Information Compression
Shannon’s original framing: cross-entropy = average bits needed to encode P using a code optimized for Q.
If you have model Q that thinks “the” is super common but actual distribution P uses “the” less often — your code is inefficient. The “extra bits” you waste = cross-entropy minus true entropy.
This is why LLMs are measured by perplexity = — average “branching factor” the model thinks each token has.
A Concrete Example
Train a sentiment classifier:
# Model output for input "This movie is great"
logits = [3.5, 0.2] # [positive, negative]
probs = softmax(logits) = [0.96, 0.04]
# True label: positive (class 0)
y = [1, 0]
# Cross-entropy
loss = -(1 * log(0.96) + 0 * log(0.04)) = -log(0.96) ≈ 0.04
Low loss — model is confident in the right answer.
If model had said [0.5, 0.5]:
loss = -log(0.5) ≈ 0.69 — much higher.
Backprop pushes the model toward the lower loss.
Practical Tips
1. Don’t combine softmax + cross-entropy yourself
# Bad: numerical instability
probs = softmax(logits)
loss = -y * log(probs)
# Good: PyTorch's cross_entropy handles it
loss = F.cross_entropy(logits, labels)
2. Watch for label smoothing
# Standard: one-hot target [0, 0, 1, 0]
# Smoothed: [0.025, 0.025, 0.925, 0.025] (less confident target)
loss_fn = nn.CrossEntropyLoss(label_smoothing=0.1)
Helps prevent over-confidence (used in image models, modern LLMs).
3. Class imbalance
If 99% of examples are class A:
weights = torch.tensor([0.01, 0.99]) # inverse frequency
loss_fn = nn.CrossEntropyLoss(weight=weights)
Up-weight the rare class so it isn’t ignored.
Information theory + deep learning isn’t an accident.
LLMs explicitly maximize log-likelihood of next tokens. This IS minimizing cross-entropy between true distribution (training data) and predicted (model).
Every training step pushes the model’s “code” closer to the true language distribution. When you see “loss = 2.3” — that’s literally bits-per-token over Shannon’s optimal code.
The deep connection: learning is information compression.
Next recommended: L1-06 Backpropagation or L1-04 Probability.