Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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=1dwj2J_\lambda(w) = J(w) + \frac{\lambda}{2m} \sum_{j=1}^d w_j^2

Note: Bias term w0w_0 is typically NOT regularized.

L1 Regularization (Lasso):

Jλ(w)=J(w)+λm∑j=1d∣wj∣J_\lambda(w) = J(w) + \frac{\lambda}{m} \sum_{j=1}^d |w_j|

Gradient for L2-Regularized Logistic Regression:

∇wJλ(w)=1m∑i=1m(fw(xi)−yi)xi+λmw\nabla_w J_\lambda(w) = \frac{1}{m} \sum_{i=1}^m (f_w(x_i) - y_i) x_i + \frac{\lambda}{m} w

(ignoring w0w_0 in the regularization term)

2. MAP Interpretation of Regularization

Bayesian View: Treat parameters ww as random variables with prior P(w)P(w).

Maximum A Posteriori (MAP) Estimation:

wMAP=arg⁡max⁡wP(w∣X,y)=arg⁡max⁡wP(y∣X;w)P(w)w_{\text{MAP}} = \arg\max_w P(w|X, y) = \arg\max_w P(y|X; w)P(w)

Taking logs:

wMAP=arg⁡max⁡w[log⁡P(y∣X;w)+log⁡P(w)]w_{\text{MAP}} = \arg\max_w [\log P(y|X; w) + \log P(w)]

L2 Regularization = Gaussian Prior:

wj∼N(0,σ2)  ⟹  log⁡P(w)=−12σ2∑jwj2+constantw_j \sim \mathcal{N}(0, \sigma^2) \implies \log P(w) = -\frac{1}{2\sigma^2} \sum_j w_j^2 + \text{constant}

This yields L2-regularized logistic regression where λ=1/σ2\lambda = 1/\sigma^2.

L1 Regularization = Laplace Prior:

P(wj)∝exp⁡(−∣wj∣b)  ⟹  log⁡P(w)=−1b∑j∣wj∣+constantP(w_j) \propto \exp\left(-\frac{|w_j|}{b}\right) \implies \log P(w) = -\frac{1}{b} \sum_j |w_j| + \text{constant}

3. Multi-Class Classification

Problem: y∈{1,2,…,C}y \in \{1, 2, \ldots, C\} where C>2C > 2

Examples:

4. One-vs-All (OvA) Approach

Method: For each class j∈{1,…,C}j \in \{1, \ldots, C\}, train a binary classifier fwj(x)f_{w_j}(x) where:

Prediction:

y^=arg⁡max⁡jfwj(x)\hat{y} = \arg\max_j f_{w_j}(x)

Pros: Simple, easy to implement, can parallelize

Cons:

5. Softmax Regression (Multinomial Logistic Regression)

Idea: Directly model class probabilities P(y=j∣x)P(y=j|x) for all jj simultaneously.

Model Definition:

For each class jj, define a linear score:

zj=wj⊤xz_j = w_j^\top x

Parameter Matrix: W=[w1,w2,…,wC]∈R(d+1)×CW = [w_1, w_2, \ldots, w_C] \in \mathbb{R}^{(d+1)\times C}

Softmax Function: Converts scores to probabilities

fW(j)(x)=P(y=j∣x;W)=exp⁡(zj)∑c=1Cexp⁡(zc)=exp⁡(wj⊤x)∑c=1Cexp⁡(wc⊤x)f_W^{(j)}(x) = P(y=j|x; W) = \frac{\exp(z_j)}{\sum_{c=1}^C \exp(z_c)} = \frac{\exp(w_j^\top x)}{\sum_{c=1}^C \exp(w_c^\top x)}

Properties:

Prediction Rule:

y^=arg⁡max⁡jfW(j)(x)=arg⁡max⁡jwj⊤x\hat{y} = \arg\max_j f_W^{(j)}(x) = \arg\max_j w_j^\top x

Cross-Entropy Loss for Softmax:

For training example (xi,yi)(x_i, y_i), define:

Per-example Loss:

costi=−∑j=1CI(yi=j)⋅log⁡(fW(j)(xi))=−log⁡(fW(yi)(xi))\text{cost}_i = -\sum_{j=1}^C \mathbb{I}(y_i = j) \cdot \log(f_W^{(j)}(x_i)) = -\log(f_W^{(y_i)}(x_i))

