1.3.19 · D1Probability & Statistics

Foundations — Cross-entropy concept

1,909 words9 min readBack to topic

Before you can read a single line of the parent note, you need to own every symbol it throws at you. This page assumes you have seen none of them. We build each one from a picture, in the order that each one leans on the one before it.


0. What is a "probability" and how do we picture it?

A probability is just a number between and that says how likely something is.

  • means "never happens"
  • means "always happens"
  • means "happens 7 times out of 10 in the long run"

The picture: think of a bar of length chopped into pieces. Each piece is one possible outcome, and the width of the piece is its probability. Because something must happen, all the pieces together fill the whole bar — the widths add up to .

Figure — Cross-entropy concept

Why the topic needs it: cross-entropy compares two of these sliced bars — reality's bar and the model's bar.


1. The symbols , ,

Now we can name the pieces.

The picture: two bars stacked on top of each other. Same slices ("cat", "dog", "bird"), but the widths differ — because the model doesn't know reality perfectly.

Figure — Cross-entropy concept

Why the topic needs it: the whole game of cross-entropy is "reality happens with frequency , but our model only believed ." Two bars, one truth, one guess.


2. The summation symbol

The picture: you point at each slice of the bar in turn, read off its number, and drop it into a running total.

Why the topic needs it: cross-entropy is an average over all outcomes. Sigma is how we say "loop over every slice and total up the surprise, weighted by how often it happens."


3. The logarithm

This is the one symbol most people fear, so we build it slowly from a picture.

Why this tool and not, say, a square root or a plain fraction? Because we want a function that turns multiplying into adding. That single property — — is exactly what we need later: surprises from two independent events should add, but their probabilities multiply. The log is the only tool that converts one into the other.

The picture: for probabilities (numbers between and ), the log curve dives downward. As (rarer and rarer), . As (certain), .

Figure — Cross-entropy concept

Why the topic needs it: log turns tiny probabilities into big numbers with a minus sign in front — the raw material of "surprise."


4. Surprise: the minus-log

Notice the log of a probability is always negative or zero (the curve lives below the axis). Surprise should be a positive feeling, so we flip the sign.

The picture: take the diving log curve from figure s03 and flip it above the axis. Now it rises toward infinity as — "the rarer it is, the more shocked I am."

Sanity check on the cases:

  • . No surprise, you knew it. ✔
  • nats. Mild surprise. ✔
  • nats. Big surprise. ✔
  • → surprise . An "impossible" event happening is infinitely shocking. ✔

Why the topic needs it: cross-entropy is average surprise. This is the thing being averaged. See Shannon Entropy for the case where you average surprise using the same distribution you're surprised by.


5. Averaging surprise: and

Now stitch and surprise together.

Read the shape of left to right:

  1. Walk over every slice (that's the ).
  2. Ask "how often does this slice really happen?" → weight .
  3. Ask "how surprised was the model?" → .
  4. Multiply weight × surprise, and total them up.

The result is the model's average surprise across reality, which is the whole topic in one formula.


6. The gap between them:

The picture: two stacked bars — total height (model's surprise) sits on top of the shorter (reality's unavoidable surprise). The extra sliver on top is .

Why the topic needs it: is fixed (you can't change reality). So training a model to shrink is the same as shrinking that sliver — pushing your guess bar to match reality's bar. See KL Divergence.


7. Where the guesses come from: one-hot labels & softmax

Two last symbols the parent leans on.

Why the topic needs it: in classification, the true bar is one-hot and the guessed bar is a softmax output. Cross-entropy between them is the loss you minimise (see Categorical Cross-Entropy and Logistic Regression).


The prerequisite map

Probability 0 to 1

Distribution p and q

Summation sigma

Logarithm log

Surprise minus log p

Entropy H of p

Cross-entropy H of p q

KL divergence

One-hot labels

Softmax gives valid q

Cross-entropy loss

Read it top to bottom: probabilities give distributions; logs give surprise; sigma averages surprise into entropy and cross-entropy; their gap is KL; softmax and one-hot supply the two bars; everything feeds the final loss.


Equipment checklist

Self-test: can you answer each before revealing?

What does a single probability of mean in the long run?
The outcome happens about 7 times out of every 10 trials.
What must all the slices of a distribution add up to, and why?
They sum to , because some outcome must happen.
In , which distribution is reality and which is the model?
is reality (true), is the model's guess.
What does the symbol command you to do?
Loop over every outcome and add up whatever follows it.
What single question does ask?
How many times must I multiply the base () by itself to reach ?
Why use instead of another function for surprise?
Only turns multiplying probabilities into adding surprises: .
What is the surprise of an outcome with probability ?
Zero — , you already knew it would happen.
What happens to surprise as a probability approaches ?
It grows without bound toward .
In , which factor sets how often a surprise is felt, and which sets how big it is?
(outside the log) sets frequency; (inside) sets size.
What is in words?
The extra average surprise caused purely by the model's guess differing from reality .
When is exactly zero?
Only when (the model matches reality perfectly).
For a one-hot true label, what does cross-entropy simplify to?
— every zero-probability term drops out.
Why do we pass model scores through softmax before cross-entropy?
To make a valid distribution (positive, summing to 1) so is well-defined.