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

MLE for Cross-Entropy and MSE Loss Functions

Welcome to the final lesson of our foundational module on neural networks. In our previous discussions, we established that neural networks are compositions of affine transformations and non-linearities, and we proved, via the Universal Approximation Theorem, that even a simple network is theoretically capable of representing any continuous function.

This is a powerful guarantee of expressivity, but it doesn't tell us how to find the specific network parameters that solve a given problem. To do that, we need to transition from theory to practice—we need a way to train the network. Training is an optimization problem, and at the heart of any optimization problem is an objective function—a way to measure how well our model is currently performing. In machine learning, we call this the loss function.

Today, we will derive the two most important loss functions in all of machine learning—Mean Squared Error (MSE) for regression and Cross-Entropy for classification. Instead of just presenting them as formulas to be memorized, we will derive them from a single, powerful statistical principle: Maximum Likelihood Estimation (MLE). This will provide a deep, probabilistic justification for why these specific functions are so fundamental to training neural networks and other machine learning models.

The Principle of Maximum Likelihood Estimation (MLE)

Before we can derive our loss functions, we must first understand the principle they are built upon. Maximum Likelihood Estimation is a general method for estimating the parameters of a statistical model. The core idea is intuitive: we should choose the parameters that make our observed data the most probable.

Imagine you have a set of data points, and you are trying to find a probability distribution that best describes them. You could try various distributions, each defined by a set of parameters (like the mean and variance of a Gaussian). For each candidate distribution, you can calculate the probability, or "likelihood," of having observed your specific data. MLE simply states that the best set of parameters is the one that maximizes this likelihood.

This plot provides a visual intuition for MLE. The bottom panel shows a histogram of observed data points. Several candidate probability density functions (the curves) are overlaid. For each candidate, the top panel shows its corresponding likelihood value. MLE selects the parameters of the green curve because it maximizes the likelihood of the observed data.

This idea is formalized in the likelihood function.

10-315 Notes Maximum Likelihood Estimation

These lecture notes from Carnegie Mellon University provide a rigorous introduction to MLE. We'll use them to define the core concepts.

