Which distributions does H(p,q) compare?
p and q use the same classes in the same order. ln gives nats.[1]
Surprisal for a class predicted under q is −ln qi. Target distribution p determines how strongly each surprisal contributes to the evaluation. Cross-entropy therefore differs from Shannon entropy H(p), which uses the same distribution in both roles.[1]
What do one-hot targets and zero probabilities mean?
For a hard class label, exactly one term remains.[1]
For pi=0, the contribution is defined as 0, even if qi=0. If pi>0 and qi=0, ideal loss is infinite. The calculator reports this explicitly instead of silently replacing q with a small number. Library clipping is a separate implementation choice.[1][2]
Changing base changes unit and numerical value, not the ordering of the same losses.[1]
Confident wrong predictions cost more
Free with a Quanta account. Your download stays ready after registration.
Two fully worked examples
One one-hot target with two predictions
- Given
- Target p=(1,0,0). Compare qA=(0.8,0.1,0.1) and qB=(0.1,0.45,0.45). All vectors are normalized.
- Find
- Find both natural-log losses.
The only positive target weight selects class 1.
Lower probability for the true class increases loss.
Result: qA has lower loss: about 0.223 rather than 2.303 nats.
Check: e^(−0.22314355)≈0.8 and e^(−2.30258509)≈0.1. Zero weights provide no additional contributions.
Weight a soft target distribution completely
- Given
- p=(0.5,0.3,0.2) and q=(0.6,0.3,0.1), with identical class order.
- Find
- Calculate H(p,q) and compare H(p,p).
Use all three positive target weights.
Sum contributions without dividing again by three.
For comparison, replace q with p.
Result: Prediction q costs about 1.07712 nats; target entropy is about 1.02965 nats.
Check: Both distributions sum to 1. For this target, q=p gives the smallest ideal cross-entropy. The gap is about 0.047469 nats.
Recall the method later with the formula’s flashcards.
10 flashcards · English materials
Already have an account? Sign in for flashcardsAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Five solved exercises with different learning goals
Calculate · Foundation
Target p=(0,1,0), prediction q=(0.10,0.75,0.15). Calculate cross-entropy in nats.
Show hint
Define the quantities first and apply the stated model.
Learning goal: Evaluate one-hot cross-entropy.
Show solution and calculation
Only the true class contributes: H=−ln(0.75)≈0.287682 nats.
The two zero weights contribute zero, not extra logarithmic terms.
Rearrange · Application
A single one-hot target has loss L=ln 4 nats. What probability did the model assign to the true class?
Show hint
Define the quantities first and apply the stated model.
Learning goal: Distinguish a special inverse from a nonunique full inversion.
Show solution and calculation
L=−ln q gives q=e^(−L)=e^(−ln 4)=1/4.
The remaining 0.75 can be allocated differently among other classes; the full q distribution is not uniquely determined.
Interpret · Application
For true class 1, qA=(0.60,0.25,0.15) and qB=(0.90,0.06,0.04) both classify correctly. Are their losses equal?
Show hint
Define the quantities first and apply the stated model.
Learning goal: Separate accuracy from probability loss.
Show solution and calculation
No.
LA=−ln 0.60≈0.510826 nats and LB=−ln 0.90≈0.105361 nats.
Equal top-1 accuracy does not imply equal probabilistic loss.
Error analysis · Application
For p=(1,0,0), q=(0.8,0.1,0.1), −log20.8≈0.321928 is labelled nats. Correct it.
Show hint
Define the quantities first and apply the stated model.
Learning goal: Check logarithm base and information unit.
Show solution and calculation
The number is in bits.
Using ln gives−ln 0.8≈0.223144 nats.
Conversion:0.321928 bits×ln 2≈0.223144 nats.
Prediction did not change; the unit was confused.
Transfer · Application
Assess p=(0,1,0) and q=(1,0,0). May ideal loss be reported as 0 or a finite rounded number?
Show hint
Define the quantities first and apply the stated model.
Learning goal: Distinguish a model zero from numerical clipping.
Show solution and calculation
No.
In the true class p2>0 and q2=0, so H=+∞.
Zero weights in other classes still contribute zero.
A finite value after clipping describes a modified procedure and must be labelled accordingly.
Check your understanding with the formula’s reference test.
7 test questions · English materials
Already have an account? Sign in for the testAvailable from Essential. A ready-made formula set, no AI credits required. Article exercises are not all included.
Limits, measurements and calculator
Across observations, losses can be summed or averaged. Averaging observations is not the same as dividing the class sum again by K. This calculator evaluates one pair of three-class distributions and performs no model training.[2]
Entered distributions are not automatically adjusted. Sum validation permits only a declared tolerance of 10−12 for rounding residues. The softmax function produces mathematically positive outputs for finite logits; a computational zero from underflow must therefore be distinguished from an explicitly entered model zero. The binary log loss is the two-class special case; this calculator evaluates three classes.[1]
Sources for this explanation
Quanta · Updated November 9, 2026
- Dive into Deep Learning §4.1.2.1–2, §4.1.3.1–3: log likelihood, soft labels, cross-entropy and units
- scikit-learn metrics.log_loss: definition; normalize; clipping; Notes (natural logarithm)
Editorial responsibility: Quanta Study editorial team
Review scope
This explanation, 2 worked examples and 5 exercises are available in German and English. Structure, source references and mathematical notation undergo automated checks. Calculations, model assumptions and both language versions receive a separate AI review.
An independent, named human subject review has not yet been documented. Our exercise numbers are worked examples, rather than measurements published by the cited sources.
Editorial responsibility, sources and methodsChange history
- Developed general three-class cross-entropy with soft targets, logarithm units, exact zero cases, two examples and five exercises.
Cite this reference page
Quanta. Cross-entropy (classification loss). Updated November 9, 2026. https://quanta-study.de/en/formula-sheet/kreuzentropie
When citing a specific scientific statement, also cite the specialist source linked to that statement.