AI / ML Theory
Chapter 0 · 18 min

0.7 — Cross-entropy as a loss, and why minimising it is MLE

Goal: Show that −Σᵢ log p(yᵢ|xᵢ; θ) is the cross-entropy between the empirical distribution and the model, so MLE and cross-entropy minimisation are the same objective.

1. The starting point: i.i.d. data and a parametric model

We have a training set D={(xi,yi)}i=1m\mathcal{D} = \{(x_i, y_i)\}_{i=1}^{m} assumed to be i.i.d. draws from some unknown joint pdata(x,y)p_{\text{data}}(x, y). We specify a parametric conditional model qθ(y∣x)q_\theta(y \mid x) and want to fit θ\theta to the data.

(I write qq instead of pp for the model to keep the truth/model distinction explicit — same convention as §0.6.)[1]

2. The likelihood

Under the i.i.d. assumption, the joint probability the model assigns to the labels given the inputs factorises:

qθ(y1,…,ym∣x1,…,xm)  =  ∏i=1mqθ(yi∣xi).q_\theta(y_1, \dots, y_m \mid x_1, \dots, x_m) \;=\; \prod_{i=1}^{m} q_\theta(y_i \mid x_i).

This is the conditional likelihood of the labels under the model. As a function of θ\theta with the data fixed, it is denoted L(θ)\mathcal{L}(\theta).

3. Maximum likelihood estimation (MLE)

