Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsInformation Theory

Information and surprise

P1.E130 min●○○ BeginnerReviewed Dec 1, 2026

Why this matters for machine learning

Information theory measures how much a probability surprises you, and almost every loss function in machine learning is that surprise averaged over the data. The surprise of an outcome is the negative log of the probability the model gave it.

  • Cross-entropy, the standard classification loss, is the average surprise of the true labels under the model’s predicted probabilities. (P1.E3: Cross-entropy)
  • A language model is trained and scored by its loss in nats or bits per token, which is the surprise of each real next token under the model. (P10.A1: What is a Large Language Model?, coming soon)
  • Compression and coding need about as many bits as the surprise of what is sent, which is why entropy is also a limit on compression. (P1.E2: Entropy)
  • Training code sums log-probabilities instead of multiplying probabilities, because a product of thousands of small numbers underflows to zero. (P1.G2: Numerical stability, coming soon)

Before this: Exponents and logarithms, Summation, products and indices, Probability basics, Joint, marginal and conditional probability

After this lesson you can

  • Define the surprise (self-information) of an event as I(x)=−log⁡p(x)I(x)=-\log p(x), and explain why the three natural requirements (rarer means more surprising, certain means 00, surprises of independent events add) lead to a logarithm
  • Compute surprise in bits (log⁡2\log_2), nats (ln⁡\ln) and bans (log⁡10\log_{10}), convert between them, and read a bit as the answer to a fair yes/no question while knowing where that picture stops working
  • Compute the surprise of a sequence as a sum, including the case of dependent events through the chain rule, and say why software adds log-probabilities instead of multiplying probabilities
  • Explain that the loss −ln⁡p-\ln p of a classifier or language model is the surprise of the true outcome under the model, so it depends on the model and not only on the outcome

Two friends send you a message. One says, “the sun rose this morning.” The other says, “it snowed in the desert this morning.” Only the second message feels like news. Both are true, yet the first one tells you almost nothing, because you expected it. The second tells you a lot, because it was unlikely.

That feeling has a number behind it. Here is a puzzle that shows it. I pick a number from 11 to 10241024 at random. You may ask yes/no questions. How many do you need? Each good question halves the candidates, so ten questions are enough, because 1024=2101024=2^{10}. A one-in-10241024 event is worth ten yes/no questions.

This lesson turns “how surprising was that?” into a formula, shows why the formula has to use a logarithm, and then points at the place where it shows up in machine learning: the loss of a classifier is the surprise the model felt at the right answer.

What makes an event surprising

Take an outcome xx with probability p(x)p(x) (P1.D1). Call its surprise I(x)I(x) (the formal name is self-information). Three requirements are hard to argue with.

  1. Rarer means more surprising. If pp goes down, II goes up.
  2. Certain means no surprise. If p=1p=1, then I=0I=0. Surprise is not negative.
  3. Independent events add. Flip a coin and roll a die, and let the two not affect each other (P1.D5). The surprise of seeing both results should be the surprise of the first plus the surprise of the second, the way two separate news items carry two portions of news.

The third requirement is the one that decides the formula. For independent events the probabilities multiply (P1.D5): P(A∩B)=P(A) P(B)P(A\cap B)=P(A)\,P(B). So we need a function II with

I(p⋅q)=I(p)+I(q).I(p\cdot q)=I(p)+I(q).

A function that turns a product into a sum is the logarithm (P1.A3): log⁡(pq)=log⁡p+log⁡q\log(pq)=\log p+\log q. The log of a probability is negative or zero, and requirement 1 asks for a larger number when pp is smaller, so put a minus sign in front:

I(x)=−log⁡p(x)=log⁡1p(x).I(x)=-\log p(x)=\log\frac1{p(x)}.

A check on requirement 2: −log⁡1=0-\log 1=0. A check on requirement 3 with a fair coin (p=12p=\frac12) and a fair die (p=16p=\frac16): the pair has probability 112\frac1{12}, and log⁡212=log⁡22+log⁡26\log_2 12=\log_2 2+\log_2 6, which is 1+2.585=3.5851+2.585=3.585 bits.

