Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Cross-Entropy Loss: Theory and Information-Theoretic Interpretation

Welcome to the final lesson of our module on the Core Concepts of Supervised Learning. In our last session, we conducted a thorough derivation of the logistic regression model, starting from the principle of Maximum Likelihood Estimation (MLE). This led us to the log-likelihood function, which we sought to maximize:

We noted that maximizing this function is equivalent to minimizing its negative, a standard practice in optimization. Today, we will formally define this negative log-likelihood as the cross-entropy loss function. Our primary objective is to move beyond this purely optimization-based view and explain its deeper information-theoretic interpretation. This will provide a robust theoretical justification for why cross-entropy is the de facto standard loss function for classification tasks, connecting probability, optimization, and information theory.

From Log-Likelihood to Cross-Entropy Loss

In machine learning, the convention is to frame learning as a process of minimizing a loss or cost function. Therefore, instead of maximizing the log-likelihood, we minimize its negative. For a dataset of samples, the average negative log-likelihood gives us the binary cross-entropy loss function:

Let's analyze the contribution of a single data point to this loss. Let be the model's predicted probability for the positive class.

  • If the true label is , the loss is .
  • If the true label is , the loss is .

This behavior is visualized below.

This plot shows the binary cross-entropy loss for a single observation. The blue curve represents the loss when the true label is 1, and the red curve shows the loss when the true label is 0. Notice how the loss approaches infinity as the model's predicted probability \(\hat{y}\) moves further from the true label.

As the plot illustrates, the loss penalizes confident, incorrect predictions exponentially. If the true class is 1 () but the model predicts a probability close to 0 (), the loss skyrockets. Conversely, a correct and confident prediction () results in a loss close to zero. This intuitive behavior is desirable, but the true power and elegance of cross-entropy come from its roots in information theory.

An Information-Theoretic Perspective

To understand why this specific functional form is so fundamental, we need to take a detour into information theory, the mathematical study of the quantification, storage, and communication of information.

The following article from Eli Bendersky's website provides an excellent, bottom-up introduction to the key concepts we need. We will work through it step by step.

Cross-entropy and KL divergence - Eli Bendersky's website

This article provides a clear, mathematical walkthrough of the core ideas of information theory that underpin the cross-entropy loss function.

First, read the section on information content. This introduces the concept of "surprise" and its quantification in "bits". Next, read the section on Entropy, which defines it as the expected surprise of a distribution. Finally, read the section that defines Cross-entropy itself, interpreting it as the average number of bits needed for encoding when using a suboptimal model.

Let's summarize the key ideas you've just read about:

  1. Information Content (Surprise): The information gained from observing an event with probability is . Unlikely events () are highly surprising and carry more information.

  2. Entropy: The entropy of a probability distribution , denoted , is the expected surprise. It's the average amount of information we get from one sample of the distribution. For a discrete distribution, it's calculated as:

    Entropy can be interpreted as the minimum average number of bits required to encode a message drawn from distribution .

  3. Cross-Entropy: This is the crucial concept. Suppose the true data distribution is , but we devise an encoding scheme based on an incorrect, estimated distribution . The average number of bits we will need to encode messages from using the code for is the cross-entropy, :

    It is the expected surprise when events are drawn from , but we evaluate their surprise using the probabilities from .

The following short video from Aurélien Géron provides a brilliant analogy for these concepts using weather forecasts, which can help solidify your intuition.

A Short Introduction to Entropy, Cross-Entropy and KL-Divergence

This video offers an intuitive explanation of entropy and cross-entropy using the idea of designing an efficient code for transmitting weather information.

Watch from the beginning until the introduction of KL Divergence. Pay close attention to how cross-entropy is framed as the "average message length" when using a code that makes implicit assumptions about the data's probability distribution.

The Connection to Machine Learning and KL Divergence

Now, let's tie this back to our classification problem.

  • The true distribution is represented by the ground-truth labels. For a multi-class problem, a specific data point's true label is a one-hot vector, like [0, 1, 0, 0]. This is a distribution with no uncertainty—its entropy is zero.
  • The predicted distribution is the output of our model, typically from a softmax layer, like [0.1, 0.7, 0.15, 0.05].

The cross-entropy loss function is precisely ! It measures the average number of bits needed to encode the true labels when we use our model's predictions as our encoding scheme. We want to adjust our model parameters to make as close to as possible, thereby minimizing this "average message length."

