Entropy and Cross-Entropy

2 min read

Entropy H(P)H(P) measures the average surprise (information content) of a distribution:

H(P)=xP(x)logP(x)H(P) = -\sum_x P(x) \log P(x)
  • Uniform distribution has maximum entropy (most uncertain)
  • A delta distribution (all mass on one outcome) has zero entropy
  • Units: bits (log base 2) or nats (natural log)

Cross-entropy H(P,Q)H(P, Q) measures the average bits needed to encode samples from PP using a code optimized for QQ:

H(P,Q)=xP(x)logQ(x)=H(P)+DKL(PQ)H(P, Q) = -\sum_x P(x) \log Q(x) = H(P) + D_{\text{KL}}(P \| Q)

Since H(P)H(P) is constant w.r.t. model parameters, minimizing cross-entropy = minimizing KL Divergence from the true distribution.

Why cross-entropy is THE classification loss:

  • For a true label yy and predicted distribution p^\hat{p}:
    • Binary: L=[ylogp^+(1y)log(1p^)]L = -[y\log\hat{p} + (1-y)\log(1-\hat{p})]
    • Multiclass: L=logp^yL = -\log\hat{p}_y (negative log-likelihood of the correct class)
  • This is equivalent to Maximum Likelihood Estimation of the model parameters
  • It heavily penalizes confident wrong predictions (because log(0)-\log(0) \to \infty)

Language-model loss in practice — shift logits/labels by one (predict token t+1t{+}1 from t\leq t), take log-softmax, gather the true-token log-prob, and mask out padding:

shift_logits = logits[:, :-1, :]
shift_labels = input_ids[:, 1:]
logprobs = F.log_softmax(shift_logits, dim=-1)
token_logprobs = logprobs.gather(-1, shift_labels.unsqueeze(-1)).squeeze(-1)
loss = -(token_logprobs * mask.float()).sum() / mask.sum()
# equivalently: F.cross_entropy(logits.view(-1, V), targets.view(-1), ignore_index=pad)

Connections:

  • Cross-entropy loss + Softmax = the standard classification pipeline
  • Language model training objective is cross-entropy over the vocabulary at each token position
  • Perplexity =2H(P,Q)= 2^{H(P,Q)} — the standard metric for language models

See also: KL Divergence, Loss Functions, Softmax

Linked from