Go deeper Why only the logarithm works

Suppose a function II on probabilities satisfies I(pq)=I(p)+I(q)I(pq)=I(p)+I(q), is decreasing, and changes smoothly (it is continuous). Then it must be a positive multiple of −log⁡p-\log p. This is a classical result about the equation f(pq)=f(p)+f(q)f(pq)=f(p)+f(q). A sketch for a fair coin shows the idea. Applying the rule nn times gives I(pn)=n I(p)I(p^n)=n\,I(p). So for p=12p=\frac12, a probability of (12)n\left(\frac12\right)^n has surprise n I ⁣(12)n\,I\!\left(\frac12\right). Roots follow the same way (I(p1/n)=1nI(p)I(p^{1/n})=\frac1n I(p)), so I ⁣(2−t)=t I ⁣(12)I\!\left(2^{-t}\right)=t\,I\!\left(\frac12\right) for every positive rational tt, and continuity extends it to every real tt. Writing q=2−tq=2^{-t} gives I(q)=−log⁡2q⋅I ⁣(12)I(q)=-\log_2 q\cdot I\!\left(\frac12\right).

The number I ⁣(12)I\!\left(\frac12\right) is a free choice. It is the unit. Choosing I ⁣(12)=1I\!\left(\frac12\right)=1 gives bits. Choosing it as ln⁡2\ln 2 gives nats. The logarithmic measure was proposed for information by Hartley in 1928 and became the basis of information theory in Claude Shannon’s 1948 paper “A Mathematical Theory of Communication”.

Units: bits, nats and bans

The base of the logarithm only changes the unit, like measuring a length in metres or in feet.

BaseUnitSurprise of a fair coin flipWhere it is common
22bitlog⁡22=1\log_2 2=1coding, compression, yes/no questions
eenatln⁡2=0.6931\ln2=0.6931calculus, NumPy and PyTorch (log is the natural log), most loss functions
1010ban (also hartley)log⁡102=0.3010\log_{10}2=0.3010older literature

Changing units is a multiplication by a constant (change of base, P1.A2):

bits=natsln⁡2,nats=bits×ln⁡2.\text{bits}=\frac{\text{nats}}{\ln2},\qquad \text{nats}=\text{bits}\times\ln2.

So 11 nat is 1ln⁡2=1.4427\frac1{\ln2}=1.4427 bits, and 11 bit is ln⁡2=0.6931\ln2=0.6931 nats. A ban is log⁡210=3.3219\log_2 10=3.3219 bits. Because the units differ only by a constant factor, the ranking of surprises does not depend on the base: the rarer event is more surprising in bits, in nats and in bans.

A word about words. The unit name “bit” is short for “binary digit”, and Shannon’s 1948 paper credits the short form to John W. Tukey. A “bit” of storage is a 00 or a 11 in memory. A “bit” of information is a measurement of surprise, and it does not have to be a whole number. A stored bit holds one bit of information when 00 and 11 are equally likely.

Here is a first table, with every value computed.

EventppSurprise in bitsSurprise in nats
a certain event110000
a fair coin lands heads12\frac12110.69310.6931
a fair die shows a 4416\frac162.5852.5851.7921.792
one in five0.20.22.3222.3221.6091.609
four in five0.80.80.3220.3220.2230.223
one in 1024102411024\frac1{1024}10106.9316.931
one in a hundred0.010.016.6446.6444.6054.605
ninety-nine in a hundred0.990.990.01450.01450.01010.0101
an impossible event00infiniteinfinite

An impossible event has −log⁡0-\log0, which is infinite: as pp gets closer to 00 the surprise grows without limit. (Software shows inf.) That infinite value matters later, because a model that gives the true answer probability 00 gets an infinite loss.

The surprise curve

