← Study Lab
Module 2updated 2026-07-04

Probability — where the loss comes from

A trained model never hands back a bare answer — it hands back a distribution, a spread of odds. Learning is choosing the knobs that make the data you actually observed most probable. That one idea, maximum likelihood, is the secret source of every loss function: mean-squared error is a Gaussian in disguise, cross-entropy is the categorical one. Fit a bell curve by hand, watch the loss appear as a bowl, then twist softmax logits and see cross-entropy punish a confident wrong answer.

Module 0 rolled a ball down a loss landscape. Module 1 showed the matmul that computes a model's raw scores. But we skipped the obvious question: where does the loss come from, and why is it shaped the way it is? The answer is probability. A model's real job is not to emit one number — it is to place a bet, to output a whole distribution over what it thinks is likely. Training then asks the same question a trillion times: given the data we genuinely saw, which knob settings make that data most probable? Answer that and the loss function writes itself — and, as CS229 shows in its very first probabilistic derivation, mean-squared error falls straight out.

The whole chapter in four lines1. A distribution spreads one unit of belief across every possible outcome — the bell curve is the famous one.
2. Likelihood asks: under these knobs, how probable was the data we actually observed?
3. The loss is negative log-likelihood — maximize the fit, minimize the loss; the same move.
4. Which distribution you assume picks the loss: Gaussian → MSE, categorical → cross-entropy.

2.1 A model outputs a distribution, not a number

Picture a single measurement — a pixel's brightness, a sensor reading, tomorrow's price. Instead of one guess, describe your belief as a curve over all the values it could take. The workhorse curve is the Gaussian (the "normal" distribution, the bell), and it has exactly two knobs: the center μ and the spread σ. Its height at a value x is the density — how much belief sits near x:

p(x)=1 / (σ√2π)·e−(x−μ)² / 2σ²

μ the center — where the bell sitsσ the spread — how wide the belief isarea = 1 total belief always sums to one

Two things matter more than the formula. First, the area under the whole curve is always 1— you have exactly one unit of belief and the curve only decides how to spread it. Second, density is not the probability of an exact point; it is how concentrated belief is there. A tall, narrow bell (small σ) says "I'm confident it's near μ"; a low, wide bell says "could be anywhere." Confidence is a knob. That's why σ also carries the famous 68–95–99.7 rule: about 68% of belief lands within ±1σ of the center, 95% within ±2σ, 99.7% within ±3σ — the shading you'll see under the curve below.

2.2 Likelihood — scoring the knobs against reality

Now flip it around. You are handed real data points and asked: how good are these knobs? For one point, the curve's height there is its score — high if the point lands under the fat middle of the bell, low if it's stranded out in a thin tail. For the whole dataset, multiply every point's height together. That product is the likelihood — the probability of having drawn exactly this data under these knobs. Because multiplying many small numbers underflows, we take the log, which turns the product into a friendly sum:

log L=logi p(xi)=Σilogp(xi)

Below is that idea you can grab. The bell is the model; the dots on the axis are real data. Each point grows a stick up to the curve — tall and green when the knobs explain it well, short and amber when they don't. Drag μ and σ to make the total as tall as possible. Then press snap to best fit to jump to the knobs that maximize it — and click the plot to drop in a new data point and watch the fit react.

viz 1 fit the bell — raise the likelihood by hand
drag μ and σ to raise the likelihood · click the plot to add a data point
mean μ0.00
spread σ1.50
log-likelihood
loss = −log L

Notice what your hands just learned. Slide μ off to the side and every stick shrinks — the loss shoots up. Shrink σ too tight and the far points fall off the thin tails and get punished; widen it too much and even the central points sit low under a flat curve. There is exactly one best setting, and snap to best fit lands on it. That best setting has a name: the maximum-likelihood estimate, and for a Gaussian it's just the data's mean and standard deviation.

2.3 Maximum likelihood is your loss — and MSE is a Gaussian

Maximizing a likelihood and minimizing a loss are the same act seen from two sides. Flip the sign of the log-likelihood and you get the quantity training actually drives downhill — the negative log-likelihood (NLL), the loss from Module 0:

