What are entropy and cross-entropy?
Entropy is the average amount of surprise in a random outcome, measured in bits. It comes from information theory, the mathematics of measuring and transmitting information that Claude Shannon founded in 1948. Cross-entropy measures how many bits you need, on average, when data comes from one distribution but you encode it with a code built for another.
In machine learning, cross-entropy is the standard loss for classification, from logistic regression to the next-token loss of large language models. Related quantities appear across the field: KL divergence in VAEs and knowledge distillation, information gain in decision trees, perplexity for evaluating language models, and mutual information for feature selection.
Key ideas
- An event with probability p carries −log₂ p bits of surprise.
- Entropy is the expected surprise of a distribution.
- Entropy is highest for a uniform distribution, at log₂ n bits for n outcomes.
- Cross-entropy H(p, q) equals H(p) + KL(p‖q), so it is never below H(p).
- Minimizing cross-entropy on labeled data is the same as maximizing the log-likelihood of the labels.
- Bits use log base 2, and nats use the natural log.
Common questions
What is the difference between binary and categorical cross-entropy?
Binary cross-entropy is for two classes or for independent yes/no labels: L = −[y log ŷ + (1 − y) log(1 − ŷ)], with ŷ from a sigmoid. Categorical cross-entropy is for one label out of K classes: L = −log q(correct class), with q from a softmax. Binary cross-entropy is the K = 2 case of the categorical form.
Why use cross-entropy instead of mean squared error for classification?
With a sigmoid output, the MSE gradient includes the sigmoid's slope, which is close to zero when the output saturates. A confidently wrong prediction then learns very slowly. Cross-entropy cancels that factor, so the gradient stays large exactly when the model is badly wrong. It is also the negative log-likelihood of a Bernoulli or categorical model, which gives it a probabilistic meaning.
What is information gain in a decision tree?
Information gain is the drop in label entropy after a split: the entropy of the parent node minus the weighted average entropy of the child nodes. ID3 picks the split with the largest gain, and C4.5 uses a normalized version called the gain ratio. A split that sends every class to its own pure child removes all the entropy.
What is mutual information?
Mutual information I(X; Y) = H(X) − H(X|Y) is the reduction in uncertainty about X after you learn Y. It is zero exactly when X and Y are independent, and unlike correlation it also detects nonlinear dependence. It is used for feature selection and in self-supervised learning objectives.
Related explainers
- KL Divergence — Measuring how different two distributions are
- Probability & Distributions — Spam, densities, and the math of uncertainty
- Loss Functions — How networks measure their mistakes
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Decision Trees & Splitting — Recursive partitioning, Gini impurity, and pruning
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
References
- Shannon (1948), "A Mathematical Theory of Communication", Bell System Technical Journal
- MacKay (2003), "Information Theory, Inference, and Learning Algorithms" (free online edition)
- Goodfellow, Bengio & Courville (2016), "Deep Learning", chapter 3: Probability and Information Theory
Last updated