Entropy measures the average surprise (information content) of a distribution:
- 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 measures the average bits needed to encode samples from using a code optimized for :
Since 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 and predicted distribution :
- Binary:
- Multiclass: (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 )
Language-model loss in practice — shift logits/labels by one (predict token from ), 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 — the standard metric for language models
See also: KL Divergence, Loss Functions, Softmax