The Maths You Actually NeedLesson 44 of 76

Module 5 · Logarithms, information and the shape of a loss

Cross-entropy and KL: the cost of believing the wrong distribution

In this lesson

  • 10 min read
  • 8 reading sections
  • Knowledge check
  • Lesson 6 of 10 in this chapter

This chapter’s goalCompute a cross-entropy and a KL divergence by hand from two small distributions and say which part of a training loss is irreducible, derive the squared-error and cross-entropy losses from a stated noise assumption, compute a softmax and its gradient for a given set of logits, and explain by the overflow limit of float32 why every library subtracts the maximum before exponentiating.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
Where a cross-entropy of 2.00 bits actually goesCross-entropy, 2.00 bitswhat a model believing q pays per outcomeKL divergence, 0.25 bitsthe removable part: how wrong the belief isEntropy of the data, 1.75 bitsthe floor, which no model can go beneathCross-entropy is entropy plus KL divergence. Training against fixed data can only shrink the secondterm, so a loss that plateaus above zero may be sitting on the floor rather than failing. Compute thefloor from your own label column before judging any curve.
Cross-entropy is entropy plus KL divergence. Training against fixed data can only shrink the second term, so a loss that plateaus above zero may be sitting on the floor rather than failing. Compute the floor from your own label column before judging any curve.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

Paying with the wrong code

Entropy is what you pay when your predictions match reality. Cross-entropy is what you pay when they do not. If outcomes arrive with probabilities p but you built your code, or your model, around probabilities q, the average cost is

Text
H(p, q) = − Σ p_i × log2(q_i)

The probabilities that decide how often you pay are the true ones, p. The probabilities that decide how much you pay each time are yours, q.

Take the reality from the last lesson, p = (0.5, 0.25, 0.125, 0.125), whose entropy is 1.75 bits. Suppose your model believes the four outcomes are equally likely, q = (0.25, 0.25, 0.25, 0.25). Every outcome now costs −log2(0.25) = 2 bits, so H(p, q) = 2 bits. You pay a quarter of a bit more per outcome than a perfect model would.

Now a model that is wrong in a different way, q = (0.7, 0.1, 0.1, 0.1):

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Funke, your AI tutor

How to cite: Addaly (2026) ‘Cross-entropy and KL: the cost of believing the wrong distribution’, in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/cross-entropy-and-kl-divergence (Accessed: 29 November 2026).