The Mathematics of Cross Entropy
The Number a Model Is Really Asked For
A classifier does not output an answer, it outputs a distribution, and training scores it on how much of that distribution landed on what actually happened.
Ask what a classifier outputs and the usual answer is a label. That is not what
it outputs. It outputs a number for every label it knows about, and those
numbers are positive and add to one. The label appears only when somebody takes
the largest of them, which is a decision made after the model has finished
speaking and is no part of the model at all.
This distinction is the whole of this lesson, because the loss is defined on
the distribution and not on the label that gets picked out of it.
What the model says, before anything is decided
Consider a model deciding which of three tags belongs on a sentence. Two models
can produce the same decision and be in completely different states:
| step | tag A | tag B | tag C | picked | correct | what happened |
|---|---|---|---|---|---|---|
| 1 | 0.4 | 0.35 | 0.25 | A | A | Right, and barely committed. Shift the input slightly and this becomes B. |
| 2 | 0.92 | 0.05 | 0.03 | A | A | Right, and certain. The same accuracy, a completely different model. |
| 3 | 0.4 | 0.35 | 0.25 | A | B | Wrong, and yet it gave the truth 0.35, which is not far off. |
| 4 | 0.97 | 0.02 | 0.01 | A | B | Wrong, and confidently so. The worst state of the four, and accuracy cannot distinguish it from the third row. |
The fourth column is what an accuracy score keeps. The final column is what
training keeps, and it is a strictly richer signal: it separates the two
correct rows and it separates the two incorrect ones.
The probability of what happened
Given one example whose observed label is , the model assigns some
probability to that label. Call it . The question training asks is not
whether was the largest but simply how large it was.
For a whole dataset, and assuming the examples were drawn independently, the
probability the model assigns to exactly the set of labels that were observed
is the product:
- the parameters, the only thing we are allowed to change
- the input of the ith example
- the label that was actually observed for it
- how many examples there are
This number is called the likelihood, and the name is worth being careful about.
It is a probability of the data, read as a function of the parameters. The
data are fixed, having already happened; the parameters are the variable. So it
is not a probability distribution over parameters and does not integrate to one
over them.
Maximum likelihood is then the entire principle: choose the parameters that
make what happened as probable as the model can make it. Every loss in this
course is a rearrangement of that sentence.
One expression instead of two cases
In the two-class case the model outputs a single number , the probability of
class one, and the probability of class zero is whatever is left. The
probability of the observed label is then a case split: it is when the
label is one and when the label is zero.
Case splits do not differentiate pleasantly, and they do not generalise to
three classes. Writing the label as the number removes it:
- survives only when the observed label is one, because otherwise this term is multiplied by zero
- survives only when the observed label is zero, for the same reason
- the same selection written with exponents instead of multipliers, which is the form that becomes convenient one lesson from now
The two forms are equal for and , which is all that is being
claimed; they disagree for anything in between, and only the exponent form
generalises to the case where the target itself is uncertain.
Why not just optimise accuracy
The obvious objection is that we care about how often the model is right, so we
should maximise that. The reason nobody does is worth seeing precisely rather
than being told it is for technical reasons.
Accuracy counts examples whose largest output is the observed label. Nudge a
parameter by a millionth and no count changes: every example that was on the
right side of the decision is still on it. So the derivative of accuracy with
respect to any parameter is zero, at almost every setting of the parameters,
and gradient descent has nothing to work with. Then at isolated settings a
prediction flips and accuracy jumps, where the derivative does not exist at all.
The probability of the data has neither problem. Every parameter affects every
a little, so there is always a direction that increases it:
Note that this does not mean accuracy is unimportant. It means accuracy is
something to report, and the likelihood is something to optimise. The two
jobs have different requirements, and confusing them is why people are
surprised when the loss improves for an epoch while accuracy sits still.
The chain this leaves behind
The pieces are now in place and nothing has been decided yet. We have a model
that emits a distribution, a number that scores it against what happened, and a
principle that says to make that number large.
A product of numbers each below one is the quantity we are about to
maximise. Two lessons from now it will be a sum, and the reason for that change
is not that sums are easier to type.
What to hold on to
A model's output is a distribution; the label is what somebody does with it
afterwards. The loss is built from the probability the model assigned to what
actually occurred, and the reason for building it that way rather than counting
correct answers is that counting gives no direction to move in. Everything
from here is a consequence of trying to make one product of probabilities
large.
Recap
- A trained model outputs a whole distribution over the possible answers, and the number training cares about is the probability it placed on the answer that was observed.
- The probability of a dataset of independent examples is the product of the per-example probabilities, and choosing parameters to make that product large is the whole of maximum likelihood.
- Accuracy cannot be optimised directly because it does not change at all until a prediction flips, so its gradient is zero almost everywhere; the probability of the data changes with every parameter.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents