Mathematical Insights into Multi-Class Cross-Entropy Loss

Freedom Preetham
Mathematical Musings
3 min readApr 26, 2024

The multi-class cross-entropy loss is an extension of the concept of entropy in information theory, applied to the context of machine learning classification tasks. Its formulation is deeply rooted in probability theory and statistical mechanics, providing a robust metric for evaluating the performance of classification models. Let’s dive deeper into the mathematical underpinnings and derivation of this loss function, along with a thorough analysis of its components.

Asked ChatGPT for an Image for this blog and this what it came up with 🤷‍♂️

Information Theory and Entropy

The genesis of the cross-entropy loss lies in information theory, where entropy quantifies the amount of uncertainty involved in predicting the value of a random variable.

The Shannon entropy for a discrete random variable X with possible values {x1​,x2​,…,xn​} and probability mass function p(x) is defined as:

In the context of classification, where each class label can be seen as a random event, entropy measures the unpredictability of the true class labels.

Kullback-Leibler Divergence

To connect entropy with machine learning, we introduce the Kullback-Leibler divergence (KL divergence), a measure of how one probability distribution diverges from a second, expected probability distribution. For discrete probability distributions P and Q over the same underlying set of events, the KL divergence is given by:

KL divergence is non-negative and quantifies the amount of lost information when Q is used to approximate P.

Derivation of Cross-Entropy

Cross-entropy can be derived from the KL divergence by expanding the terms related to the true probability distribution P and the predicted probability distribution Q:

For multi-class classification, where P is the true distribution (one-hot encoded) and Q is the predicted distribution, the cross-entropy simplifies to:

Here, yc​ is 1 for the actual class and 0 for others, simplifying the sum to the negative log-likelihood of the correct class.

Mathematical Components Analyzed

One-Hot Encoding, yc​:

  • Mathematical Characterization: Acts as an indicator function, isolating the term corresponding to the actual class in the loss computation.
  • Information Theoretic Role: Ensures focus solely on the probability assigned to the true class, reflecting the typical setup in information retrieval where only the relevant item’s score matters.

Predicted Probability, y^​c​:

  • Statistical Role: Represents the model’s estimation of the likelihood of each class being the correct classification.
  • Behavior in the Loss Function: The function log(y^​c​) increases monotonically, making the loss more sensitive to changes in small probabilities (i.e., low predicted probabilities for the correct class yield higher losses).

Logarithmic Function, log:

  • Mathematical Insight: Converting probabilities to a logarithmic scale penalizes wrong classifications more severely, especially when the model is confident (incorrectly assigning high probabilities to wrong classes). It deals with the exponential nature of information growth, anchoring it back to a linear scale which is crucial for learning stability.

Summary

The multi-class cross-entropy loss not only encapsulates the essence of how confident a model is about its predictions but also mathematically incentivizes the reduction of uncertainty in these predictions. Its formulation is an elegant translation of theoretical principles from information theory into practical, actionable insights in machine learning.

This deeper look into the mathematics behind multi-class cross-entropy loss reveals its fundamental role in guiding the training of classification models towards more accurate and confident predictions. We invite further discussion on this topic to explore even more nuanced aspects of this essential machine learning component.

--

--