This brings us to a related concept: Kullback-Leibler (KL) Divergence. The cross-entropy is the cost of using code , while the entropy is the cost of the optimal code. The difference between them is the "extra" cost, or wasted bits, due to using the suboptimal code . This extra cost is the KL Divergence.

Since , this gives us the important inequality: . The cross-entropy is always greater than or equal to the true entropy, with equality holding if and only if .

Let's continue with the resources to formalize this.

Cross-entropy and KL divergence - Eli Bendersky's website

This part of the article formalizes KL Divergence and connects all the concepts back to Maximum Likelihood Estimation.

Read the section on KL divergence to see how it is defined as the difference between cross-entropy and entropy. Then, read the next section which makes the crucial point that for training, minimizing cross-entropy is equivalent to minimizing KL divergence. Finally, the section on the Relation to MLE completes the theoretical loop, showing that minimizing cross-entropy is equivalent to maximizing the log-likelihood you saw in the previous lesson.

The key insight for machine learning is that during training, the true data distribution is fixed. Therefore, its entropy is a constant. As a result:

Minimizing with respect to model parameters is equivalent to minimizing .

This provides a powerful justification: training a classifier with cross-entropy loss is equivalent to minimizing the "distance" (in an information-theoretic sense, as KL-divergence is not a true metric) between the predicted probability distribution and the true one.

The following video provides another excellent summary of these relationships.

Fantastic KL Divergence and How to (Actually) Compute It

This video offers a concise, visual summary of the connections between surprise, entropy, cross-entropy, and KL divergence, and how they apply directly to classification loss.

Watch the segments that recap the definitions of surprise, entropy, and cross-entropy. Then, focus on the definition of KL divergence as the "extra bits wasted". Finally, see how it's applied in the classification context.

Binary vs. Categorical Cross-Entropy

Let's explicitly write out the cross-entropy for the two main types of classification.

1. Binary Cross-Entropy:
As we saw, for a single data point, the true distribution is and the predicted distribution is . The cross-entropy is:

This is exactly one term in the loss function for logistic regression.

2. Categorical Cross-Entropy:
For multi-class classification with classes, the output of the model is typically a vector of probabilities from a Softmax function. The true label is a one-hot vector . The loss for a single sample is:

Since is one-hot, only the term corresponding to the true class is non-zero. For example, if the true class is , then and all other , so the loss simplifies to .

This entire process for multi-class classification is depicted below.

This diagram shows a typical multi-class classification setup. The raw outputs from a model (logits), denoted by \(s\), are passed through a Softmax function to produce a probability distribution \(f(s)\). This distribution is then compared to the true, one-hot target distribution \(t\) using the Categorical Cross-Entropy (CE) loss.

For a rigorous walkthrough of the generalization from binary to multi-class cross-entropy and its equivalence to maximizing the multinoulli log-likelihood, the following resource is invaluable.

22.11. Information Theory

This resource provides a comprehensive mathematical treatment of cross-entropy and its role as an objective function.

Read the section on Cross-Entropy, particularly the subsection "Cross-Entropy as An Objective Function of Multi-class Classification". This formally shows how minimizing the categorical cross-entropy loss is mathematically equivalent to maximizing the log-likelihood for a multi-class problem.

Conclusion

In this lesson, we established a deep, multi-faceted understanding of the cross-entropy loss function. It is not merely an arbitrary choice that happens to work well; it is a principled objective derived from both probability theory and information theory.

Here are the key takeaways:

  • Probabilistic View: Minimizing the cross-entropy loss is equivalent to Maximum Likelihood Estimation, i.e., finding the model parameters that make the observed data most probable.
  • Information-Theoretic View: Cross-entropy measures the average number of bits required to encode the true data distribution using the model's predicted distribution. Minimizing it means making our model's predictions a more efficient code for the reality.
  • Equivalence to KL Divergence: Since the entropy of the true data is constant, minimizing cross-entropy is equivalent to minimizing the KL divergence between the predicted and true probability distributions.
  • Binary vs. Categorical: The familiar loss function from logistic regression is binary cross-entropy. For multi-class problems, we use categorical cross-entropy, which compares the model's softmax output to the one-hot encoded true label.

You have now completed the core theoretical foundations of supervised learning, from linear and logistic regression models to the loss functions that guide their training. In the next module, Optimization and Model Fitting, we will begin by formally comparing the loss functions we've seen (Mean Squared Error and Cross-Entropy). We will then dive deep into the primary algorithm used to minimize them: gradient descent.

Can't find a good explanation? Sign up and we'll make it for you

Sign up