Maximum likelihood picks θ\theta that maximises L(θ)\mathcal{L}(\theta). Products are awful to differentiate, so take the log (which is monotone and so doesn't change the argmax):

θ^MLE  =  arg⁡max⁡θ  log⁡∏i=1mqθ(yi∣xi)  =  arg⁡max⁡θ  ∑i=1mlog⁡qθ(yi∣xi).\hat\theta_{\text{MLE}} \;=\; \arg\max_\theta\; \log\prod_{i=1}^{m} q_\theta(y_i \mid x_i) \;=\; \arg\max_\theta\; \sum_{i=1}^{m} \log q_\theta(y_i \mid x_i).

Flip the sign to turn maximisation into minimisation (the convention in optimisation):

  θ^MLE=arg⁡min⁡θ  −∑i=1mlog⁡qθ(yi∣xi).  \boxed{\;\hat\theta_{\text{MLE}} = \arg\min_\theta\; -\sum_{i=1}^{m} \log q_\theta(y_i \mid x_i).\;}

This is the negative log-likelihood (NLL). Divide by mm to get the per-sample version (gradients are then unitless in batch size):

θ^MLE=arg⁡min⁡θ  −1m∑i=1mlog⁡qθ(yi∣xi).\hat\theta_{\text{MLE}} = \arg\min_\theta\; -\frac{1}{m}\sum_{i=1}^{m} \log q_\theta(y_i \mid x_i).

4. The empirical distribution

Define the empirical distribution as the discrete measure that puts mass 1/m1/m on each training point:

p^(x,y)  =  1m∑i=1m1{x=xi, y=yi}.\hat p(x, y) \;=\; \frac{1}{m}\sum_{i=1}^{m} \mathbb{1}\{x = x_i,\, y = y_i\}.

Expectation under p^\hat p is "average over the training set", which we already noted in §0.4:

E(X,Y)∼p^[f(X,Y)]  =  1m∑i=1mf(xi,yi).\mathbb{E}_{(X, Y)\sim \hat p}[f(X, Y)] \;=\; \frac{1}{m}\sum_{i=1}^{m} f(x_i, y_i).

5. NLL is cross-entropy

Apply this to f(x,y)=−log⁡qθ(y∣x)f(x, y) = -\log q_\theta(y \mid x):

−1m∑i=1mlog⁡qθ(yi∣xi)=E(X,Y)∼p^ ⁣[−log⁡qθ(Y∣X)]=−∑(x,y)p^(x,y) log⁡qθ(y∣x).\begin{aligned} -\frac{1}{m}\sum_{i=1}^{m} \log q_\theta(y_i \mid x_i) &= \mathbb{E}_{(X, Y)\sim \hat p}\!\left[-\log q_\theta(Y \mid X)\right] \\ &= -\sum_{(x, y)} \hat p(x, y)\, \log q_\theta(y \mid x). \end{aligned}

That last line is the cross-entropy between p^\hat p and the conditional model qθq_\theta, written with the joint factored as p^(x,y)=p^(x) p^(y∣x)\hat p(x, y) = \hat p(x)\,\hat p(y \mid x):

−∑x,yp^(x,y)log⁡qθ(y∣x)=−∑xp^(x)∑yp^(y∣x)log⁡qθ(y∣x)=EX∼p^ ⁣[H[p^(⋅∣X), qθ(⋅∣X)]],\begin{aligned} -\sum_{x, y} \hat p(x, y) \log q_\theta(y \mid x) &= -\sum_x \hat p(x) \sum_y \hat p(y \mid x) \log q_\theta(y \mid x) \\ &= \mathbb{E}_{X \sim \hat p}\!\left[ H[\hat p(\cdot \mid X),\, q_\theta(\cdot \mid X)] \right], \end{aligned}

i.e. the input-averaged cross-entropy between the empirical conditional p^(y∣x)\hat p(y \mid x) and the model conditional qθ(y∣x)q_\theta(y \mid x).

Putting it together:

  θ^MLE  =  arg⁡min⁡θ  EX∼p^ ⁣[H[p^(⋅∣X), qθ(⋅∣X)]].  \boxed{\;\hat\theta_{\text{MLE}} \;=\; \arg\min_\theta\; \mathbb{E}_{X \sim \hat p}\!\left[ H[\hat p(\cdot \mid X),\, q_\theta(\cdot \mid X)] \right].\;}

MLE on a discriminative model is exactly cross-entropy minimisation on the empirical distribution. The two names refer to the same gradient.

6. Why the model's "missing" entropy term doesn't matter for gradients

Recall from §0.6 that H[p^,qθ]=H[p^]+KL(p^ ∥ qθ)H[\hat p, q_\theta] = H[\hat p] + \mathrm{KL}(\hat p\,\|\,q_\theta). So minimising cross-entropy over θ\theta is identical to minimising KL(p^ ∥ qθ)\mathrm{KL}(\hat p\,\|\,q_\theta), since H[p^]H[\hat p] doesn't depend on θ\theta and drops out of the gradient. Three names for the same optimisation:

  • MLE (statistics).
  • Cross-entropy minimisation (information theory).
  • Forward KL minimisation against the empirical distribution (Bayesian ML).

7. The two cases you'll see daily

Binary classification with y∈{0,1}y \in \{0, 1\} and qθ(y=1∣x)=y^q_\theta(y = 1 \mid x) = \hat y for some predicted probability:

−log⁡qθ(y∣x)={−log⁡y^if y=1,−log⁡(1−y^)if y=0.-\log q_\theta(y \mid x) = \begin{cases} -\log \hat y & \text{if } y = 1, \\ -\log (1 - \hat y) & \text{if } y = 0. \end{cases}

These two cases unify into one expression — the binary cross-entropy loss:

  ℓ(y^,y)=− ylog⁡y^  −  (1−y)log⁡(1−y^).  \boxed{\;\ell(\hat y, y) = -\,y \log \hat y \;-\; (1 - y)\log(1 - \hat y).\;}

Multi-class classification with y∈{1,…,K}y \in \{1, \dots, K\} and qθ(y=k∣x)=y^kq_\theta(y = k \mid x) = \hat y_k for predicted class probabilities summing to 1 (e.g. coming from a softmax — see Ch. 1):

  ℓ(y^,y)=−∑k=1K1{y=k} log⁡y^k  =  −log⁡y^y.  \boxed{\;\ell(\hat y, y) = -\sum_{k=1}^{K} \mathbb{1}\{y = k\}\, \log \hat y_k \;=\; -\log \hat y_y.\;}

The last form (−log⁡y^y-\log \hat y_y) is what you read in code: take the predicted probability of the true class, take its log, negate. That's PyTorch's F.cross_entropy (modulo the fact that PyTorch's cross_entropy expects pre-softmax logits and applies the softmax internally for numerical stability — more on that in §0.10).

8. What we've earned

The same loss object reads three ways depending on the lens. Picking the right lens unstrucks problems:

  • Statistical lens (MLE): what data-generating assumption does my loss implicitly make? E.g. mean-squared error ↔ Gaussian noise (Ch. 1).
  • Information-theoretic lens (cross-entropy / KL): how far is my model from the empirical distribution? Variational and distillation methods sit here.
  • Operational lens (NLL): what does the gradient look like and how do I implement it? See §0.10 for the stability tricks that make this practical.

For every loss in the rest of the course, ask which of the three views makes the calculation easy. Use that one.

Self-test

A neural network outputs a single logit zz (a real number, no activation). The label is binary, y∈{0,1}y \in \{0, 1\}.

  1. Apply the sigmoid: y^=σ(z)=1/(1+e−z)\hat y = \sigma(z) = 1/(1 + e^{-z}). Write the binary cross-entropy ℓ(z,y)\ell(z, y) purely in terms of zz and yy, eliminating y^\hat y.
  2. Compute ∂ℓ/∂z\partial \ell / \partial z. You should get a famously clean expression involving σ(z)−y\sigma(z) - y. This is the identity that makes logistic regression numerically and didactically pleasant.
  3. Sanity check: at y=1y = 1, the gradient is −(1−σ(z))-(1 - \sigma(z)); at y=0y = 0, it is σ(z)\sigma(z). Why does this match the intuition that "the loss pushes the logit up when y=1y = 1 and down when y=0y = 0, proportionally to how wrong we are"?

References

  1. [1]

    Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory (2nd ed.). Wiley. — Used in 0.6 (entropy, KL divergence) and 0.7 (cross-entropy as MLE).

    ↩