Concept · Chapter 2: The Math Toolkit
Cross-Entropy Loss
Cross-entropy loss is the negative log of the probability a model assigned to the correct answer — near zero when the model is confidently right, large when it is confidently wrong.
The problem
A classifier or language model outputs a probability distribution; we need a loss that rewards putting high probability on the right answer and is smooth enough to train with gradients.
The solution
Take −log of the probability given to the true class or token, averaged over examples. Minimizing it is the same as maximizing the likelihood of the data.
The consequence
It is the training objective of essentially every classifier and every language model; its average per token, exponentiated, is perplexity.
You should understand first
- Probability and Distributions
- Entropy
- Softmax
- Loss Functions
- Cross-Entropy Loss
Intuition
A language model reads "The cat sat on the" and assigns probabilities to every possible next word. The true next word was "mat". How good was that prediction? Look only at the probability given to "mat": the higher, the better. Cross-entropy turns that probability into a cost with .
Tiny numeric example (natural log)
Confident and right
p(mat) = 0.9 → lossHedging
p(mat) = 0.25 → loss — exactly the loss of a uniform guess over 4 words.Confident and wrong
p(mat) = 0.01 → loss . The log makes near-zero probabilities very expensive.
The equation
Try it
Try it · toy model
Adjust a model's scores for the next word and watch cross-entropy loss and perplexity respond — the exact quantity language models are trained to minimize.
Where it appears in AI
- LLM pretraining is literally "minimize next-token cross-entropy over trillions of tokens".
- Sanity check: a fresh model with vocabulary guesses roughly uniformly, so its initial loss is about — about 10.8 for a 50,000-token vocabulary. If your first loss is far from that, something is wrong.
- Cross-entropy entropy KL divergence, and its exponential is perplexity.
Why should I care?
As a researcher
LLM pretraining loss curves, scaling laws and perplexity comparisons are all cross-entropy; its link to KL divergence also underlies distillation and preference optimization.
As an engineer
It's the number you watch during training. Knowing that ln(vocab size) is the loss of a uniform guess tells you instantly whether a run is learning at all.
Modern systems that depend on it
- LLM pretraining
- Every classifier
- Perplexity
- Scaling laws
- Knowledge distillation
Historical context
Before
Squared error was common for classification in early neural networks; information theory (Shannon, 1948) provided the underlying concepts.
After
Became the standard loss for softmax outputs; next-token cross-entropy at scale is the pretraining objective of modern LLMs.
Used today
Every pretraining step of every LLM minimizes the average cross-entropy of the next token.
What to remember
- Loss = −log p(correct). p = 1 → 0; p → 0 → ∞.
- Confidently wrong is punished hardest.
- General form: H(p, q) = −Σ p log q (target p, model q); with a one-hot target it reduces to −log q(correct).
- Minimizing cross-entropy = maximizing likelihood.
- Uniform guess over V options gives loss ln V — a useful sanity check.
Key papers
A Mathematical Theory of Communication
C. E. Shannon · 1948 · Bell System Technical Journal
Founded information theory: it defined entropy as a measure of uncertainty and showed how much any message can be compressed. Cross-entropy loss and perplexity come straight from here.
How to read it: Don't read it cover to cover. Part I (sections 1–7) contains entropy and the famous 'series of approximations to English' — a 1948 language model.
Watch
Aurélien Géron
A Short Introduction to Entropy, Cross-Entropy and KL-Divergence
Ten minutes that tie entropy, cross-entropy and KL divergence together — the three quantities behind language-model training.