Data Science & ML
Advanced

Categorical Cross-Entropy

Loss for multi-class classification with one-hot labels.

Formula

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

Variables

y_iTrue (one-hot)
\hat{y}_iPredicted prob
KClasses

Example

True class prob 0.7: loss = -log(0.7) = 0.357

How it works

Categorical cross-entropy measures how well a predicted probability distribution matches the true class label when exactly one class is correct. With one-hot labels, every term except the correct class is zero, so the loss reduces to the negative log of the model’s assigned probability to the true class; this strongly penalizes confident wrong answers and rewards confident correct ones. It comes directly from the negative log-likelihood of a multinomial model, which is why it is the standard objective for probabilistic multi-class classifiers.

When to use it

Use this for multi-class classification problems where each example belongs to one and only one class, such as digit recognition, topic classification, or image labeling. It is especially appropriate when your model outputs softmax probabilities and you want training to reflect calibrated confidence, not just hard class choices.

Worked example, step by step

Suppose there are 4 classes and the true label is class 3, so y = [0, 0, 1, 0]. The model predicts probabilities ŷ = [0.10, 0.20, 0.60, 0.10]. Apply L = -Σ y_i log(ŷ_i). Only the true class contributes: L = -(0·log 0.10 + 0·log 0.20 + 1·log 0.60 + 0·log 0.10). So L = -log(0.60). Using the natural log, L ≈ 0.5108. Final answer: categorical cross-entropy loss = 0.5108.

Common mistakes to avoid

  • Using probabilities that do not sum to 1, instead of softmax outputs
  • Taking the log of the predicted class label rather than the predicted probability for the true class
  • Forgetting that the loss is averaged or summed over a batch depending on the implementation

Did You Know?

Paired with softmax outputs in most neural classifiers.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026