3.1.6Neural Network Fundamentals

Softmax for output layers

1,821 words8 min readdifficulty · medium

WHAT is softmax?


WHY these three design choices?

WHY exponentiate? We need every output positive. Raw logits can be negative; ezie^{z_i} is always positive, so no probability is ever negative. Exponentiation also amplifies gaps — a slightly larger logit becomes a much larger probability, giving a confident-but-differentiable "winner."

WHY divide by the sum? Normalization. After exponentiating we have positive numbers, but they don't sum to 1. Dividing by their total forces a valid distribution — like sharing a pie so all slices add up to the whole pie.

WHY not just normalize the raw logits directly (i.e. zi/zjz_i / \sum z_j)? Because raw logits can be negative, giving negative "probabilities," and the sum could be zero (division blows up). The exponential fixes both.


HOW to derive it from first principles

We want a function zp\mathbf{z} \to \mathbf{p} with these demands:

  1. pi0p_i \ge 0
  2. ipi=1\sum_i p_i = 1
  3. Larger ziz_i \Rightarrow larger pip_i (monotonic)
  4. Differentiable everywhere (so we can train with gradient descent)

Step 1 — force positivity. Apply a positive, increasing function ff. The exponential f(z)=ezf(z)=e^{z} is the canonical choice (satisfies 1, 3, 4).

Why exp specifically? It arises naturally from the maximum-entropy / Gibbs distribution: if you assume the log-probability is linear in the score, logpizi\log p_i \propto z_i, then piezip_i \propto e^{z_i}. It's the least-biased distribution consistent with given expected scores.

Step 2 — enforce sum-to-1. We have unnormalized weights ezie^{z_i}. Divide by their total: pi=ezijezjp_i = \frac{e^{z_i}}{\sum_j e^{z_j}} Now ipi=iezijezj=1\sum_i p_i = \frac{\sum_i e^{z_i}}{\sum_j e^{z_j}} = 1. ✔️ Done — this is softmax.

Figure — Softmax for output layers

Numerical stability trick (HOW to compute safely)

Why this is safe: the ratio is unchanged, so predictions are identical — we only fixed floating-point overflow.


The gradient (WHY softmax + cross-entropy is magic)

Softmax is almost always paired with the cross-entropy loss. For a true class yy (one-hot tit_i), loss L=itilogpiL = -\sum_i t_i \log p_i.

The beautiful result — derive it:

pizk=pi(δikpk)\frac{\partial p_i}{\partial z_k} = p_i(\delta_{ik} - p_k) where δik=1\delta_{ik}=1 if i=ki=k else 00. Combined with cross-entropy, the gradient w.r.t. the logits collapses to: Lzk=pktk\boxed{\frac{\partial L}{\partial z_k} = p_k - t_k}

Why this matters: the gradient is just (prediction − target). Clean, cheap, no vanishing pieces from the softmax and log cancelling. This is the reason they're used together.



Recall Feynman: explain to a 12-year-old

You have three friends voting on where to eat, and each shouts a "loudness score." Softmax turns those loudness scores into slices of a pizza. Louder shout ⇒ bigger slice, but every slice is positive and all slices together make one whole pizza. The loudest gets the most, but everyone gets some. That's how the computer says "I'm 66% sure it's a cat, 24% dog, 10% bird" — all adding up to one whole "sure."


Active Recall

Flashcards

What does softmax convert logits into?
A valid probability distribution — values in (0,1)(0,1) that sum to 1.
Write the softmax formula for class ii.
pi=ezi/jezjp_i = e^{z_i} / \sum_j e^{z_j}.
Why exponentiate the logits?
eze^{z} is always positive (no negative probabilities) and amplifies gaps between scores, while staying differentiable.
Why divide by the sum of exponentials?
To normalize so all outputs sum to 1, forming a proper distribution.
What is softmax's shift-invariance property?
Adding a constant cc to all logits leaves outputs unchanged: softmax(z+c)=softmax(z)\text{softmax}(z+c)=\text{softmax}(z).
What numerical trick prevents overflow?
Subtract maxkzk\max_k z_k from every logit before exponentiating.
What is the gradient of cross-entropy+softmax w.r.t. logits?
L/zk=pktk\partial L/\partial z_k = p_k - t_k (prediction minus target).
Softmax vs sigmoid output layer — when each?
Softmax for mutually-exclusive single-label classification; independent sigmoids for multi-label (classes not competing).
What's the derivative pi/zk\partial p_i/\partial z_k?
pi(δikpk)p_i(\delta_{ik}-p_k).
Why is applying softmax twice wrong?
It flattens the distribution toward uniform and discards information; apply once to raw logits.

Connections

  • Cross-Entropy Loss — softmax's natural partner; together give gradient ptp-t.
  • Sigmoid Activation — the 2-class / multi-label sibling of softmax.
  • Logits and Log-Odds — the raw inputs softmax consumes.
  • Maximum Entropy Distributions — theoretical justification for the exponential form.
  • Backpropagation — where the clean ptp-t gradient flows.
  • Temperature Scaling — dividing logits by TT to sharpen/soften softmax (calibration).

Concept Map

input to

produces

forces positivity

enforces sum to 1

amplifies gaps

property

justifies

derive

subtract max logit

derived from

fails, negatives and zero sum

Raw logits z

Softmax function

Probability distribution p

Exponentiate e^z

Divide by sum

Confident smooth winner

Each p in 0,1 and sum equals 1

Max-entropy Gibbs distribution

Design demands: positive, sum 1, monotonic, differentiable

Shift-invariance

Numerical stability

Normalize raw logits

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho tumhare neural network ke last layer se teen raw numbers nikalte hain — inko logits kehte hain, jaise [2.0,1.0,0.1][2.0, 1.0, 0.1]. Ye numbers negative bhi ho sakte hain, bade bhi — ye probabilities nahi hain. Softmax ka kaam hai in raw scores ko ek proper probability distribution mein badalna: har value 00 se 11 ke beech, aur sabka total exactly 11. Formula simple hai: pehle har logit ka ezie^{z_i} nikaalo (exponentiate), phir sabke sum se divide karo (normalize). Bas — "Exponentiate, then Equalize".

Exp kyun? Kyunki eze^{z} hamesha positive hota hai, toh koi bhi probability negative nahi aayegi, aur bade-chote scores ke beech gap bhi badh jaata hai (confident winner milta hai). Divide by sum kyun? Taaki total 11 ho jaaye — jaise ek poori pizza ko slices mein baant diya. Ek important practical trick: computation se pehle sabse bada logit subtract kar do (max subtraction). Isse answer bilkul same rehta hai (shift-invariance), par e1000e^{1000} jaisa overflow nahi hota.

Softmax ka best partner hai cross-entropy loss. Dono saath mein use karo toh gradient super clean nikalta hai: pktkp_k - t_k, yaani prediction minus target. Sahi class ka logit upar push hota hai, galat waale neeche — exactly jo learning ko chahiye. Isiliye classification networks mein ye combo standard hai.

Do galtiyan avoid karo: (1) softmax ko multi-label problem mein mat use karo — wahan classes compete karti hain (sum = 1), toh agar image "beach" aur "sunny" dono ho sakti hai toh alag-alag sigmoids lagao. (2) Softmax outputs ko "100% certain truth" mat samjho — ye relative scores hain, kabhi-kabhi overconfident aur miscalibrated hote hain.

Go deeper — visual, from zero

Test yourself — Neural Network Fundamentals

Connections