Cross Entropy Calculator
Compute cross-entropy between true probabilities and predicted probabilities. This educational calculator shows the formula, result, and step-by-step interpretation.
Calculator
What this calculator teaches
Cross entropy is a common loss function in classification and information theory.
Use the result as a learning aid. For classwork, still show the formula and intermediate reasoning so the final answer is not just a black-box number.
Cross Entropy as a Penalty for Predictive Surprise
Cross entropy measures how costly predictions q are when outcomes follow a target distribution p. For discrete classes and base-2 logarithms, H(p,q) = −Σpilog2(qi). Each true-probability weight pi multiplies the information cost of assigning probability qi to that class. Predictions that place low probability on events that actually matter under p receive a large penalty.
When p is one-hot, only the true class contributes, so cross entropy becomes −log2(qtrue). Predicting the true class with probability 0.5 costs 1 bit, while probability 0.25 costs 2 bits. For a general distribution, cross entropy decomposes as H(p,q) = H(p) + DKL(p||q), so it is minimized at q = p when p is fixed.
| Quantity | Meaning | Requirement |
|---|---|---|
| pi | Target probability for class i | pi ≥ 0 and totals 1 for a distribution |
| qi | Predicted probability for class i | qi > 0 where pi > 0 |
| −log2(qi) | Information cost in bits | Grows as assigned probability falls |
The logarithm base controls the unit: base 2 gives bits and natural logarithms give nats. The ranking of predictions is unaffected by changing the base because all values are multiplied by a positive constant.
Formula & Symbols
| Concept | Formula or rule |
|---|---|
| Cross entropy | H(p,q) = -Σ pᵢ log₂(qᵢ) |
Worked example
Common mistakes
Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.
Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.
Cross entropy is defined on probability distributions. Classification logits must be converted to probabilities, commonly with softmax, before applying this probability-form expression.
Entropy H(p) uses p inside the logarithm. Cross entropy H(p,q) uses q inside the logarithm and measures the cost of representing p using predictions q.
FAQ
Related calculators
These links will work after the calculators are registered in the final Math layout update.
Formula Explorer connections
Interpretation: This relationship quantifies information, representation, storage, error, search or computational performance. Assumption: Use the exact encoding, data distribution, machine representation and algorithm assumptions. Real systems also include implementation and hardware overhead.