Formula Bro

Math and physics formulas, explained like you are new.


Cross-Entropy Loss

In one sentence: Cross-entropy loss gives a small penalty when the model gave the right answer a high probability, and a big penalty when it gave the right answer a small probability.

L=−∑i=1Kyiln⁡(ŷi)L = -\sum_{i=1}^{K} y_i \ln(\hat{y}_i)

Read it out loud: “L equals minus the sum, for i from 1 up to K, of y sub i times the natural log of y hat sub i. Here L is the loss, K is the number of classes, y sub i is the true label, which is 1 or 0, and y hat sub i is the probability the model gave to class i.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

The natural logarithm ln\ln

The natural logarithm, written ln\ln and read ‘ln’, asks a question: what power of ee gives this number? So ln⁡(e)=1\ln(e) = 1, because ee to the power 1 is ee, and ln⁡(1)=0\ln(1) = 0, because ee to the power 0 is 1. For a number between 0 and 1, the answer is negative. For example, ln⁡(0.5)≈−0.693\ln(0.5) \approx -0.693 and ln⁡(0.1)≈−2.303\ln(0.1) \approx -2.303. The smaller the number, the more negative its log.

The minus sign in front of a log

A minus sign in front flips the sign of the number. Because a probability is between 0 and 1, its natural log is always negative or zero. The minus sign turns those negative logs into positive penalties. So −ln⁡(0.5)=0.693-\ln(0.5) = 0.693, and −ln⁡(0.1)=2.303-\ln(0.1) = 2.303. This is why the loss is zero or positive, and why a lower loss means a better model.

Labels that are 0 or 1 (one-hot labels)

A label is the correct answer written as numbers. In a one-hot label, every class gets a 0 except the correct class, which gets a 1. If the photo really is a cat, the label is (1,0,0)(1, 0, 0) for cat, dog and bird. Multiplying by 0 removes a term from a sum, and multiplying by 1 keeps it, so a one-hot label keeps only the correct class in the total.

What a loss is

A loss is one number that says how wrong a model was. Training is the process of making the loss smaller. A loss of 0 would mean a perfect score, and a bigger loss means a bigger mistake. Different tasks use different loss formulas, and cross-entropy is the usual choice when the model picks one class out of several.

What each symbol means

SymbolMeaning

LL

The loss, one number that says how wrong the model was on this example. Lower is better, and 0 is a perfect score. When the natural log is used, the unit is the nat, a unit of information used with natural logs, where the bit goes with base-2 logs. Most people read the loss as a plain score with no everyday unit.

KK

The number of possible classes, such as cat, dog and bird. In the example, K=3K = 3. It is a whole number with no unit, and it sets where the sum sign stops.

ii

A counter that gives the position of one class in the list, starting at 1. The first class is i=1i = 1, the second is i=2i = 2, and so on up to KK. It has no unit and is only a label.

∑i=1K\sum_{i=1}^{K}

The sum sign, read ‘add up for every ii from 1 to KK’. It goes through every class and totals the terms. Most terms are zero for a one-hot label, so in practice only the correct class’s term remains.

yiy_i

The true label for class ii. It is 1 for the correct class and 0 for every other class. It has no unit. It is the answer you know is right, written as numbers so the formula can use it.

ŷi\hat{y}_i

The probability the model gave to class ii. It is a number between 0 and 1, and the hat over the yy marks it as the model’s prediction. For each example, the probabilities over all the classes should add up to 1, which is the usual output of softmax.

ln\ln

The natural logarithm, the log with base ee. It asks what power of ee gives the number inside the bracket. Any number between 0 and 1 gives a negative log, which is why the formula has a minus sign in front of it.

The formula piece by piece

ln⁡(ŷi)\ln(\hat{y}_i)

Start with the probability given to one class and take its natural log. Because ŷi\hat{y}_i is between 0 and 1, this log is negative. When the probability is close to 1, the log is close to 0. When the probability is close to 0, the log is a large negative number.

yiln⁡(ŷi)y_i \ln(\hat{y}_i)

Multiply the log by the true label. For a wrong class, yiy_i is 0, so the product is 0 and that class contributes nothing. For the correct class, yiy_i is 1, so the product is the log itself. This step is how the label chooses which class counts.

∑i=1Kyiln⁡(ŷi)\sum_{i=1}^{K} y_i \ln(\hat{y}_i)

Add these products for every class in the list. With a one-hot label, every term except the correct one is 0, so the total is the log of the probability given to the correct class.

L=−∑i=1Kyiln⁡(ŷi)L = -\sum_{i=1}^{K} y_i \ln(\hat{y}_i)

The whole formula in one sentence: the loss equals minus the sum, over all the classes, of the true label times the natural log of the predicted probability, which gives a positive penalty that grows as the probability of the correct answer shrinks.

