Maximum Likelihood and Cross-Entropy Loss
Why mean squared error for regression and cross-entropy for classification? Because both are the same principle, maximum likelihood, applied to different output distributions. Seeing a loss as a negative log-likelihood tells you which loss to use for a new kind of output and what the network's outputs mean. The general theory is in Maximum Likelihood Estimation.
The principle
A network with parameters defines a conditional distribution . Maximum likelihood picks the parameters that make the observed labels most probable, which, after taking logs and averaging, means minimizing the negative log-likelihood:
This is the cross-entropy between the empirical data distribution and the model (Entropy and KL).
Three outputs, three losses
Gaussian output, MSE. If with fixed , then : minimizing the NLL is minimizing the squared error.
Bernoulli output, binary cross-entropy. With , .
Categorical output, cross-entropy. With , .
import numpy as np
y, f, sigma = 2.5, 2.0, 1.0
nll = 0.5 * np.log(2 * np.pi * sigma**2) + (y - f)**2 / (2 * sigma**2)
print(f"Gaussian NLL {nll:.4f} = squared error/2 {(y - f)**2 / 2:.4f} + const {0.5 * np.log(2 * np.pi):.4f}")
z, label = 1.2, 1
mu = 1 / (1 + np.exp(-z))
print(f"binary CE: {-(label * np.log(mu) + (1 - label) * np.log(1 - mu)):.4f}")
logits, k = np.array([2.0, 0.5, -1.0]), 0
lse = logits.max() + np.log(np.sum(np.exp(logits - logits.max())))
print(f"categorical CE: {lse - logits[k]:.4f}")Gaussian NLL 1.0439 = squared error/2 0.1250 + const 0.9189
binary CE: 0.2633
categorical CE: 0.2413
Why not MSE for classification?
With a sigmoid output and MSE loss, the gradient with respect to the logit is . When the network is confidently wrong (, ), the factor is nearly 0 and learning stalls. The cross-entropy gradient is simply : the log cancels the saturation of the sigmoid exactly.
import numpy as np
y = 1.0
for z in (-8.0, -2.0, 0.0, 2.0):
mu = 1 / (1 + np.exp(-z))
g_mse = (mu - y) * mu * (1 - mu)
g_ce = mu - y
print(f"logit {z:+.0f}: mu {mu:.4f} dMSE/dz {g_mse:+.5f} dCE/dz {g_ce:+.4f}")logit -8: mu 0.0003 dMSE/dz -0.00034 dCE/dz -0.9997
logit -2: mu 0.1192 dMSE/dz -0.09248 dCE/dz -0.8808
logit +0: mu 0.5000 dMSE/dz -0.12500 dCE/dz -0.5000
logit +2: mu 0.8808 dMSE/dz -0.01252 dCE/dz -0.1192
At logit the model is badly wrong, yet the MSE gradient is about ; the cross-entropy gradient is about .
Learning the variance too
A regression network can output both and and minimize the full Gaussian NLL . It then learns where its predictions are uncertain (heteroscedastic regression). Predicting the log-variance keeps automatically.
In software
nn.MSELoss,nn.BCEWithLogitsLoss,nn.CrossEntropyLossandnn.GaussianNLLLossare the four negative log-likelihoods above.- Label smoothing replaces the one-hot target by on the true class and elsewhere, discouraging overconfident logits.
- LLM pre-training minimizes exactly the categorical NLL of the next token (Language Modeling).
Exercises
Exercise 1: Laplace noise
If is a Laplace distribution , which loss does maximum likelihood give?
Answer
The mean absolute error .
Solution
; with fixed, minimizing the NLL minimizes the absolute error.
Exercise 2: Cross-entropy gradient
Show that .
Answer
Use .
Solution
Exercise 3: Uniform guessing (code)
What is the cross-entropy of a classifier that outputs the uniform distribution over 10 classes? Check with code.
Answer
.
Solution
Every class gets , so the loss is for every example. An untrained network should start near this value; a much larger initial loss signals badly scaled logits.
import numpy as np
logits = np.zeros(10)
print(round(np.log(np.sum(np.exp(logits))) - logits[3], 4))2.3026
Test yourself
Minimizing mean squared error is maximum likelihood under which noise model?
- Laplace
- Gaussian with fixed variance
- Bernoulli
- Uniform
For a sigmoid output, why is cross-entropy preferred over MSE?
- It is cheaper
- Its gradient does not vanish when the model is confidently wrong
- MSE cannot be differentiated
- Cross-entropy is always smaller
The expected initial loss of a well-initialized 1000-class classifier is about…
- 0
- 1
- 1000