Softmax Regression
1. Regularized Logistic Regression¶
Problem: Overfitting when model is too complex or data is limited.
L2 Regularization (Ridge):
Jλ(w)=J(w)+2mλj=1∑dwj2 Note: Bias term w0 is typically NOT regularized.
L1 Regularization (Lasso):
Jλ(w)=J(w)+mλj=1∑d∣wj∣ Gradient for L2-Regularized Logistic Regression:¶
∇wJλ(w)=m1i=1∑m(fw(xi)−yi)xi+mλw (ignoring w0 in the regularization term)
2. MAP Interpretation of Regularization¶
Bayesian View: Treat parameters w as random variables with prior P(w).
Maximum A Posteriori (MAP) Estimation:
wMAP=argwmaxP(w∣X,y)=argwmaxP(y∣X;w)P(w) Taking logs:
wMAP=argwmax[logP(y∣X;w)+logP(w)] L2 Regularization = Gaussian Prior:
wj∼N(0,σ2)⟹logP(w)=−2σ21j∑wj2+constant This yields L2-regularized logistic regression where λ=1/σ2.
L1 Regularization = Laplace Prior:
P(wj)∝exp(−b∣wj∣)⟹logP(w)=−b1j∑∣wj∣+constant 3. Multi-Class Classification¶
Problem: y∈{1,2,…,C} where C>2
Examples:
Weather: {sunny,cloudy,rain,snow}
Email tags: {work,family,friends,hobby}
4. One-vs-All (OvA) Approach¶
Method:
For each class j∈{1,…,C}, train a binary classifier fwj(x) where:
Prediction:
y^=argjmaxfwj(x) Pros: Simple, easy to implement, can parallelize
Cons:
Requires training C separate models
Predicted probabilities don’t necessarily sum to 1
Class imbalance in each binary problem
5. Softmax Regression (Multinomial Logistic Regression)¶
Idea: Directly model class probabilities P(y=j∣x) for all j simultaneously.
Model Definition:¶
For each class j, define a linear score:
zj=wj⊤x Parameter Matrix: W=[w1,w2,…,wC]∈R(d+1)×C
Softmax Function: Converts scores to probabilities
fW(j)(x)=P(y=j∣x;W)=∑c=1Cexp(zc)exp(zj)=∑c=1Cexp(wc⊤x)exp(wj⊤x) Properties:
fW(j)(x)∈[0,1] for all j
∑j=1CfW(j)(x)=1
Prediction Rule:
y^=argjmaxfW(j)(x)=argjmaxwj⊤x Cross-Entropy Loss for Softmax:¶
For training example (xi,yi), define:
Ground-truth (one-hot): P(y=j∣xi,yi)=I(yi=j)
Predicted: P(y=j∣xi;W)=fW(j)(xi)
Per-example Loss:
costi=−j=1∑CI(yi=j)⋅log(fW(j)(xi))=−log(fW(yi)(xi)) Overall Objective:
J(W)=m1i=1∑mj=1∑CI(yi=j)⋅log(fW(j)(xi))=−m1i=1∑mlog(fW(yi)(xi)) Convexity Proof Sketch:¶
The softmax objective is convex because:
The log-sum-exp function g(z)=log(∑cexp(zc)) is convex
The softmax loss can be written as: costi=−wyi⊤xi+log(∑cexp(wc⊤xi))
This is an affine function plus a convex function (log-sum-exp)
Therefore, the overall objective is convex in W
6. Gradient Descent for Softmax Regression¶
Gradient with respect to wj:
∂wj∂J(W)=m1i=1∑m[fW(j)(xi)−I(yi=j)]xi Proof:
From J(W)=−m1∑ilog(fW(yi)(xi)), where:
fW(yi)(xi)=∑cexp(wc⊤xi)exp(wyi⊤xi) Let σj=fW(j)(xi) for brevity.
For a single example (x,y) with zj=wj⊤x:
−∂wj∂log(σy)=−∂wj∂[zy−log(c∑exp(zc))]=−[∂wj∂zy−∑cexp(zc)1⋅∂wj∂c∑exp(zc)] Case 1: j=y (true class):
−∂wy∂log(σy)=−[x−∑cexp(zc)1⋅exp(zy)⋅x]=−[x−σyx]=(σy−1)x Case 2: j=y:
−∂wj∂log(σy)=−[0−∑cexp(zc)1⋅exp(zj)⋅x]=σjx Combining both cases using indicator function:
−∂wj∂log(σy)=(σj−I(y=j))x Update Rule (SGD):
For randomly sampled (xi,yi):
wj←wj−α[fW(j)(xi)−I(yi=j)]xi Remark: This is a natural extension of logistic regression gradient descent. For C=2, it reduces to the logistic regression update.