2.2.12 · D4Linear & Logistic Regression

Exercises — Multinomial - softmax regression

3,346 words15 min readBack to topic

Before we start, four objects appear in almost every problem — let us pin them down precisely so nothing here relies on memory.

Now, one shared picture of the whole machine. Every exercise on this page lives in one of these four boxes.

Figure — Multinomial - softmax regression

Read it left to right: raw logits (any real number) → exp makes them positive → divide by the total makes them a probability pie → compare to the one-hot truth to get the error . Below, each exercise announces which box it lives in, so keep this figure in view.

To make the "SHARE the pie" box concrete before the numbers start, here is the same logit vector at three temperatures of confidence — watch the tall bars steal probability from the short ones:

Figure — Multinomial - softmax regression

Level 1 — Recognition

Goal: read the formula and plug numbers in. No traps yet, just fluency. (Pipeline box in play: the third box — "share / sum".)

Recall Solution L1.1

The denominator is the total of every exponentiated logit — one shared number for all classes (the size of the whole pie in the third box of figure s01). Because every is divided by that same total, The numerator sum and denominator sum are identical, so they cancel to exactly . This is why softmax outputs form a valid probability distribution: they always add to the whole pie.

Recall Solution L1.2

Exponentiate: , three identical numbers. Divide each by their sum: each class gets . Meaning: when all scores tie, softmax says "I have no idea" — perfectly uniform. This is the maximum-uncertainty case, matching the flat, equal-height panel of figure s02.


Level 2 — Application

Goal: run the full compute pipeline and one gradient step. (Pipeline boxes in play: all four boxes of figure s01, end to end.)

Recall Solution L2.1

Exponentiate using (box 2): Sum (box 3 normalizer). Divide: Notice the logs were chosen so the pie splits into clean tenths. Highest logit () → highest probability (). ✅

Recall Solution L2.2

One-hot zeroes every term except the true class (only ): Meaning: loss depends only on the probability you gave the correct class. Give it → pay . Had the model given class 3 a probability near , the loss would fall toward .

Recall Solution L2.3

For : . The gradient is negative-scaled because the true class was under-predicted (). Gradient descent subtracts this, so moves in the direction — raising next time. The model learns to trust class 3 more for this input. ✅


Level 3 — Analysis

Goal: reason about structure — invariances, limits, why the gradient is clean. (Pipeline boxes in play: box 2 "exp" for invariance; boxes 3→4 for the gradient.)

Recall Solution L3.1

Start from the definition with shifted logits : The factor appears in every numerator and in the denominator, so it factors out and cancels: This is the whole justification for the log-sum-exp trick: choose so the largest exponent becomes (and , never overflowing) — the probabilities are provably identical.

Recall Solution L3.2

Divide numerator and denominator by (legal since ): Meaning: with two classes only the difference matters — one extra degree of freedom (the common part) is invisible. That is exactly the shift-invariance of L3.1 showing up as Logistic Regression's single logit.

Recall Solution L3.3

Part (a) — deriving the softmax Jacobian. Write with normalizer . We differentiate with respect to one logit using the quotient rule; note (only the term of depends on ), and (the top moves only when — this is exactly what encodes). Split into two fractions and recognize and : Intuition: raising grows its own probability (the "self" term, present only when ) but simultaneously steals from every class by inflating the shared normalizer (the "competition" term). Softmax outputs are coupled — one bar rising pushes the others down (exactly what figure s02 shows).

Part (b) — the cancellation. Differentiate ; the chain rule on gives : The cancels the — the beautiful cancellation: Now (the delta picks out only the term) and (one-hot): Every messy exp/normalization term vanished, leaving prediction minus truth.

Part (c) — chain rule from logit to weights. The loss depends on only through the logit . So by the chain rule, the derivative with respect to one weight component is The first factor is from Part (b). For the second, since , differentiating w.r.t. one weight leaves only its own input: . Therefore Stacking over all components into a vector is exactly This is the rule we simply used in L2.3 — now derived: the scalar error scales the input direction .


Level 4 — Synthesis

Goal: combine numerical stability, loss, and gradient into one coherent computation. (Pipeline boxes in play: all four boxes of figure s01, with box 2 made numerically safe.)

Recall Solution L4.1

. Subtract it from each (shift-invariance guarantees the answer is unchanged): Exponentiate the safe, small numbers: Sum . Divide: Without the trick you would have computed — fine here, but the pattern is identical, and for logits like it prevents inf. Highest logit → highest prob. ✅

Recall Solution L4.2

(a) One-hot picks class 2: High loss — the model badly under-rated the true class (). (b) Error for : . Large negative error → a big corrective step: descent subtracts this, pushing up strongly to raise . The second component is because carries no information to update. ✅


Level 5 — Mastery

Goal: prove a property and connect the whole family.

Recall Solution L5.1

Compute the new logits after subtracting from every weight vector: where is the same scalar for every class (it depends on and , not on ). So all logits drop by the identical amount . By shift-invariance (Exercise L3.1), subtracting a common constant from all logits leaves softmax unchanged: Conclusion: the model is over-parameterized — an entire direction of weight-space (subtract any ) produces zero change in predictions. You can therefore pin without losing any expressive power, giving effectively independent weight vectors. This matches Logistic Regression (), which needs just one logit/weight vector, not two. ✅

Recall Solution L5.2

(a) Softmax: , , sum . (b) Sigmoid of difference: . They agree to four decimals. ✅ This is the Neural Network Output Layer fact: a 2-way softmax head is a sigmoid unit on the logit gap — the Logistic Regression special case, using Cross-Entropy Loss as its objective, itself derived by Maximum Likelihood Estimation.


Recap Reveal

Answer before revealing:

Softmax outputs of equal logits (K=3)
uniform, each
Why every softmax output is strictly positive
for any real , and a positive over a positive stays positive
Cross-entropy for one example collapses to
(one-hot kills other terms)
Gradient w.r.t. logit
Chain rule step from logit to weights
, since
Softmax Jacobian
Kronecker delta
if , else
Why the LSE trick leaves the loss unchanged
probabilities are shift-invariant, loss depends only on probabilities
2-class softmax equals
sigmoid of the logit difference
Independent weight vectors in softmax
(a common shift does nothing)

Connections