← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Entropy

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.11 · Entropy & Information Theory

Introduction

A certain event needs no surprise, while a fair coin needs one bit and four equally likely outcomes need two. Entropy turns this uncertainty into a number that also supports classification loss and language-model perplexity.

Learning goal

Calculate entropy for small distributions and connect uncertainty, cross-entropy loss, and perplexity without confusing their meanings.

Before you start

Probabilities, logarithms, weighted sums, and the idea of a class prediction.

Lesson plan

  1. Compare certain and uncertain distributions using exact small probability examples.
  2. Calculate entropy and examine its minimum, maximum, and symmetry properties.
  3. Extend the same calculation to cross-entropy classification loss and language-model perplexity.

Predict the uncertain case. A fair coin gives two equally likely outcomes, so one observation carries 1 bit of uncertainty. A coin that always lands heads has 0 bits: the outcome adds no surprise under that distribution.

Measuring uncertainty

Entropy quantifies the uncertainty or "surprise" in a probability distribution. It's the foundation of cross-entropy loss, the standard loss for classification.

Definition · Shannon entropy

For a discrete probability distribution $p = (p_1, \ldots, p_n)$:

$$H(p) = -\sum_{i=1}^n p_i \log_2 p_i$$

Measured in bits (with $\log_2$) or nats (with $\ln$). Convention: $0 \log 0 = 0$.

Intuition

Entropy is the expected number of bits needed to encode an outcome drawn from $p$. Equivalently, it's the expected "surprise" — where surprise of event $i$ is $-\log_2 p_i$:

$$H(p) = \mathbb{E}_p[-\log_2 p_i] = \sum_i p_i (-\log_2 p_i)$$
Example · Fair vs. biased coin
  • Fair coin: $p = (0.5, 0.5) \implies H = 1$ bit. Maximum uncertainty.
  • Biased coin: $p = (0.9, 0.1) \implies H \approx 0.47$ bits. Less uncertainty.
  • Deterministic: $p = (1, 0) \implies H = 0$ bits. No uncertainty.

Properties of entropy

  • $H(p) \geq 0$, with equality iff $p$ is a point mass (deterministic)
  • $H(p) \leq \log_2 n$, with equality iff $p$ is uniform
  • $H$ is concave: mixing distributions increases entropy

Cross-entropy

When we use a model $q$ to encode data actually drawn from the true distribution $p$, the expected code length is the cross-entropy:

$$H(p, q) = -\sum_i p_i \log q_i$$

By Gibbs' inequality, $H(p, q) \geq H(p)$, with equality iff $q = p$. The excess $H(p,q) - H(p)$ is the KL divergence.

Cross-entropy loss in classification

In classification, the true label is a one-hot vector $y$ (the true distribution $p$), and the model outputs probabilities $\hat{y}$ (the model distribution $q$). The cross-entropy loss is:

$$L = -\sum_i y_i \log \hat{y}_i = -\log \hat{y}_{y^*}$$

where $y^*$ is the true class. Minimising cross-entropy pushes the model to assign high probability to the correct class.

Why not MSE for classification?

Cross-entropy loss has well-behaved gradients: $\frac{\partial L}{\partial z_i} = \hat{y}_i - y_i$ (for softmax output). MSE with sigmoid produces vanishing gradients when predictions are wrong, making learning slow. Cross-entropy avoids this.

Perplexity

In language modelling, perplexity is the exponentiated cross-entropy:

$$\text{PPL} = 2^{H(p, q)} \quad \text{or} \quad e^{H(p, q)}$$

It measures the effective branching factor of the model. Lower perplexity = better model. A perplexity of 10 means the model is as uncertain as choosing uniformly among 10 options.