Move the probability and watch the surprise. The curve is −log⁡p-\log p. The slider is linear in bits, so equal slider distances are equal changes in surprise (on an ordinary probability scale, most of the action happens in a tiny stretch near 00). With the dot focused, Page Up and Page Down move exactly one bit, which doubles or halves pp. The plot stops at p=11024p=\frac1{1024} (about 0.0010.001) so that the numbers stay readable. The real curve keeps rising past the left edge.

Probability and surprise

Units:
00.20.40.60.810246810coindie face1 in 10240.990.01

Horizontal: probability p, from 1 in 1024 (about 0.001) to 1. Vertical: surprise, −log₂ p in bits. The curve does not stop at the left edge; the plot does.

p = 0.5
one in 1/p: 1 in 2
surprise in bits = 1
surprise in nats = 0.693
like 1 fair coin flips, each landing as guessed (a whole number only when p is a power of 1/2)

p = 1/2: exactly 1 bit (0.693 nats). Each halving of the probability adds 1 bit, which is 0.693 nats.

Drag on the plot, or focus the dot and use the arrow keys (0.1 bit per press; Page Up and Page Down move 1 bit, which doubles or halves p; Home and End jump to 1 in 1024 and 1).

Surprise as a function of probability. The curve is the negative log of p, in bits or in nats. The blue dot is the chosen probability, and the readouts give the surprise in both units, ‘one in 1/p’ and the number of fair coin flips with the same surprise in bits. Grey, green and red markers show a fair coin, a die face, a one-in-1024 event, and a model that gave the true answer 0.99 or 0.01. The second panel puts two independent events side by side: their surprises, stacked, have the same length as the surprise of the pair computed from the product of the probabilities.

What to try:

  • Fair coin. Press “Fair coin”: 11 bit. Then press Page Down once: p=14p=\frac14 and the surprise is exactly 22 bits. Each halving of pp adds one bit.
  • Certain event. Press End: p=1p=1 and the surprise is 00.
  • Edge. Press Home: p=11024p=\frac1{1024} and the surprise is 1010 bits, as many as ten fair coin flips all landing as guessed.
  • Units. Switch between bits and nats: the shape stays the same and only the vertical scale changes, by the factor ln⁡2\ln 2.
  • Model right, model wrong. A model that gave the true answer 0.990.99 is barely surprised (0.01450.0145 bits). One that gave it 0.010.01 is surprised by 6.6446.644 bits.
  • Two events. Open the second panel. With two fair coin flips the two bars are 11 and 11, and the pair is 22 bits. Set the first probability to 11 (a certain event): it adds nothing.

Bits as yes/no questions

A bit has a concrete meaning when the outcomes are equally likely. With 10241024 equally likely items, each question can cut the list in half: “is it in the first 512512?”, and so on. Ten questions find the item, which is the 1010 bits of surprise of one particular item. For a million equally likely items, log⁡21,000,000=19.93\log_2 1{,}000{,}000=19.93, so twenty well-chosen questions are enough (220=1,048,5762^{20}=1{,}048{,}576). That is why the game “twenty questions” works.

Where the picture stops. A fair die has log⁡26=2.585\log_2 6=2.585 bits, but you cannot ask 2.5852.585 questions. One die needs three questions in the worst case. The fraction shows up when you ask about several dice together: 610=60,466,1766^{10}=60{,}466{,}176 outcomes of ten dice need 2626 questions in the worst case (225=33,554,4322^{25}=33{,}554{,}432 is too few and 2262^{26} is enough), which is 2.62.6 per die, and the per-die cost approaches 2.5852.585 as the number of dice grows. And an event with probability 0.80.8 has 0.3220.322 bits, less than one question: a likely answer teaches you little. So read “bits are yes/no questions” as exact for probabilities that are powers of 12\frac12, and as a guide for the rest.

Surprise adds for independent events

Take a coin that lands heads with probability 0.80.8 and flip it three times, getting H, H, T. If the flips do not affect each other, the probability of the sequence is 0.8×0.8×0.2=0.1280.8\times0.8\times0.2=0.128. The surprises add:

