# Softmax Regression

### From Ufldl

## Contents |

## Introduction

**Softmax regression**, also known as **multinomial logistic regression**, is a generalisation of logistic regression to problems where there are more than 2 class labels.

Recall that in logistic regression, our hypothesis was of the form:

where we trained the logistic regression weights to optimize the (conditional) log-likelihood of the dataset using *p*(*y* | *x*) = *h*_{θ}(*x*). In softmax regression, we are interested in multi-class problems where each example (input image) is assigned to one of `k` labels. One example of a multi-class classification problem would be classifying digits on the MNIST dataset where each example has label 1 of 10 possible labels (i.e., where it is the digit 0, 1, ... or 9).

To extend the logistic regression framework which only outputs a single probability value, we consider a hypothesis that outputs K values (summing to 1) that represent the predicted probability distribution. Formally, let us consider the classification problem where we have *m* *k*-dimensional inputs with corresponding class labels , where , with *n* being the number of classes.

Our hypothesis *h*_{θ}(*x*), returns a vector of probabilities, such that

where are each *k*-dimensional column vectors that constitute the parameters of our hypothesis. Notice that normalizes the distribution so that it sums to one.

*Strictly speaking, we only need n − 1 parameters for n classes, but for convenience, we use n parameters in our derivation.*

Now, this hypothesis defines a predicted probability distribution given some `x`, *P*(*y* | *x*^{(i)}) = *h*(*x*^{(i)}). Thus to train the model, a natural choice is to maximize the (conditional) log-likelihood of the data, .

Motivation: One reason for selecting this form of hypotheses comes from connections to linear discriminant analysis. In particular, if one assumes a generative model for the data in the form and selects for *p*(*x* | *y*) a member of the exponential family (which includes Gaussians, Poissons, etc.) it is possible to show that the conditional probability *p*(*y* | *x*) has the same form as our chosen hypotheses *h*(*x*). For more details, see the CS 229 Lecture 2 Notes.

## Optimizing Softmax Regression

Expanding the log-likelihood expression, we find that:

Unfortunately, there is no closed form solution to this optimization problem (although it is concave), and we usually use an off-the-shelf optimization method (e.g., L-BFGS, stochastic gradient descent) to find the optimal parameters. Using these optimization methods require computing the gradient ( w.r.t. θ_{k}), which can can be derived as follows:

With this, we can now find a set of parameters that maximizes , for instance by using L-BFGS with minFunc.

### Weight Regularization

When using softmax regression in practice, it is important to use weight regularization. In particular, if there exists a linear separator that perfectly classifies all the data points, then the softmax-objective is unbounded (given any θ that separates the data perfectly, one can always scale θ to be larger and obtain a better objective value). With weight regularization, one penalizes the weights for being large and thus avoids these degenerate situations.

Weight regularization is also important as it often results in models that generalize better. In particular, one can view weight regularization as placing a (Gaussian) prior on θ so as to prefer θ with smaller values.

In practice, we often use a L2 weight regularization on the weights where we penalize the squared value of each element of θ. Formally, we use:

This regularization term is added together with the log-likelihood function to give a cost function, *J*(θ), which we want to **minimize** (note that we want to minimize the negative log-likelihood, which corresponds to maximizing the log-likelihood):

The gradients with respect to the cost function must then be adjusted to account for the weight decay term:

Minimizing *J*(θ) now performs regularized softmax regression.

## Parameterization

We noted earlier that we actually only need *n* − 1 parameters to model *n* classes. To see why this is so, consider our hypothesis again:

Letting Θ_{j} = θ_{j} − θ_{n} for gives

Showing that only *n* − 1 parameters are required.

In practice, however, it is often easier to implement the version which is over-parametrized although both methods will lead to the same classifier.

### Binary Logistic Regression

In the special case where *n* = 2, one can also show that softmax regression reduces to logistic regression: