ContentsThe library

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:

FIG 1Two models, one decision
steptag Atag Btag Cpickedcorrectwhat happened
10.40.350.25AARight, and barely committed. Shift the input slightly and this becomes B.
20.920.050.03AARight, and certain. The same accuracy, a completely different model.
30.40.350.25ABWrong, and yet it gave the truth 0.35, which is not far off.
40.970.020.01ABWrong, and confidently so. The worst state of the four, and accuracy cannot distinguish it from the third row.
4 steps
Accuracy sees two rights and two wrongs. The probability placed on the true tag sees 0.40, 0.92, 0.35 and 0.02, which is the ordering anybody would actually want.

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 tt, the model assigns some

probability to that label. Call it ptp_t. The question training asks is not

whether ptp_t 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:

FIG 2The likelihood of the data
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
One factor per example, each one the probability the model gave to the label that turned out to be there. Nothing in this expression mentions which label was largest.

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 pp, 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 pp when the

label is one and 1−p1 - p 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 t∈{0,1}t \in \{0, 1\} removes it:

FIG 3The label as a selector
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
Both forms do the same job: the label is a switch that turns one term off entirely. The exponent form looks stranger and is the one that will survive contact with a logarithm.

The two forms are equal for t=0t = 0 and t=1t = 1, 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

ptp_t a little, so there is always a direction that increases it:

FIG 4What each candidate loss says about one example
0.001.252.503.755.000.00.30.50.81.0probability the model put on the observed label
negative log probability, the loss we are heading towardsone minus the probability, an alternative that seems reasonable
Both fall as the model improves, so both could serve. They differ where it matters: at 0.02 the linear version calls the example almost as bad as at 0.10, while the logarithmic one calls it five times worse. The next lesson derives which of these is the right shape and why.

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.

FIG 5From parameters to a single score
The whole of maximum likelihood, before any logarithm has appeared. The next lesson attacks exactly one thing on this diagram: the multiplication at the end.

A product of NN 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

NextWhy a Logarithm →

The rest of this course

  1. 01The Number a Model Is Really Asked Foryou are here
  2. 02Three Reasons the Logarithm Is Not Optionalopening only
  3. 03The Loss, Written Out in Fullopening only
  4. 04Turning Scores Into a Distributionopening only
  5. 05The Simplest Gradient in the Subjectopening only
  6. 06The Floor the Loss Cannot Go Belowopening only
  7. 07What a Perplexity of Twelve Meansopening only
  8. 08When a Low Loss Still Lies to Youopening only

Read alongside