The first H costs −ln⁡0.8=0.2231-\ln0.8=0.2231, the second H costs 0.22310.2231 again, and the T costs −ln⁡0.2=1.6094-\ln0.2=1.6094 (all in nats). The sum equals the surprise of the whole sequence:

0.2231+0.2231+1.6094=2.0557−ln⁡0.128=2.0557\begin{aligned} 0.2231+0.2231+1.6094&=2.0557\\ -\ln0.128&=2.0557 \end{aligned}

The number is the one from the likelihood table of P1.D10: the log-likelihood of H, H, T at θ=0.8\theta=0.8 was −2.0557-2.0557, so the negative log-likelihood is the total surprise. In bits the same sequence is 0.3219+0.3219+2.3219=2.96580.3219+0.3219+2.3219=2.9658 bits. The tail is by far the largest part, because it was the unlikely result.

Independence is needed. If the events depend on each other, the probability of both is P(A∩B)=P(A) P(B∣A)P(A\cap B)=P(A)\,P(B\mid A) (the chain rule, P1.D5), and the surprise splits the same way:

I(A∩B)=I(A)+I(B∣A).I(A\cap B)=I(A)+I(B\mid A).

The second term is the surprise of BB after AA is known. Example: it rains with probability 0.30.3 (1.7371.737 bits), and given rain, someone carries an umbrella with probability 0.90.9 (0.1520.152 bits). The surprise of “rain and an umbrella” is 1.737+0.152=1.8891.737+0.152=1.889 bits, which is −log⁡20.27-\log_2 0.27. Adding the surprise of the umbrella without the rain information would use the wrong probability.

Surprise of a model’s prediction

Now the connection to machine learning. A classifier outputs a probability for each class. After the true class is revealed, look at the probability pp the model had given to it. The model’s surprise at the truth is −ln⁡p-\ln p. P1.D3 already showed these numbers as losses: a spam model that said 0.80.8 for spam has loss −ln⁡0.8=0.2231-\ln0.8=0.2231 when the email is spam, and if the email is not spam the truth had probability 0.20.2, so the loss is −ln⁡0.2=1.6094-\ln0.2=1.6094. This is the log loss, and it is the surprise in nats.

pp given to the truth0.90.90.50.50.10.10.010.01
Loss in nats, −ln⁡p-\ln p0.1050.1050.6930.6932.3032.3034.6054.605
Bits, −log⁡2p-\log_2 p0.1520.152113.3223.3226.6446.644

A model that was confident and right (0.990.99) has a loss of 0.01010.0101. A model that was confident and wrong (0.010.01) has 4.6054.605, about 458458 times as much. The loss grows like the log of how unlikely the model thought the truth was, with no upper limit as pp approaches 00.

Surprise belongs to an outcome and a model together. Suppose it rains. A forecaster who said 90%90\% is unsurprised, −log⁡20.9=0.152-\log_2 0.9=0.152 bits. A forecaster who said 10%10\% is surprised by −log⁡20.1=3.322-\log_2 0.1=3.322 bits. The event is the same, and the surprise differs, because the surprise is measured with the probability a particular model gave. A model with probabilities that are far from the real frequencies will be surprised more often than a well-calibrated model on the same data.

Surprise also depends on how the event is described. Any exact sequence of ten fair coin flips, such as H T T H H H T H T T, has probability 2−102^{-10} and surprise 1010 bits. But the event “some sequence of ten flips happened” has probability 11 and surprise 00. A rare-looking detail is not news when everything has a rare-looking detail.

Surprise of a sequence, and tokens

A language model reads text token by token and gives a probability to each next token, given the tokens before it (P1.D5, chain rule). The probability of a text is the product of those probabilities, so the surprise of the text is the sum of the surprises of its tokens:

I=∑t=1T−log⁡p(xt∣x<t).I=\sum_{t=1}^{T}-\log p\bigl(x_t\mid x_{\lt t}\bigr).

Here xtx_t is the token at position tt and x<tx_{\lt t} stands for all the tokens before it.

