Zero Labs
Industry News / AI

From Probability to Loss - How MLE Builds Machine Learning: Part 2

Source · Towards AI By Manish Patil

From the publisher’s feed, collected in the ZeroLabs newsroom.

A quick recap of where we left offIn Part 1, we followed a single thread of logic from start to finish.Read: From Probability to Loss — How MLE Builds Machine Learning: Part 1We started with a question: Given data, how do we find parameters that make sense of it? We introduced likelihood as the right framing for that question: instead of asking what data a known distribution produces, we ask what distribution best explains the data we already have. We then maximized that likelihood, took the log to keep the math stable, flipped the sign to get something an optimizer can minimize, and arrived at the Negative Log-Likelihood, a loss function, derived from first principles.We then plugged in the Gaussian distribution and watched Mean Squared Error fall out of the algebra. Not invented. Derived.In Part 2, we’re going to push further. We’ll derive cross-entropy the same way. We’ll visit Poisson and see what its loss looks like. And then we’ll zoom out and ask the bigger question: how does all of this actually work inside a neural network? What happens during training, really? And what can go wrong?Section 1: Cross-entropy: the classification loss, derivedIn Part 1 we assumed data was Gaussian, continuous, symmetric, bell-shaped. That gave us MSE.Now switch contexts. You’re doing classification. Your model is predicting which category something belongs to cat or dog, spam or not spam, one of ten handwritten digits. The output isn’t a continuous number. It’s a probability distribution over a set of discrete classes.The right distribution for this is the categorical distribution, a generalization of the Bernoulli distribution from Part 1 that handles more than two classes.For binary classification, it’s simply Bernoulli:P(y | x, θ) = ŷʸ (1 − ŷ)¹⁻ʸWhere ŷ is your model’s predicted probability of class 1, and y is the true label (0 or 1).Now take the log-likelihood:log L = Σ [ y × log(ŷ) + (1−y) × log(1−ŷ) ]Flip the sign to get the NLL:Loss = − Σ [ y × log(ŷ) + (1−y) × log(1−ŷ) ]That’s binary cross-entropy. The standard loss for every binary classification problem you’ll ever work on. Derived from the Bernoulli assumption and MLE, the exact same process that gave us MSE.For multi-class classification, the same logic extends to the categorical distribution:Loss = − Σ yᵢ × log(ŷᵢ)Where the sum is over all classes, yᵢ is 1 for the correct class and 0 for everything else, and ŷᵢ is your model’s predicted probability for class i.This is categorical cross-entropy, what you’re using every time you train a classifier with a softmax output.And remember from Part 1: minimizing this cross-entropy loss is equivalent to minimizing KL divergence between your model’s predicted distribution and the true data distribution. You’re not just reducing a number. You’re closing the gap between what your model believes and what the data actually says.Section 2: The Poisson assumption: when your data is countsSo far we’ve covered two assumptions: Gaussian for continuous data, Bernoulli/categorical for class labels. But real-world data comes in many shapes.What if your data is counts? Number of cars passing a checkpoint per hour. Number of emails arriving per day. Number of words in a document. These are non-negative integers, often skewed, and a Gaussian is a poor fit for them.The natural distribution for count data is the Poisson distribution, which we introduced in Part 1. Recall:P(Y = y | λ) = (λʸ × e^−λ) / y!Where λ is the expected count: your model’s prediction.Take the log-likelihood:log L = Σ [ yᵢ × log(λᵢ) − λᵢ − log(yᵢ!) ]The last term, log(yᵢ!), doesn’t depend on your model parameters at all. So when you flip the sign and minimize, you can drop it:Loss = Σ [ λᵢ − yᵢ × log(λᵢ) ]This is the Poisson negative log-likelihood loss. It’s used whenever your target is a count.Where you’ll see it in practice:Event modeling: predicting how many times something will happen in a time windowTraffic counts: number of vehicles, clicks, purchases per intervalNLP token counts: modelling word frequencies in a documentAny scenario where your output is “how many times” rather than “what value” or “which class”The pattern should be familiar by now. You choose a distribution that matches the shape of your data. You write down its likelihood. You take the log, flip the sign, and drop the constants. What remains is your loss function.Section 3: The loss function inside a neural networkWe’ve been treating loss as something you write down on paper and hand to an optimizer. Now let’s open the black box and see what actually happens during training.A neural network is, at its core, a function with a very large number of parameters: weights and biases spread across many layers. You feed it an input, it produces an output, and the loss function measures how wrong that output is.But knowing how wrong you are is only half the battle. The question is: which parameters do you adjust, and by how much?The forward passTraining begins with a forward pass. You take a data point (or a batch of them), pass it through every layer of the network, and get a prediction out the other end.That prediction goes into the loss function. You get a single number back, the loss. High loss means the model is far from the truth. Low loss means it’s close.At this point the network knows that it’s wrong. It doesn’t yet know why.BackpropagationThis is where backpropagation comes in.Conceptually, backpropagation is the network asking a question at every layer: “how much did I personally contribute to this loss?” It works backwards from the loss, hence “back” propagation through every layer to every weight in the network.The mathematical tool it uses is the gradient, a measure of how much the loss changes if you nudge a particular weight slightly. A large gradient on a weight means that weight has a big influence on the loss. A small gradient means it barely matters right now.Think of it like this: you’re standing on a hilly landscape and you want to get to the lowest point. The gradient tells you the slope at your current position which direction goes uphill and how steep it is. You step in the opposite direction. That’s one training step.This process is called gradient descent.The optimizer’s jobThe gradient tells you the direction. The optimizer decides how big a step to take.The simplest optimizer just multiplies the gradient by a small number called the learning rate and subtracts it from the weight. Too large a learning rate and you overshoot, you jump past the valley floor and land on the other side. Too small and training takes forever.More sophisticated optimizers like Adam keep track of past gradients and adapt the step size automatically for each weight. But the core idea is the same: use the loss, use its gradient, move the parameters in the direction that reduces the loss.Do this thousands or millions of times across your data, and the parameters slowly converge to values that minimise the loss: which, as we now know, means they converge toward the maximum likelihood estimate.This is what training a neural network actually is.Section 4: When training goes wrongThe loss going down during training is a good sign. But it’s not the whole story. There are two fundamental ways training can mislead you, and understanding them comes directly from understanding what the loss function is actually measuring.UnderfittingUnderfitting happens when your model is too simple to capture the patterns in the data. The loss is high, not just on new data, but on the training data itself.Think of trying to fit a straight line to data that clearly follows a curve. The line will be consistently wrong everywhere. No amount of training will fix it, because the model doesn’t have the capacity to represent the true pattern.The signal: high training loss and high validation loss.The fix: a more expressive model, more parameters, or a different architecture.OverfittingOverfitting is the opposite problem and far more common. Your model is so expressive that instead of learning the underlying pattern, it memorises the training data noise and all.The training loss gets very low. But when you test the model on data it hasn’t seen, the loss shoots back up. The model has learned to describe the training set perfectly but has failed to generalise.Think of a student who memorises every past exam paper word for word. They’ll ace a repeat of those exact questions. Give them a question they haven’t seen before and they’re lost.The signal: low training loss, high validation loss.Decision boundaries for different Regression and Classification models (Source: Wonseok Shin)Regularization: teaching the model to generaliseRegularization is the set of techniques that push back against overfitting. The core idea is to add a penalty to the loss function that discourages the model from becoming too complex or too confident.L2 regularization (also called weight decay) adds a term to the loss proportional to the square of each weight:Total Loss = NLL + λ × Σ wᵢ²This pushes weights toward zero, preventing any single weight from dominating. It keeps the model smooth and general.L1 regularization does the same but with the absolute value of weights:Total Loss = NLL + λ × Σ |wᵢ|L1 tends to push weights all the way to zero, effectively removing features which is useful when you suspect many of your inputs are irrelevant.Dropout takes a different approach entirely. During training, it randomly switches off some neurons on each pass. This forces the network to learn redundant, distributed representations, it can’t rely too heavily on any one path through the network. At test time, all neurons are on.“X” indicates neurons that are dropped out during trainingThe common thread: all of these techniques modify or constrain the loss landscape to steer the model toward solutions that generalise rather than solutions that merely memorise.ConclusionA distribution assumption about your data gives you a likelihood. Likelihood gives you a log-likelihood. Log-likelihood gives you a loss. That loss flows backwards through your network via gradients, updating every weight on every training step. The optimizer decides how large each step is. Regularization keeps the whole thing honest.Every piece connects. Every choice has a probabilistic justification. Nothing is arbitrary.From Probability to Loss - How MLE Builds Machine Learning: Part 2 was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.