Cross-Entropy Loss Calculator
Compute cross-entropy (log) loss for binary and multi-class classification in your browser. Paste true labels and predicted probabilities or logits to get per-sample loss, the mean log-loss metric, perplexity, and the full step-by-step working — matching log_loss and PyTorch CrossEntropyLoss.
How it works
Cross-entropy loss — also called log loss — measures how far a classifier's predicted probabilities sit from the true labels. It is the negative log-likelihood of the correct class, averaged over the dataset. The definition comes from information theory (Goodfellow, Bengio & Courville, Deep Learning, Ch. 3) and is the loss returned by scikit-learn's log_loss and PyTorch's CrossEntropyLoss.
For binary classification with true label yᵢ ∈ {0, 1} and predicted positive-class probability pᵢ, the per-sample loss is:
Lᵢ = −[ yᵢ·ln(pᵢ) + (1 − yᵢ)·ln(1 − pᵢ) ]
For multi-class classification with K classes and integer true class cᵢ, only the correct class's probability contributes:
Lᵢ = −ln( p(class cᵢ)ᵢ )
- Convert, if needed.When you pass logits, a sigmoid (binary) or a numerically-stable softmax (multi-class, subtract the row-max) maps them to probabilities first — the same pipeline as PyTorch's
BCEWithLogitsLossandCrossEntropyLoss. - Clip. Each probability is clipped to
[eps, 1 − eps]witheps = 1e-15(scikit-learn's default) so a predicted 0 yields a large finite loss instead ofln 0 = −∞. - Score each sample with the formula above to get the per-sample
Lᵢ. - Reduce. Mean —
L = (1/N)·Σ Lᵢ— is the standard log-loss metric (scikit-learn default). Sum givesΣ Lᵢ; None returns the per-sample vector unchanged.
Natural log gives the loss in nats (the ML convention, matching PyTorch); base-2 gives bits, where L_bits = L_nats / ln 2. The related perplexity is exp(mean loss in nats), read as the effective number of equally likely classes the model is still unsure between. As a credibility check, the tool also recomputes the mean by the independent product form −ln((∏ p)^(1/N)) and confirms the two agree to floating-point precision. Every step is plain double-precision arithmetic in your browser — nothing is uploaded.
Worked examples
Frequently asked questions
Sources & references
- scikit-learn — sklearn.metrics.log_loss (log loss definition, eps clipping, mean reduction)
- PyTorch — torch.nn.CrossEntropyLoss (multi-class loss, reduction modes)
- PyTorch — torch.nn.BCELoss (binary cross-entropy per-sample formula)
- Goodfellow, Bengio & Courville — Deep Learning (MIT Press, 2016), Ch. 3 & §5.5
The formulas on this page were last cross-checked against these sources on 2026-06-10. Cross-entropy is a stable mathematical definition, so this tool needs no rate or schedule updates — only the worked examples are periodically re-reconciled.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.