Dividing by the number of tokens TT gives the average surprise per token, reported as nats per token or bits per token. When a library reports a training loss for a language model, it is this average, in nats for the usual frameworks (PyTorch’s cross-entropy uses the natural log). To compare numbers from two sources, check the unit: bits per token is nats per token divided by ln⁡2\ln2.

Example: a model gives three tokens the probabilities 0.50.5, 0.250.25 and 0.1250.125. The surprises are 11, 22 and 33 bits, adding to 66 bits (4.1594.159 nats). The average is 22 bits per token, or 1.3861.386 nats per token. The number 22=42^2=4 (equivalently e1.386=4\mathrm{e}^{1.386}=4) is the perplexity: the model is about as unsure as a fair choice between four options at each step. P1.E5 develops it.

Why software adds log-probabilities

The product of many probabilities is a very small number. A thousand tokens, each with probability 0.10.1, have a product of 10−100010^{-1000}, which a computer stores as 0.0 (P1.D10). The sum of the surprises is an ordinary number: 1000ln⁡10=2302.5851000\ln10=2302.585 nats, or 3321.93321.9 bits. So training code keeps log-probabilities, adds them, and returns a loss that never needs the tiny product. Frameworks go one step further and compute the log-probabilities directly from the model’s raw scores (the logits) with a stable formula, instead of computing a probability and taking its log (P1.G2). That also avoids taking the log of an exact 00.

What comes next

Surprise is about one outcome. Four later lessons average it, compare it and combine it.

  • Entropy (P1.E2): the average surprise of a source. A fair coin averages 11 bit. A coin that lands heads with probability 0.990.99 averages only 0.08080.0808 bits per flip, because the common result is rarely surprising.
  • Cross-entropy (P1.E3): the average surprise of the true outcomes under the model’s probabilities. That is the classification loss, and the average of the numbers in the table above.
  • KL divergence (P1.E4): the extra average surprise a model has beyond the best possible.
  • Mutual information and perplexity (P1.E5): how much knowing one variable reduces the surprise of another, and perplexity as the effective number of choices.
Go deeper Surprise for continuous values

The definition needs an outcome with a probability, so it fits discrete outcomes such as a class or a token. For a continuous quantity, a single exact value has probability 00 (P1.D2). The negative log of a density is sometimes used, but a density can be larger than 11, which makes −ln⁡-\ln of it negative, and its value changes with the units of xx. P1.D10 had the same issue when comparing log-likelihoods. So for continuous data, surprise is meaningful for ranges of values (an interval has a probability), and for comparing models on the same data. This lesson stays with discrete events.

Go deeper Surprise is not importance

Shannon’s measure counts how unlikely an outcome was under a model. It does not say whether the outcome matters or what it means. A long string of random letters is highly surprising and carries no meaning. “The server is down” may be a likely message on a bad day and still be the most useful message you read. Information theory measures the cost of describing an outcome, and judging value takes more than that.

Try it

Try it: surprise in bits and nats, sums, and a thousand tokens

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

The first block prints 00 bits for the certain event, 11 bit for the coin, 2.5852.585 for the die, 1010 for one in 10241024, and 0.01450.0145 and 6.64396.6439 bits for the model that gave the truth 0.990.99 and 0.010.01 (the same values in nats are 0.01010.0101 and 4.60524.6052). The sum of surprises for H, H, T is 2.96582.9658 bits or 2.05572.0557 nats, equal to the surprise of the product. The three tokens give 22 bits and 1.38631.3863 nats per token and a perplexity of 44. The thousand-token product prints 0.0, while the total surprise is 2302.5852302.585 nats, which is 3321.93321.9 bits. The last line prints inf. Challenge: change the coin’s 0.80.8 to 0.50.5 and check that every flip adds exactly 11 bit.

Intuition, math and code at a glance

Intuition

Surprise is how unexpected an outcome was under your model. A likely outcome is hardly surprising, and a rare one is very surprising. Taking the log makes surprises add when events are independent, like adding the lengths of two ropes. Each halving of the probability adds one bit: ten halvings take you from certainty to one in 10241024.