Overall Objective:

J(W)=1m∑i=1m∑j=1CI(yi=j)⋅log⁡(fW(j)(xi))=−1m∑i=1mlog⁡(fW(yi)(xi))J(W) = \frac{1}{m} \sum_{i=1}^m \sum_{j=1}^C \mathbb{I}(y_i = j) \cdot \log(f_W^{(j)}(x_i)) = -\frac{1}{m} \sum_{i=1}^m \log(f_W^{(y_i)}(x_i))

Convexity Proof Sketch:

The softmax objective is convex because:

  1. The log-sum-exp function g(z)=log⁡(∑cexp⁡(zc))g(z) = \log(\sum_c \exp(z_c)) is convex

  2. The softmax loss can be written as: costi=−wyi⊤xi+log⁡(∑cexp⁡(wc⊤xi))\text{cost}_i = -w_{y_i}^\top x_i + \log(\sum_c \exp(w_c^\top x_i))

  3. This is an affine function plus a convex function (log-sum-exp)

  4. Therefore, the overall objective is convex in WW

6. Gradient Descent for Softmax Regression

Gradient with respect to wjw_j:

∂J(W)∂wj=1m∑i=1m[fW(j)(xi)−I(yi=j)]xi\frac{\partial J(W)}{\partial w_j} = \frac{1}{m} \sum_{i=1}^m [f_W^{(j)}(x_i) - \mathbb{I}(y_i = j)] x_i

Proof:

From J(W)=−1m∑ilog⁡(fW(yi)(xi))J(W) = -\frac{1}{m} \sum_i \log(f_W^{(y_i)}(x_i)), where:

fW(yi)(xi)=exp⁡(wyi⊤xi)∑cexp⁡(wc⊤xi)f_W^{(y_i)}(x_i) = \frac{\exp(w_{y_i}^\top x_i)}{\sum_c \exp(w_c^\top x_i)}

Let σj=fW(j)(xi)\sigma_j = f_W^{(j)}(x_i) for brevity.

For a single example (x,y)(x, y) with zj=wj⊤xz_j = w_j^\top x:

−∂∂wjlog⁡(σy)=−∂∂wj[zy−log⁡(∑cexp⁡(zc))]=−[∂zy∂wj−1∑cexp⁡(zc)⋅∂∂wj∑cexp⁡(zc)]\begin{align*}-\frac{\partial}{\partial w_j} \log(\sigma_y) &= -\frac{\partial}{\partial w_j} \left[z_y - \log\left(\sum_c \exp(z_c)\right)\right] \\ &= -\left[\frac{\partial z_y}{\partial w_j} - \frac{1}{\sum_c \exp(z_c)} \cdot \frac{\partial}{\partial w_j} \sum_c \exp(z_c)\right]\end{align*}

Case 1: j=yj = y (true class):

−∂∂wylog⁡(σy)=−[x−1∑cexp⁡(zc)⋅exp⁡(zy)⋅x]=−[x−σyx]=(σy−1)x\begin{align*}-\frac{\partial}{\partial w_y} \log(\sigma_y) &= -\left[x - \frac{1}{\sum_c \exp(z_c)} \cdot \exp(z_y) \cdot x\right] \\ &= -[x - \sigma_y x] \\ &= (\sigma_y - 1)x\end{align*}

Case 2: j≠yj \neq y:

−∂∂wjlog⁡(σy)=−[0−1∑cexp⁡(zc)⋅exp⁡(zj)⋅x]=σjx\begin{align*}-\frac{\partial}{\partial w_j} \log(\sigma_y) &= -\left[0 - \frac{1}{\sum_c \exp(z_c)} \cdot \exp(z_j) \cdot x\right] \\ &= \sigma_j x\end{align*}

Combining both cases using indicator function:

−∂∂wjlog⁡(σy)=(σj−I(y=j))x-\frac{\partial}{\partial w_j} \log(\sigma_y) = (\sigma_j - \mathbb{I}(y=j)) x

Update Rule (SGD): For randomly sampled (xi,yi)(x_i, y_i):

wj←wj−α[fW(j)(xi)−I(yi=j)]xiw_j \leftarrow w_j - \alpha [f_W^{(j)}(x_i) - \mathbb{I}(y_i = j)] x_i

Remark: This is a natural extension of logistic regression gradient descent. For C=2C=2, it reduces to the logistic regression update.