← All algorithms

Classification

Softmax Classifier

2023-12-17

Logistic regression can be used for binary classification, with labels of 0 or 1. For problems with more than two classes, we can use a one-vs.-all strategy or softmax regression, an extension of logistic regression.

One vs. All strategy can be carried out by performing multiple Binary classifications to identify the decision boundary which only suitable for the interested class. For example, if there are three kinds of balls marked by red, green, and blue. We can arrange the training set such that red balls = 0 and all others (i.e. green and blue) = 1 to perform any binary classification (e.g. Logistic regression) to set the decision boundary for the red ball only. The same procedure can be repeated to the green or blue balls to find their own decision boundaries.

For the quick grasp of the idea, the Softmax classifier will be explained in a comparison with the Logistic Regression.

​

(1) For the training purpose, each sample will have a list of flags of 0 or 1 to tell which class it belongs to. Logistic regression only needs one flag of either 0 or 1. Since Iris dataset consist with 3 classes, it will have the 3 label values per each sample as shown in the table below.

(2) Each class has its own independent coefficient vector. For example, if there are d features or independent variables and m classes, the total number of coefficients required is (d*m) number of weights and m number of the bias. For the iris dataset, since there are 4 features and 3 classes, the number of coefficients becomes 4x3+3.

\(z_k=\sum _{i=1}^4w_{ki}x_i+bias_k&s=1, k \in {1,2,3}\)

​​

(3) As a probability function, use the following 'Softmax' instead of 'Sigmoid' 

\(P_k=\dfrac{\exp (z_k)}{\sum _j^{\ }\exp (z_j)}\)

​

For example, when there are three classes, i.e. m=3, it is expressed as

\(P_k= \dfrac {\exp (z_k)} {\exp (z_1) + \exp (z_2) + \exp(z_3)}\)​

(4) The cost function for each sample uses Cross Entropy, which measures how well the actual and predicted probabilities match.

\(L_i=y_1\log P_1+y_2\log P_2+y_3\log P_3\)

Our goal of the optimization is to find the coefficient matrix to maximize the total sum of all the costs for the training samples. ​

\(L=\sum _{\ }^{\ }L_i\)

​We often prefer "minimize" to "maximize" cost function akin to the regression. This can be done simply by taking negative of the cost function.

(5) The inference from Softmax classifiers can be carried out by selecting the class with the largest z (or P) value. 

In the following steps show how to implement Softmax in Excel worksheet using the Iris dataset.

Step1. Generate a response array by assigning three flags to choose 0 or 1 per each data. Use Array function to fill the Columns at once.​

Column 'F' {=IF(A2:A151="Iris-setosa",1,0)}

Column 'G' {= If(A2:A151="Iris_versicolor",1,0)}

​Column 'H' {= If(A2:A151="Iris_virginica",1,0)}

Note that the brace {} at the function call represents it is an array function.

​

Step 2. Set the size of unknown coefficients and bias and initialize with random numbers.

We will use an array function to call 'RAND' function. First select the empty 5x3 Cells which include 4 weights for 4 source parameters and 1 bias for each Iris type.

Then, type '=RAND()' at the function window followed by Shift+Ctrl+Enter to excute Array function call.

Step 3. Calculate "z" using MMULT by Array Function.

​Z = X • W + bias = MMULT(X,W)+Bias

​

Step 4. Calculate the LOG likelihood of Softmax ​

Q2=LOG(EXP(N2)/(EXP($N2)+EXP($O2)+EXP($P2)))

​

​Step 5. Compute the cost to minimize per each sample for the given W

Cell T2 = -SUMPRODUCT(F2:H2,Q2:S2)

Cell T3 = -SUMPRODUCT(F3:H3,Q3:S3), then copy and paste to the rest of the Column T.

​

Step 6. The cost function to minimize using Solver is the sum of all the cost.

​

Step 7. Excute Excel Solver ​with setting as below to optimal coefficients.

Set Objective: = Total Cost

To: Min

Changing Variable Cells: All the coefficients

​After Solver found a solution, it pops out following message window. Click 'OK'

Step 8. After completion of the optimization, The most likely class, which is the largest p value, among them is the inference per sample. They can be found using the following INDEX, MATCH, and MAX functions. 

U2 =INDEX($F$1:$H$1,1,MATCH(MAX(Q2:S2),Q2:S2,0))

The inference results are shown at Column U.​

​

<Note> Proof of the Softmax classification of the two classifiers is essentially same with Logistic Regression.

(1) Cost function of the two classifiers, m=2

Since y1=0, and y2=1, P0 = 1-P1, The cost function, L , becomes

\(L=\sum _{y=1}^{\ }P_1+\sum _{y=0}^{\ }(1-P_1)&s=1&fg=eeeeee&bg=1e1e1e\)

which is the cost function of the Logistic Regression.

​(2) Softmax function with the two classifiers

\(P_1=\dfrac{\exp (z_1)}{\exp (z_0)+\exp (z_1)}\)latex L=\sum _{\ }^{\ }L_i&s=1&fg=eeeeee&bg=1e1e1e$

Divide both the nominator and the denominator by exp(z1), and let

\(z_1-z_0\equiv z \)latex L=\sum _{\ }^{\ }L_i&s=1&fg=eeeeee&bg=1e1e1e$

At the ends, it becomes Sigmoid function.

\(P_1=\dfrac{1}{1+\exp -(z_1-z_0)}=\dfrac{1}{1+\exp (-z)} \)latex L=\sum _{\ }^{\ }L_i&s=1&fg=eeeeee&bg=1e1e1e$