Math

I(x)=−log⁡p(x)=log⁡1p(x)I(x)=-\log p(x)=\log\frac1{p(x)}

Adding surprises, and changing units:

I(A∩B)=I(A)+I(B)I(A∩B)=I(A)+I(B∣A)bits=nats/ln⁡2\begin{aligned} I(A\cap B)&=I(A)+I(B)\\ I(A\cap B)&=I(A)+I(B\mid A)\\ \text{bits}&=\text{nats}/\ln2 \end{aligned}

The first sum holds for independent events and the second holds in general.

Facts to remember:

  • I(1)=0I(1)=0, and I(p)I(p) is larger when pp is smaller
  • a fair coin is 11 bit, a fair die is log⁡26=2.585\log_2 6=2.585 bits, and p=2−kp=2^{-k} is kk bits
  • the loss −ln⁡ptrue-\ln p_{\text{true}} is the model’s surprise at the truth, in nats

Code

Compute surprise in bits and nats with NumPy.

import numpy as np

p = np.array([0.5, 1 / 6, 0.01])
print(np.log2(1 / p))          # about [1, 2.585, 6.644] bits
print(np.log(1 / p))           # about [0.693, 1.792, 4.605] nats

Common mistakes

Check your understanding

  1. A fair six-sided die lands on 44. How surprising is this, in bits?

  2. Two independent events have surprises of 33 bits and 44 bits. How surprising is it that both happen?

  3. A classifier gave the true class probability 0.50.5 on one example and 0.010.01 on another. How many more nats of surprise did the second example cause?

  4. Which property of the logarithm makes it the right function for the surprise of independent events?

  5. A language model reports an average loss of 33 nats per token. About how many bits per token is that?

Exercises

  1. Find the surprise in bits and in nats of an event with probability 18\frac18, one with probability 0.20.2, and one with probability 0.990.99.
    Show answerFor 18\frac18: log⁡28=3\log_2 8=3 bits and 3ln⁡2=2.0793\ln2=2.079 nats. For 0.20.2: log⁡25=2.322\log_2 5=2.322 bits and ln⁡5=1.609\ln 5=1.609 nats. For 0.990.99: 0.01450.0145 bits and 0.01010.0101 nats.
  2. A lottery ticket wins with probability 11 in 1,000,0001{,}000{,}000. How surprising is a win in bits and nats, and about how many fair coin flips in a row is that?
    Show answerlog⁡2106=19.93\log_2 10^6=19.93 bits and ln⁡106=13.82\ln10^6=13.82 nats. It is as surprising as about 2020 fair coin flips all landing as guessed.
  3. An item is chosen at random from 100100 equally likely items. What is its surprise in bits, and how many yes/no questions do you need in the worst case?
    Show answerlog⁡2100=6.644\log_2 100=6.644 bits. Seven questions are needed, because 26=642^6=64 is less than 100100 and 27=1282^7=128 is enough. The 6.6446.644 is not a whole number of questions, so the picture of questions fits only roughly.
  4. A card drawn from a deck is a heart (p=14p=\frac14), and a separate fair die shows a 66 (p=16p=\frac16). Assume the two do not affect each other. Find each surprise and the surprise of both.
    Show answerHeart: 22 bits. Six: 2.5852.585 bits. Both: 124\frac1{24}, so log⁡224=4.585\log_2 24=4.585 bits, which is the sum 2+2.5852+2.585.
  5. It rains. Forecaster A had said 0.90.9 and forecaster B had said 0.10.1. Find each one’s surprise in bits and in nats.
    Show answerA: 0.1520.152 bits and 0.1050.105 nats. B: 3.3223.322 bits and 2.3032.303 nats. The event is the same, and the surprises differ because the models gave different probabilities.
  6. It rains with probability 0.30.3, and given rain an umbrella appears with probability 0.90.9. What is the surprise of rain and an umbrella, in bits?
    Show answerP(both)=0.3×0.9=0.27P(\text{both})=0.3\times0.9=0.27, and −log⁡20.27=1.889-\log_2 0.27=1.889 bits. Equivalently I(rain)+I(umbrella∣rain)=1.737+0.152=1.889I(\text{rain})+I(\text{umbrella}\mid\text{rain})=1.737+0.152=1.889 bits.
  7. A language model gives three tokens the probabilities 0.50.5, 0.250.25 and 0.1250.125. Give the total surprise and the surprise per token in bits and nats, and the perplexity.
    Show answerThe bits are 11, 22, 33, adding to 66 bits, or 6ln⁡2=4.1596\ln2=4.159 nats. Per token: 22 bits or 1.3861.386 nats. Perplexity: 22=e1.386=42^2=\mathrm{e}^{1.386}=4.
  8. A model gives 500500 independent examples a probability of 0.050.05 each. What is the total surprise in nats and in bits, and why does the product of the probabilities fail on a computer?
    Show answer500ln⁡20=1497.87500\ln20=1497.87 nats, or 2160.962160.96 bits. The product is about 10−650.510^{-650.5}, below the smallest positive float (about 5×10−3245\times10^{-324}), so it is stored as 0.00.0.
  9. A model’s average loss is 0.6930.693 nats per token. How many bits per token is that, and what does it say about the model’s choices?
    Show answer0.693/ln⁡2=10.693/\ln2=1 bit per token, the surprise of a fair coin flip (perplexity 22): on average the model is as unsure as a fair choice between two options.
  10. Explain why a specific sequence of ten fair coin flips has 1010 bits of surprise and “some sequence of ten flips” has 00.
    Show answerOne particular sequence has probability 2−10=110242^{-10}=\frac1{1024}, so its surprise is 1010 bits. The event “some sequence” covers all 10241024 sequences and has probability 11, so its surprise is −log⁡1=0-\log1=0. Surprise depends on how finely the event is described.

