WHY exponentiate? We need every output positive. Raw logits can be negative; ezi 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/∑zj)? Because raw logits can be negative, giving negative "probabilities," and the sum could be zero (division blows up). The exponential fixes both.
Differentiable everywhere (so we can train with gradient descent)
Step 1 — force positivity. Apply a positive, increasing function f. The exponential f(z)=ez 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, logpi∝zi, then pi∝ezi. It's the least-biased distribution consistent with given expected scores.
Step 2 — enforce sum-to-1. We have unnormalized weights ezi. Divide by their total:
pi=∑jezjezi
Now ∑ipi=∑jezj∑iezi=1. ✔️ Done — this is softmax.
Softmax is almost always paired with the cross-entropy loss. For a true class y (one-hot ti), loss L=−∑itilogpi.
The beautiful result — derive it:
∂zk∂pi=pi(δik−pk)
where δik=1 if i=k else 0. Combined with cross-entropy, the gradient w.r.t. the logits collapses to:
∂zk∂L=pk−tk
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."
Socho tumhare neural network ke last layer se teen raw numbers nikalte hain — inko logits kehte hain, jaise [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 0 se 1 ke beech, aur sabka total exactly 1. Formula simple hai: pehle har logit ka ezi nikaalo (exponentiate), phir sabke sum se divide karo (normalize). Bas — "Exponentiate, then Equalize".
Exp kyun? Kyunki ez 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 1 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 e1000 jaisa overflow nahi hota.
Softmax ka best partner hai cross-entropy loss. Dono saath mein use karo toh gradient super clean nikalta hai: pk−tk, 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.