Writing Down What Does Not Settle on One Answer — Probability
Chapter 0-2 settled "which way makes it better." What is left is what counts as better. The training objective — the loss — did not fall from the sky. Write down one assumption in the language of probability and the loss is decided for you.
Probability shows up in deep learning in three places.
- The output itself. The 0.87 a classifier returns is a probability: how sure it is.
- The objective. Cross-entropy and squared error both follow from distributional assumptions (section 5).
- The gradient itself. A minibatch gradient is an estimate of the full-data gradient, and estimates carry error (section 6).
This chapter lays down exactly what those three need.
1. Sample space, events, three promises
Definition. The set of all possible outcomes is the sample space ; its subsets are events. A probability is a number satisfying:
- if (they cannot both happen) then
That is all. What probability means — a long-run frequency, a degree of belief — is not in these three. The arithmetic needs only the three promises; the interpretation sits outside it.
The third gives a familiar form at once: and cannot both happen and together make , so .
2. Conditional probability and Bayes — swapping the direction
Definition. For , the probability of given that happened is
Equation (1) swaps the denominator. Stop measuring inside the whole world and measure it inside instead.
Dividing the ends of by ,
Bayes' theorem. The derivation is one line and the content is "the same written two ways." It earns its keep because the direction you can measure and the direction you want rarely match. A test gives you , the fraction of sick people who test positive; you want , the fraction of positives who are sick. Equation (2) swaps them, and the swap always costs you a — how likely it was beforehand. Dropping that term is this chapter's common misreading.
Independence. When , and are independent. Substituting into equation (1) gives : learning does not move your view of .
3. Random variables, expectation, variance
Attach a number to each outcome and you have a random variable (a die face, an image label, one example's gradient).
Definition. The expectation of a random variable is
and its variance is .
Expectation is a weighted average. Variance measures spread around that average, and expanding gives
Its square root is the standard deviation, carried in the same units as .
Worked numbers. For a fair die, , , , .
Only two properties are needed here.
- Linearity. — this holds even when they are dependent.
- Variances add when independent. If are independent, . This one needs independence.
From the second, for the mean of independent observations,
The centre is right whatever is; only the spread shrinks, and it shrinks by . Equation (5) is the workhorse of this chapter and returns in section 6 as minibatching. Note , not .
4. Three distributions worth knowing
Bernoulli. One shot at 0 or 1. With , for — since is 0 or 1, one exponent is always 0 and that factor disappears. Mean , variance . The that a binary classifier outputs is this .
Categorical. One of . with and . Softmax is the map that forces numbers into exactly those constraints: makes them positive with the exponential and sums to one by dividing — one step per constraint.
Gaussian (normal). Continuous values.
with mean and variance . Assume it in regression and squared error falls out (next section).
5. Maximum likelihood — where the loss came from
Given data and a model that emits a conditional distribution , choose so that the data you actually have is as likely as possible. That is maximum likelihood.
Three things happen at that equals sign. Take logs (products become sums, and the underflow of multiplying small numbers goes away; is increasing so the arg max does not move). Flip the sign (maximise becomes minimise). Divide by (so the scale does not change with the number of examples).
Now substitute the distributions from section 4 and familiar losses appear.
Substituting Bernoulli. With and , , so equation (6) becomes
Cross-entropy. Chapter 1 of Part III reached the same expression by saying "we want large where the label is 1 and large where it is 0; written together in one line." What that "written together" was is the Bernoulli probability itself.
Check it numerically. For and the likelihood product is , and . Computing equation (7) directly also gives — identical, to zero difference.
Substituting Gaussian. With , one example's negative log-likelihood is
The second term has no in it. Minimise over and only squared error remains; fixing leaves nothing but a constant factor. For , , , the mean negative log-likelihood is and the mean squared error , differing by exactly — a constant, as promised.
6. Minibatches — the gradient is an estimate, not a measurement
One training step's gradient is really an average over all the data:
With in the millions, every single step would read millions of examples. So draw of them and use their average — that is a minibatch, and equation (5) applies verbatim. The centre is right (unbiased); the spread is .
Measured. Two-dimensional logistic regression, 1,000 examples (seed 20261013), evaluated at , : I formed the per-example gradient for all of them. The full-data gradient is and the per-example standard deviation is . Drawing of them and averaging, 20,000 times for each :
| measured standard error | ratio | ||
|---|---|---|---|
| 1 | 0.371975 | 0.375821 | 0.990 |
| 4 | 0.187782 | 0.187911 | 0.999 |
| 16 | 0.092750 | 0.093955 | 0.987 |
| 64 | 0.046893 | 0.046978 | 0.998 |
| 256 | 0.023700 | 0.023489 | 1.009 |
At the error, , is more than four times the gradient itself, . Steps that point the wrong way are routine. Training still works because the estimate is unbiased: average enough of them and they pull the right way. The first half of equation (5) is that guarantee — and nothing guarantees any individual step.
Out of this comes an operational number. Four times the batch costs four times the compute and halves the error. Going takes the error from to , for four times the work. That is why bigger batches are not simply better.
A common misreading: "a 99%-accurate test came back positive, so I'm 99% likely to be ill"
Drop the in equation (2) — how likely it was beforehand — and this is what you get. The numbers make the gap plain.
Take a disease with prevalence 0.1%, a test with sensitivity (sick people who test positive) 99% and specificity (healthy people who test negative) 99%. Count out 10,000 people.
| positive | negative | |
|---|---|---|
| ill (10 people) | 9.9 | 0.1 |
| healthy (9,990 people) | 99.9 | 9,890.1 |
people test positive, of whom are actually ill.
Nine per cent, not ninety-nine. The test did nothing wrong. There are 9,990 healthy people, so even a 1% error rate produces 99.9 of them, swamping the 9.9 real cases. In equation (2), the small factor multiplies the numerator only.
Change the prevalence and the answer changes, with the test untouched.
| prevalence | |
|---|---|
| 0.1% | 9.0164% |
| 1% | 50.0000% |
| 10% | 91.6667% |
| 50% | 99.0000% |
At 1% it is exactly a coin flip; only at 50% does it reach 99%.
Exercises
The answers are folded away. Work them first.
Exercise 1. Apply the same test (sensitivity 99%, specificity 99%) to a population with prevalence 10%. (a) What is ? (b) Why does the answer change when the test did not? (c) What is the classifier equivalent of this story?
Show the answer
(a) 91.6667%. Out of 10,000: 1,000 ill and 9,000 healthy. Positives are and , so 1,080 in total, of which 990 are real. .
(b) Because in equation (2) changed. A test hands you and , neither of which contains the prevalence. The quantity you want, , multiplies by before taking the ratio. Same instrument, different population, different answer.
(c) The gap between the positive rate in the training data and the positive rate where the model is used. Train on a 50/50 sample of positives and deploy into a 0.1%-positive world and you travel between the top and bottom rows of the table in the text. The model has not changed; its precision in the field collapses anyway.
Exercise 2. At some point in training, the per-example gradient has standard deviation . (a) The standard error at . (b) What halves it? (c) What divides it by ten? (d) What does that tell you?
Show the answer
(a) .
(b) (four times). Halving means doubling , which means quadrupling .
(c) (a hundred times), since the error goes as .
(d) Buying accuracy with batch size gets worse and worse value. Compute grows with while error falls with : pay 100× in compute, get 10× in error. Large batches win for a different reason — the same work packs into parallel hardware. Balancing that against the learning rate is Chapter 4.
Exercise 3. Regression assuming . (a) Write one example's negative log-likelihood and point out the term with no in it. (b) Does doubling change the minimising ? (c) What happens when the errors are not Gaussian but include outliers?
Show the answer
(a) . The second term has no , so it differentiates away and cannot affect the minimiser.
(b) No. Changing scales the first term by , and a positive constant factor does not move the location of a minimum. The size of the gradient does change, so at a fixed learning rate your effective step size changes.
(c) One outlier drags everything. Squared error punishes by the square of the miss, so a point off by weighs as much as a hundred points off by . Gaussian tails are exponentially thin — the assumption said large misses essentially never happen — so when one does, the assumption is what is wrong. Swap the assumed distribution, not the loss by taste: assume a heavy-tailed distribution and the same derivation hands you absolute error or the Huber loss.
What this chapter does not say
- Entropy and KL divergence belong to the next chapter (0-4). This chapter stops at maximising likelihood; where the name "cross-entropy" comes from — its meaning as a quantity in information theory — is not touched. The formula itself is fully derived in section 5.
- Bayesian inference (carrying a posterior around) is not covered. Equation (2) was used as a one-off calculation; putting priors on parameters and training distributions is a different programme.
- Continuous random variables are not treated rigorously (measure, and how a density differs from a probability). A density is not a probability and can exceed 1; nothing computed here depends on that distinction.
- The central limit theorem is shown in the figure, not stated or proved. Equation (5) — variance — needs only independence; "it goes bell-shaped" is a separate claim.
- The minibatch measurement is one point in parameter space and one component of the gradient. As training moves, moves too. What does not move is the shape.
- Every number in the text was computed and checked (the die's variance, the Bayes table and all four prevalences, the five standard errors, the likelihood product against cross-entropy, and the gap between Gaussian negative log-likelihood and squared error being exactly ).
Going deeper
- Probability and statistics for AI — a model's output is a probability
- Bayes' theorem from scratch — why a 99%-accurate test becomes a coin flip
- A zoo of distributions — where normal, Poisson and the exponential family come from
- Monte Carlo from scratch — solving integrals with dice
- Statistical learning theory — why learning generalises at all
Comments
Sign in to comment