2.2.12 · D2Linear & Logistic Regression

Visual walkthrough — Multinomial - softmax regression

2,098 words10 min readBack to topic

We will use a running example of three contestants with raw scores , , . Watch these same three numbers travel through every step.


Step 1 — What we start with: raw scores that can be anything

WHAT. A machine looks at an input (a picture of a digit, say) and, for each possible answer, spits out one number. We call these numbers logits — a fancy word for "raw scores before we tidy them up." We write them , where is how many possible answers there are.

  • — the raw score for class . It can be any real number: big, small, positive, or negative.
  • — the machine's "rulebook" for class (one per class).
  • — the input, written as a list of numbers.

WHY. Before we can talk about probabilities (which must behave nicely), we must be honest that the machine's raw output is unconstrained. Nothing stops from being .

PICTURE. Three bars, one per contestant, sitting on a number line that runs into negative territory. Notice the bars can dip below zero — that is the whole problem we must fix.

Figure — Multinomial - softmax regression

Step 2 — Problem 1: probabilities can't be negative

WHAT. A probability is a fraction of certainty. You cannot be " sure." So any raw score that is negative is illegal as a probability. We need a machine that takes any real number and returns a positive number, while keeping the order (a bigger score should stay bigger).

WHY this tool — the exponential . Why not just "add a big number to make everything positive," or "take absolute value"? Absolute value would make and collide — order lost. Adding a constant fails if a score is very negative. We want a function that is:

  • always positive (never touches zero, never dips below),
  • increasing (bigger input → strictly bigger output, so order is preserved),
  • smooth (so we can take gradients later).

The exponential does exactly this. is just a fixed magic number (). Raising it to any power gives a positive result: (tiny but positive), , .

PICTURE. The left panel shows the negative-capable line ; the curve bends every input up into positive territory. The right panel shows our three scores after the lift — all bars now above zero, and the tallest score got stretched the most.

Figure — Multinomial - softmax regression

Step 3 — Problem 2: they must add up to one whole

WHAT. Probabilities of all the possible answers must add up to (i.e. something must be the answer). Right now our lifted scores sum to , which is not .

To fix any pile of positive numbers so it sums to 1, divide each one by the total. We give the total a name, the normalizer:

  • — "add up, letting run over every class to ."
  • — the lifted score of class .
  • — one single number: the grand total of all lifted scores.

Then each share is:

  • — the fraction of the whole pie that class gets. Guaranteed positive (top and bottom both positive) and guaranteed to sum to 1 (every slice divided by the same total).

WHY. Dividing by the total is the only operation that forces the pieces to sum to exactly 1 while keeping their ratios intact. It's the same as slicing a pizza: everyone's slice is "their portion ÷ the whole pizza."

PICTURE. The three lifted bars get poured into one pie. Each contestant's slice angle is proportional to their bar height. Slices fill the pie completely — no gaps, no overlap.

Figure — Multinomial - softmax regression


Step 4 — Edge case: what if two scores are equal? What if one is huge?

WHAT. We must check the machine behaves sensibly at the corners, not just the "nice" example.

  • All scores equal (): every lifted score is the same, so every slice is . With that's a perfectly fair three-way split of each. Softmax says "I have no idea, so I'll guess evenly" — exactly right.
  • One score dominates ( while others stay fixed): swamps the sum, and the rest . Softmax turns into a confident "it's class 1."
  • One score very negative (): , so but never exactly 0. This is important — softmax never fully rules a class out, which keeps finite later.

WHY. These three limits confirm the three properties we demanded: positivity (nothing ever hits 0), sum-to-one (holds in every limit), and monotonicity (bigger score → bigger slice, all the way to certainty).

PICTURE. Three mini-pies side by side: the fair split, the near-certain split, and the "almost-ignored" split. Watch how the slices move as one score grows.

Figure — Multinomial - softmax regression

Step 5 — The hidden freedom: shifting all scores changes nothing

WHAT. Add the same constant to every score. Then , so

  • — the rule that "adding in the exponent = multiplying outside."
  • The appears in both the top and bottom, so it cancels. Same slices.

This is called shift-invariance. Our scores and (all shifted by ) give identical probabilities .

WHY it matters practically. Because we can shift freely, we shift by so the largest score becomes before exponentiating. Then no can overflow to infinity — this is the Log-Sum-Exp Trick. Same answer, safe arithmetic.

PICTURE. Two pies from two shifted score-sets. Bars move up by on the left; the two pies on the right are pixel-for-pixel identical.

Figure — Multinomial - softmax regression

Step 6 — Grading the machine: cross-entropy loss

WHAT. Now the machine predicts slices , but we know the true answer. We encode the truth with a one-hot vector (see One-Hot Encoding): a list of zeros with a single at the true class. If the truth is class 1: .

We want a score that is small when the machine put a big slice on the true class, and big (bad) when it didn't. The natural choice from Maximum Likelihood Estimation is the negative log of the true class's slice:

  • — 1 only for the true class, 0 elsewhere. So the sum picks out one term: the true class.
  • — as , (no loss); as , (huge loss). The minus sign flips this into a positive penalty.

WHY ? Multiplying probabilities across many examples gives tiny unstable numbers; taking turns products into sums and stretches the "you were almost sure and wrong" region into a heavy penalty. This is Cross-Entropy Loss.

PICTURE. The curve of : flat and near zero when the true slice is fat, rocketing upward as the true slice shrinks. A dot marks our example (, loss ).

Figure — Multinomial - softmax regression

Step 7 — The beautiful cancellation: the gradient is just (predicted − true)

WHAT. To learn, we ask: if I nudge score , how does the loss change? That rate of change is . The clean result is:

  • — the slice the machine gave class (what it predicted).
  • — 1 if is the true class, else 0 (the truth).
  • prediction minus truth: the error on class .

WHY it's this clean. The in contributes a ; the softmax derivative contributes a factor; they cancel. Because the shift-freedom (Step 5) and the sum-to-one (Step 3) glue everything together, all the messy exp/normalizer terms collapse and only the error survives. Chaining to the weights via :

PICTURE. For our example (, truth class 1) the error vector is drawn as arrows: the true class's arrow points to raise its score, the wrong classes' arrows point to lower theirs. This is Gradient Descent in one glance.

Figure — Multinomial - softmax regression

The update (with input , true class 1) nudges by , i.e. it increases so class 1's score climbs next time — the machine learns to trust the right answer more.


The one-picture summary

The whole pipeline in a single strip: raw scores (can be negative) → exponentiate (all positive) → divide by the total (slices summing to 1) → compare to the one-hot truth → error flows back to fix the rules.

Figure — Multinomial - softmax regression
Recall Feynman retelling: explain the whole walkthrough to a friend

Three judges hand you three raw scores, and some can be negative — useless as probabilities. First you make every score positive without messing up the order, by using it as a power of the magic number ; big scores stretch bigger, negatives become tiny positives, none reach zero. Second you add up all these boosted scores and give each judge a slice of the pie equal to their share of that total — now every slice is positive and all slices add to one whole pie. Then you check who actually won using a one-hot flag (a single 1 at the true answer). You grade with of the true winner's slice: tiny loss if their slice was fat, huge loss if it was thin. Finally the fix is astonishingly simple: for each class, subtract truth from prediction (), multiply by the input, and step the rules that way — raise the true class's score, lower the impostors'. Exponentiate, share the pie, then push predicted toward true.


Connections