Artificial Intelligence 🤖
Hyperparameter Tuning & Batch Normalization
Softmax Classification

Softmax Classificationn

While binary classification deals with two possible outcomes, Softmax regression is the go-to method for classifying instances into multiple categories, such as identifying animal types in images.

Working through Softmax

In a scenario where we have classes like Dog (11), Cat (22), Baby Chick (33), and None (00). Here, CC is the number of classes (4 in this example above), the range of classes is: [0,C1][0, C-1] and for nodes in the output layer, n[L]=Cn^{[L]} = C. We first convert the class labels into a vector representation using one-hot encoding.

One Hot Encoding

We use one-hot encoding to represent our classes. This means for each class, we have a vector where one element is 11 indicating the class, and the rest are 00s. It is the way we usually represent class labels in a vector. With our YY vector with numbers of range [0,C1][0, C−1], where CC is the number of classes, we perform one hot encoding to get a matrix YY with dimensions (C,m)(C, m). Say we have:

y=[123021]\mathbf{y} = \begin{bmatrix} 1 & 2 & 3 & 0 & 2 & 1 \end{bmatrix}

We convert this into:

[000100100001010010001000]class = 0class = 1class = 2class = 3\begin{bmatrix} 0 & 0 & 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 1 \\ 0 & 1 & 0 & 0 & 1 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ \end{bmatrix} \begin{array}{l} \text{class = 0} \\ \text{class = 1} \\ \text{class = 2} \\ \text{class = 3} \\ \end{array}

This is called "one hot" encoding because in the converted representation, exactly one element of each column is hot (meaning set to 1). To do this conversion in numpy, you might have to write a few lines of code. In TensorFlow, you can just use tf.one_hot(labels, depth, axis=0). axis=0 indicates the new axis is created at dimension 0.

Softmax Calculation

Given an input vector from the last layer z[L]\mathbf{z}^{[L]}, the Softmax function calculates the probabilities as follows:

σ(z[l])j=ezj[l]i=1Cti\sigma(\mathbf{z}^{[l]})_j = \frac{e^{\mathbf{z}^{[l]}_j}}{\sum_{i=1}^{C}{t}_i}

Where tit_i is:

ti=ez[l]t_i = e^{\mathbf{z}^{[l]}}

The denominator sums tt, the exponential function applied to each component of the input vector, ensuring that the softmax output is a probability distribution that sums to 1. For example, given

z[L]=[5213]\mathbf{z}^{[L]} = \begin{bmatrix} 5 \\ 2 \\ -1 \\ 3 \end{bmatrix}

We compute tt as follows:

t=[e5e2e1e3] \mathbf{t} = \begin{bmatrix} e^5 \\ e^2 \\ e^{-1} \\ e^3 \end{bmatrix}

Then we the softmax function as follows:

σ(z[L])=a[L]=[e5e5+e2+e1+e3e2e5+e2+e1+e3e1e5+e2+e1+e3e3e5+e2+e1+e3]=[0.8420.0420.0020.114]\sigma(\mathbf{z}^{[L]}) = \mathbf{a}^{[L]} = \begin{bmatrix} \frac{e^5}{e^5 + e^2 + e^{-1} + e^3} \\ \frac{e^2}{e^5 + e^2 + e^{-1} + e^3} \\ \frac{e^{-1}}{e^5 + e^2 + e^{-1} + e^3} \\ \frac{e^3}{e^5 + e^2 + e^{-1} + e^3} \end{bmatrix} = \begin{bmatrix} 0.842 \\ 0.042 \\ 0.002 \\ 0.114 \end{bmatrix}

The output of Softmax gives us a vector where each element is a probability of the input belonging to one of the classes i.e. P(classx(i))P(\text{class} | \mathbf{x}^{(i)}) . This result shown gives a a 0.8420.842 chance od being in class 0 for example. That is the highest probability, i.e. the "soft max".

Each of the CC values in the output layer will contain a probability of the example to belong to each of the classes.

Training the Classifier

The Softmax classifier uses a cross-entropy loss function, which aims to maximize the probability of the correct class. If the classifier is confident about the correct class, the loss is low. However, if it's unsure or wrong, the loss goes up.

Contrasted to softmax, there's an activation which is called hard max, which gets 1 for the maximum value and zeros for the others. If you are using NumPy, it's np.max over the vertical axis. It is a form of maximum likelihood estimation.

The Softmax name came from softening the values and not harding them like hard max i.e a more gentle maxing. Softmax is a generalization of logistic activation function to CC classes. If C=2C = 2, softmax reduces to logistic regression. The loss function used with softmax:

L(y,y^)=j=1Cyjlogy^jL(y, \hat{y}) = - \sum_{j=1}^{C}{y_j \log{\hat{y}_j}}

Here is an example. Say that we have a cat:

y=[0010]y = \begin{bmatrix} 0 \\ 0 \\ 1 \\ 0 \end{bmatrix}

And our softmax classifier outputs:

a[L]=y^=[0.30.20.10.4]\mathbf{a}^{[L]} = \mathbf{\hat{y}} = \begin{bmatrix} 0.3 \\ 0.2 \\ 0.1 \\ 0.4 \end{bmatrix}

We can compute the loss as follows:

L(y,y^)=j=14yjlogy^j=log0.1=2.3L(y, \hat{y}) = - \sum_{j=1}^{4}{y_j \log{\hat{y}_j}} = - \log{0.1} = 2.3

The loss function will first multiply out the incorrect classes in y^\hat{y}, and we are left with logy^2- \log{\hat{y}_2}. This means the loss function tries to make sure that the corresponding probability of that class is as high as possible (here y^2\hat{y}_2). The cost function used with Softmax:

J(w[1],b[1],...)=1mi=1mL(y(i),y^(i))J(\mathbf{w}^{[1]}, \mathbf{b}^{[1]}, ...) = - \frac{1}{m} \sum_{i=1}^{m}{L(y^{(i)}, \hat{y}^{(i)})}

Also, in terms of back propagation with softmax:

dz[L]=Lz[L]=a[L]y=y^yd\mathbf{z}^{[L]} = \frac{\partial L}{\partial \mathbf{z}^{[L]}} = \mathbf{a}^{[L]} - \mathbf{y} = \hat{\mathbf{y}} - \mathbf{y}