Log-Loss (Cross-Entropy Loss)

Machine Learning
Introduced by
Information theory and machine learning literature
Year
1990s
Type
Loss function
Family
Probabilistic Loss Metric

What does it do?

Log-loss (logarithmic loss, also called cross-entropy loss) scores a probabilistic classifier by the probability it assigns to each sample's true label, averaged over all samples.

Because it uses the full predicted probability rather than a thresholded class, it reflects both how often the model is right and how well its probabilities are scaled. The same expression, in its multiclass softmax form, is also used as the training objective for models such as logistic regression and neural networks.

The intuition behind the method

Log-loss measures how well a classifier's predicted probabilities match the true labels. If a classifier gives a positive sample a probability of 0.9 and the sample is positive, the loss contribution is small. If it gives that same sample 0.1, the contribution is large. The penalty grows without bound as the probability assigned to the true label approaches zero, so a confident error costs far more than a hedged one. Lower values are better, the opposite direction from accuracy.

History

Log-loss, also called cross-entropy loss, draws on information theory and probability theory, and its expression is the negative average log-likelihood of the observed labels. No single originator is named here, and no firm date is given for its adoption. Bishop (1995) discusses error functions for neural networks from a statistical pattern recognition perspective, which is the setting in which such losses are motivated.

When to use it

Choose log-loss when the model outputs probabilities and the quality of those probabilities matters, not only the final class label. It fits tasks where predicted probabilities feed later decisions, such as threshold selection or cost-sensitive classification, and it is a natural choice when the same objective is used for training and evaluation. Do not use it when the model outputs only class labels, because the formula requires probabilities. Avoid relying on it to compare results across datasets with different class balances. If only the correct or incorrect final decision matters, accuracy or F1-score answers the question more directly.

Limitations and considerations

  • The value is not a proportion, so a particular log-loss value has no direct reading as a fraction of correct decisions, unlike accuracy or F1-score.
  • Sensitive to class imbalance: the average is driven by the samples with the largest losses, which may be concentrated in the rarer class.
  • A small number of very confident wrong predictions can dominate the average, so the metric can be unstable on small samples.
  • Requires predicted probabilities for every sample; class labels alone cannot be scored.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Scores the predicted probability itself rather than a thresholded label, so two models with the same accuracy can be separated by how their probabilities are distributed.
  • The penalty grows without bound as the probability on the true label approaches 0, so confident errors are costly and extreme outputs are discouraged. This does not by itself guarantee calibrated probabilities.
  • The expression is differentiable in each p_i away from 0 and 1, so it can serve directly as a training objective, as in logistic regression and neural networks.

Applications

  • Evaluating predicted risk in medical diagnosis, where the reported probabilities themselves must match observed outcomes.
  • Scoring probabilistic binary classifiers before their outputs are thresholded for a downstream decision.
  • Serving as the training objective for logistic regression and neural network classifiers.
  • Comparing candidate probability models on the same held-out labelled dataset.

How it works

How it works

1

For each sample i, obtain the predicted probability p_i that the sample belongs to the positive class. Let y_i be the actual label, coded 1 for positive and 0 for negative. The logarithms are finite only when each p_i lies strictly between 0 and 1.

Formula
p_i \in (0,1),\quad y_i \in \{0,1\}
2

Compute logloss = -1/N * sum(y_i * log(p_i) + (1-y_i) * log(1-p_i)) over all N samples. For each sample only one of the two terms is active: the positive-class term when y_i is 1, and the negative-class term when y_i is 0. The result is the negative average of the log-probabilities assigned to the true labels. For K mutually exclusive classes with one-hot labels y_i and softmax output probabilities o_i, the cross-entropy loss for one sample is L = -sum_i y_i log(o_i), and the reported value is its average over the N samples. The binary formula above is the two-class special case; the formula as written applies to binary probabilistic classifiers.

Formula
\text{logloss} = -\frac{1}{N}\sum_{i=1}^{N}\left[y_i\log(p_i) + (1-y_i)\log(1-p_i)\right]
3

Log-loss is zero only when every predicted probability is exactly 1 for positive samples and exactly 0 for negative samples, and it has no upper bound. Values closer to zero mean the predicted probabilities place more weight on the true labels. A single sample with p_i close to 0 for a positive label can raise the average substantially, so a few extreme wrong predictions can dominate the result. To judge a value, compare it with a baseline: a constant predictor that always outputs the positive-class base rate. A useful model should score lower than this baseline on the same data.

Common pitfalls

  • Treating a single log-loss value as evidence of good calibration. Log-loss mixes how well probabilities separate the classes with how well they match observed frequencies, so a model with good ranking but a few extreme errors can score poorly. Inspect a calibration curve alongside it.
  • Comparing log-loss across datasets with different class balances. The loss of a constant predictor that outputs the class base rate changes with that rate, so values are comparable only within one dataset.
  • Predicting exactly 0 or 1 for a sample whose label turns out to be the opposite. The logarithm is undefined at those values and the loss becomes infinite. Implementations commonly clip probabilities away from 0 and 1, and the clipping level changes the reported result.

Frequently asked questions

How does log-loss differ from accuracy?

Accuracy only checks whether the final decision, after thresholding at 0.5, is correct. Log-loss uses the probability assigned to the true label, so a model can have high accuracy and still high log-loss if its probabilities are poorly scaled. Log-loss therefore gives finer information about probability quality.

Why penalize confident wrong predictions more heavily?

A confident wrong prediction assigns very little probability to the true label, and the loss grows without bound as that probability approaches zero. Downstream decisions that use the probability are misled most by such errors, so the metric pushes models to express uncertainty when the evidence is weak. The metric does not reward uniform hedging either, because the loss at intermediate probabilities is still positive.

Can I directly compare log-loss values across different datasets?

No. The loss of a constant predictor that outputs the class base rate depends on the class balance, so a value that is good on one dataset may be poor on another. Compare log-loss within the same dataset, or report it relative to that constant-predictor baseline computed on the same data.

Related methods

Sources

  1. Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.

    Open publication reference
  2. Bishop, C. M. (1995). Neural Networks for Pattern Recognition. Oxford University Press.

    Open DOI reference