What the formula is doing, and why

A classifier outputs a probability for each class, such as the probabilities produced by softmax, explained on its own page. The loss asks one question: how much probability did the model give to the answer that was actually right? The loss is then a single number that gets smaller when that probability gets bigger.

Why only the correct class? With a one-hot label, every yiy_i is 0 except the correct class, where it is 1. In the sum, every term with yi=0y_i = 0 drops out, so only one term is left: −ln⁡(ŷc)-\ln(\hat{y}_c), where cc is the correct class. The loss is minus the natural log of the probability given to the correct answer, and the wrong answers do not change it directly.

Why the log? The log is what makes the score behave well. When the probability is close to 1, the log is close to 0, so the loss is close to 0. When the probability is close to 0, the log becomes a large negative number, so the loss becomes large. A confident wrong answer is punished hard, and the penalty grows without limit as the probability heads toward 0. A log has this shape, so a tiny probability for the truth costs far more than a moderate one.

Why the minus sign? A probability between 0 and 1 has a negative log, so without the minus sign the loss would be negative. The minus sign turns the negative logs into positive losses, so a lower loss means a better model. Training pushes the loss down, which pushes the probability of the correct answer up.

An everyday picture

Think of a bet where you must put all your money on one horse. If you put a lot of your money on the horse that wins, you lose little. If you put almost nothing on the winner, you lose heavily. Cross-entropy charges the model according to how little it put on the correct answer: a tiny share costs a lot, and the charge rises faster the smaller that share gets.

Worked example: Cat, dog or bird, with a different correct answer

The softmax example gave one photo the probabilities cat 0.6590.659, dog 0.2420.242 and bird 0.0990.099. We now ask how much the loss is when each animal is the true answer. Each case uses a one-hot label, so only the correct class has y=1y = 1. The loss has no everyday unit beyond the natural-log scale, so it is shown as a plain number.

  1. Start with the case where the photo really is a cat. The label is (1,0,0)(1, 0, 0), so only the cat term survives in the sum. The other two terms are multiplied by 0 and vanish. The loss is L=−ln⁡(0.659)L = -\ln(0.659).
  2. Compute the log for the cat. The natural log of 0.659 is about −0.417-0.417. Because 0.659 is between 0 and 1, its log is negative, and the minus sign in the formula turns it into L=0.417L = 0.417. This is a small loss, since the model gave the right answer a fairly high probability.
  3. Now the photo really is a dog. The label becomes (0,1,0)(0, 1, 0), so only the dog term survives. The loss is L=−ln⁡(0.242)≈1.420L = -\ln(0.242) \approx 1.420. The model gave the dog only 24.2 percent, so the penalty is larger than for the cat.
  4. Now the photo really is a bird. The label is (0,0,1)(0, 0, 1), and the model gave the bird only 9.9 percent. The loss is L=−ln⁡(0.099)≈2.313L = -\ln(0.099) \approx 2.313. The probability is small, so the log is a large negative number, and the minus sign makes the penalty large and positive.
  5. Compare the cat and bird losses. The bird loss divided by the cat loss is 2.3130.417≈5.5\frac{2.313}{0.417} \approx 5.5. The bird probability is only about 6.7 times smaller than the cat probability, 0.659/0.099≈6.70.659 / 0.099 \approx 6.7, but its loss is about 5.5 times bigger. The penalty grows faster than the probability shrinks, which is the point of using a log.

Answer: When the true answer was cat, the loss is a small 0.417, because the model gave cat 65.9 percent. When the true answer was dog, the loss is 1.420. When the true answer was bird, the model had put only 9.9 percent on it, and the loss jumps to 2.313. The less probability the model gave to the truth, the faster the loss rises.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why not use mean squared error for classes?

Cross-entropy matches how probabilities are scored, and it punishes confident wrong answers much harder than squared error does. Mean squared error is built for numbers, and its page explains the details.

What does ln mean?

The natural log, ln\ln, asks which power of ee gives a number. ln⁡(1)=0\ln(1) = 0, and any number between 0 and 1 gives a negative log. That is why the formula has a minus sign.

Is the loss the same as accuracy?

No. Accuracy only asks whether the top answer was right. The loss also counts how confident the model was, so two models with the same accuracy can have different losses.

What is the loss for a yes-or-no question?

It is the two-class form −[yln⁡(ŷ)+(1−y)ln⁡(1−ŷ)]-[y \ln(\hat{y}) + (1 - y)\ln(1 - \hat{y})]. It uses the same idea with two classes instead of many. Here yy is 1 for yes and 0 for no, and ŷ\hat{y} is the probability of yes.

Sources

  1. Cross-entropy, Wikipedia
  2. Loss function, Wikipedia

| All AI & ML formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: ai-ml/cross-entropy-loss.md.