First, read the definition of the likelihood function in Section 2. Focus on Equation 1 and the explanation that follows. Notice how the i.i.d. assumption allows us to express the likelihood of the entire dataset, p(D|\theta), as a product of individual probabilities. Next, continue reading into Section 3, "Maximum Likelihood Estimation". Pay close attention to the concepts of the likelihood function (note the change in notation to emphasize it's a function of \theta), the log-likelihood, and the negative log-likelihood. The text clearly explains the two critical reasons for working with logarithms: avoiding numerical underflow and simplifying calculus by turning products into sums. Finally, read the general recipe for performing MLE in Section 3.4, "Optimization for MLE." This sequence—formulate likelihood, take the negative log, and minimize—is the exact blueprint for how we train neural networks.

To summarize the key points:

  1. Likelihood Function: Given a dataset and a model parameterized by , the likelihood, assuming data is independent and identically distributed (i.i.d.), is:
  2. Log-Likelihood: To make optimization tractable, we work with the log-likelihood:
  3. The Goal of MLE: We want to find the parameters that maximize this function:
  4. Connection to Minimization: Since standard optimization algorithms like gradient descent are framed as minimization problems, we typically minimize the negative log-likelihood (NLL), which is an equivalent problem:

This final expression, , is our loss function. The process of minimizing the NLL is the process of training our model.

Deriving Mean Squared Error (MSE) for Regression

Let's apply this principle to a regression problem. Our goal is to train a neural network, , to predict a continuous target variable .

The crucial step in MLE is to make a probabilistic assumption about how the data is generated. For regression, the standard assumption is that the target is not perfectly determined by the input , but includes some zero-mean Gaussian noise. In other words, we assume the target is drawn from a Normal distribution whose mean is the network's output and whose variance is some fixed .

Assumption:

The probability density function for a single data point is therefore:

Before we derive the loss function from this, let's watch a video that demonstrates the general procedure for finding the MLE of a Gaussian distribution's parameters. This will solidify the mechanics before we apply them to our specific neural network case.

L20.10 Maximum Likelihood Estimation Examples

This MIT OpenCourseWare video walks through the MLE derivation for the parameters of a Normal distribution.

Please watch the second example in the video, which begins at the start of the derivation for the Normal distribution. Focus on the strategy: Write the joint PDF (the likelihood function). Take the negative logarithm to get the objective function. Take partial derivatives with respect to the parameters (\mu and v) and set them to zero to find the optimal values.

Now, let's apply this exact same logic to our regression problem. We form the log-likelihood for the entire dataset :

Using the properties of logarithms, we can simplify this expression:

Our goal is to find the network parameters that maximize this log-likelihood. Notice that the first term, , does not depend on . Therefore, to maximize with respect to , we only need to maximize the second term.

Maximizing is equivalent to minimizing its negation:

Since is a positive constant with respect to , we can drop it from the optimization. We are left with:

This is exactly the Sum of Squared Errors. If we average it over the dataset by dividing by , we arrive at the familiar Mean Squared Error (MSE) loss function. Thus, we have shown that minimizing MSE is equivalent to performing Maximum Likelihood Estimation under the assumption of Gaussian noise.

Deriving Cross-Entropy for Classification

Now, let's turn to classification. Here, our network's output is a probability (or a vector of probabilities for multi-class problems), and our true label is a class index.

The key assumption for classification is that the true label is drawn from a discrete probability distribution whose parameters are given by the network's output.

  • For binary classification (), we assume follows a Bernoulli distribution.
  • For multi-class classification ( is a one-hot vector), we assume follows a Categorical distribution.

The CMU notes you read earlier have a fantastic section that performs this derivation explicitly for logistic regression. Since a neural network's final layer for classification is typically a logistic (sigmoid) or softmax function, this derivation applies directly.

10-315 Notes Maximum Likelihood Estimation

This section connects all the dots, deriving cross-entropy loss from Maximum Conditional Likelihood Estimation.

Please read Section 4, "MLE for Logistic Regression", in its entirety. This is the most important reading for this lesson. First, understand the distinction made in 4.1 between regular MLE and Maximum Conditional Likelihood Estimation (MCLE). When we model p(y|x) for supervised learning, we are technically performing MCLE, though in practice it is almost always just called MLE. Next, carefully follow the derivation in Section 4.2.1 for binary logistic regression. It starts by defining the model's output as the parameter of a Bernoulli distribution. It writes out the conditional likelihood function L(\theta; D) for the whole dataset. It computes the log-likelihood \ell(\theta; D). Finally, it shows that minimizing the negative log-likelihood, \arg\min_{\theta} -\ell(\theta; D), yields an expression that is identical to the binary cross-entropy loss function. Then, follow the parallel derivation in Section 4.2.2 for multi-class logistic regression. The logic is the same, but using the Categorical distribution and softmax outputs, and it culminates in the multi-class cross-entropy loss.

Let's recap the results from that reading.

Binary Cross-Entropy:

  • Assumption: , where is the network's output probability for class 1.
  • Likelihood (one point):
  • Negative Log-Likelihood (Loss): This is precisely the binary cross-entropy loss.

Categorical Cross-Entropy:

  • Assumption: , where is the vector of probabilities from the network's softmax output.
  • Likelihood (one point):
  • Negative Log-Likelihood (Loss): This is the categorical cross-entropy loss.

The term "cross-entropy" itself comes from information theory, where it measures the difference between two probability distributions (the true distribution defined by and the predicted distribution defined by ). Our derivation shows that minimizing this information-theoretic quantity is equivalent to maximizing the likelihood of the observed data.

Conclusion

In this lesson, we have forged a critical link between the abstract principles of statistics and the practical machinery of training neural networks. We've moved beyond simply stating loss functions and have derived them from first principles.

Key Takeaways:

  • Maximum Likelihood Estimation (MLE) provides a foundational principle for deriving objective functions. The goal is to find model parameters that maximize the probability of the observed data.
  • In practice, we minimize the negative log-likelihood (NLL), which is numerically stable, mathematically convenient, and aligns with standard gradient-based optimization.
  • Minimizing Mean Squared Error (MSE) in regression is equivalent to performing MLE under the assumption that the data contains Gaussian noise.
  • Minimizing Cross-Entropy in classification is equivalent to performing MLE under the assumption that the class labels are generated from a Bernoulli or Categorical distribution parameterized by the network's outputs.

This concludes our first module. We now have all the conceptual components of a feedforward neural network: we know its architecture (Lesson 3), we know it is a powerful approximator (Lesson 6), and we now have a principled way to define the objective function we want to optimize (this lesson).

The only remaining question is how to perform this optimization. How do we efficiently calculate the gradients of this loss function with respect to the millions of parameters in a deep network? The answer lies in the elegant algorithm that powers all of modern deep learning: backpropagation. This will be the subject of our first lesson in the next module.

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

Sign up