Next: P1.E2 averages the surprise over a source and calls it entropy, which is also a limit on how far lossless compression can go. P1.E3 averages the surprise of the true labels under a model’s probabilities and gets cross-entropy, the classification loss. P1.E4 measures the extra surprise from using the wrong model, and P1.E5 covers mutual information and perplexity.

Key takeaways

  • The surprise (self-information) of an outcome with probability pp is I=−log⁡p=log⁡1pI=-\log p=\log\frac1p. A certain event (p=1p=1) has surprise 00, rarer events have more, and an impossible event would have infinite surprise.
  • The logarithm is chosen because probabilities of independent events multiply, P(A∩B)=P(A)P(B)P(A\cap B)=P(A)P(B), and the log turns a product into a sum, so I(A∩B)=I(A)+I(B)I(A\cap B)=I(A)+I(B). With continuity, the logarithm is the only function with this property, up to the choice of units. Without independence, the second term is conditional: I(A∩B)=I(A)+I(B∣A)I(A\cap B)=I(A)+I(B\mid A).
  • The base of the log sets the unit: log⁡2\log_2 gives bits, ln⁡\ln gives nats, log⁡10\log_{10} gives bans (hartleys). 11 nat =1/ln⁡2≈1.4427=1/\ln2\approx1.4427 bits. A fair coin flip is 11 bit, a fair die roll is log⁡26≈2.585\log_2 6\approx2.585 bits, and each halving of the probability adds exactly 11 bit.
  • A model that gives the true outcome probability pp has the loss −ln⁡p-\ln p, which is the surprise of the truth in nats. A confident wrong prediction is very surprising (−ln⁡0.01=4.605-\ln0.01=4.605 against −ln⁡0.99=0.0101-\ln0.99=0.0101). The same outcome has different surprise under different models.
  • Language-model losses are surprises per token, in nats or bits. A sequence’s surprise is the sum of its tokens’ surprises, and software adds log-probabilities because a product of many probabilities underflows to 00. The average surprise is entropy (P1.E2), and the average surprise of the truth under a model is cross-entropy (P1.E3).
Found a mistake? Tell us