loss=Σilogp(xi)NLL

Here is the punchline that makes probability the root of the whole field — the derivation CS229 does in its opening lectures. Take the Gaussian, hold σ fixed, and expand−log p(x). The constants log(σ√2π) don't depend on μ, so they fall away, and what's left is (x − μ)² / 2σ². Summed over the data:

NLL(μ)=const+1/2σ²Σi(xiμ

That sum Σ(xᵢ − μ)² is mean-squared error. MSE was never an arbitrary choice — it is exactly what you get when you assume your errors are Gaussian and ask for the most likely knobs. And because it's a sum of squares in μ, the loss is a perfectparabola — a bowl, the same convex bowl the ball rolled down in Module 0. Dragμ below and watch the ball ride the NLL bowl; its lowest point sits exactly over the data's mean. Widen σ and the bowl flattens (the model is less sure, so being off costs less); tighten it and the walls steepen.

viz 2 the loss is a bowl — Gaussian NLL over μ is mean-squared error
μ
data mean (min)
Σ(xᵢ−μ)² (MSE·n)
NLL (loss)
Every loss you'll meet is a negative log-likelihood in disguiseAssume Gaussian noise → the loss is mean-squared error. Assume a categorical output → the loss is cross-entropy. Assume Laplace noise → you get mean absolute error. Choosing a loss is really choosing what kind of distribution you believe your data came from. The optimizer just finds the knobs that make it most probable.

2.4 Surprise — why the log, and why −log p

Where does the −log come from, really? Information theory gives it meaning: −log pis surprise (Shannon called it "self-information"). An outcome the model was sure of (p → 1) carries zero surprise — you learned nothing, you already knew it. An outcome it thought impossible (p → 0) is infinitely surprising — a slap in the face. Training to minimize −log p(data) is training the model to be as un-surprised by reality as possible. Drag the probability the model assigned to the thing that actually happened, and watch the surprise the loss charges it:

viz 3 surprise = −log p — being confidently wrong is catastrophic
p0.50
surprise −log p0.69
verdict

2.5 The discrete cousin: softmax & cross-entropy

Bell curves are for continuous values. For a choice — which digit, which class, which next token — the model instead outputs one probability per option. A layer produces raw scores called logits(a matmul, from Module 1), and softmax exponentiates them (making everything positive) and normalizes (making them sum to 1): a proper distribution over the options.

softmax(z)i=ezi / Σj ezj,CE=logptrue

The loss looks only at the option that was actually correct and reports−log(probability the model gave it) — the surprise curve you just dragged, applied to the true class. Confident and right → near-zero loss; confident and wrong → a large one. That iscross-entropy, and it is, once again, a negative log-likelihood. Twist the logits below, pick which class is truly correct, and watch softmax redistribute the belief while cross-entropy scores it. Every single next-token an LLM predicts during training is graded by exactly this rule, billions of times.

viz 4 logits → softmax → cross-entropy · click a bar to set the true class
true class0
p(true)
cross-entropy

Play the two failure modes. Pile all the logit mass on the wrong class while the truth sits low — the loss explodes toward the steep left wall of the surprise curve. Now spread the logits flat (all equal): softmax returns 1/4 for everyone and the loss is −log(0.25) ≈ 1.39 — the "I have no idea" baseline. Good training drives the true class's bar toward 1 and the loss toward 0.

▸ how this connects to the endgame

A training run exists to push one number — the negative log-likelihood — downhill. Each step evaluates that loss on a batch and backpropagates its gradient, and every one of those evaluations is a tower of the matmuls from Module 1. So the loss is the reason the GPU is lit up at all: the whole machine is grinding to make the observed data one notch more probable.

Which sharpens the question Plasmient exists to answer. nvidia-smi tells you the GPU was "busy" — but busy doing what? The only work that matters is the arithmetic that actually moved the loss. If lanes stalled on memory while the loss barely budged, that FLOP-time was spent, not used. To measure a training run honestly you must connect the loss going down to the work truly done to move it. That link is the product.

Go deeper — the original sources

We teach it in our own words and build the demo ourselves. To go to the source, these are the lectures and texts this chapter draws on — linked, not rehosted.