← All algorithms

Classification / Neural Network

Multilayer Perceptron, MLP

2023-12-25

​

In this tutorial, we build a multilayer perceptron (MLP) to classify handwritten digits from the MNIST dataset.

​The MNIST handwritten digit dataset is a collection of 28x28 pixel gray-scale images of handwritten digits from 0 to 9, commonly used as a representative dataset for classification and clustering algorithms.

​

​In Excel, the Solver can't handle the image of size 28x28 due to the limitation of the optimization, we need to down sample it to 7x7. To do this, first download the file from the following GitHub below, which converted the 28x28 size of data to 1x768 size and save it as a CSV file.

https://github.com/phoebetronic/mnist/blob/main/mnist_test.csv.zip

​The data file contains 10,000 handwritten dataset, and when converted back to 28x28, it has the following form and values. Below is an example of when the data is classified as 7.

​After extracting every 3rd data along the horizontal and vertical axes followed by excluding the data of the first row and column, the down-sampled data which is shown in 7x7 red color, is flattened back into a line. The numbers below indicate the corresponding positions among 1 to 768.

​The following result shows the 7x7 data at the position in red after the down sampling, before converted into a single line.

​Sampling of values for each column can be done using the Excel INDEX function as follows. Here, each value is divided by 100 for the convenience of numerical analysis. Ultimately, the size of the data is 10000x49, and each data is classified with a value between 0 and 9.

​

​

​

For the entire classification process, the following neural network structure was used. The nodes in the input layer are flattened 49 nodes from the 7x7 formation. The output layer consists of 9 nodes, each representing a number from 0 to 9, and the hidden layer uses 3 nodes.

​

​

The number of weights being used for the MLP formulation,

\(y=\phi _v(V\phi(Xw-b)-b_v)\)

is total 190 which is the sum of the number from the input layer to the hidden layer (49x3+3), and the number from the hidden layer to the output layer(3x10+10).

​

Step 0. Data preparation. Each row consists with one label between 0 and 9 and the 49 number of data between 0 and 2.55. In the attached Excel file, please refer the Worksheet '7x7' which contains 10000 rows of data.

Step 1. Prepare the Output node values for the multiclass classification.

​

Step 3. Secure the cell positions as follows for the coefficients and thresholds of the hidden layer, and for the weights and thresholds of the output layer, and initialize them with random values between 0 and 1.

Use Excel function RAND() along with Cell Function (Shift+Ctrl+Enter)

​

Step 4. Calculate the output of the hidden layer

Φ=1/(1+EXP(-SUMPRODUCT(W,X)+b))

​

Step 5. Calculate the output of the output layer

y'=1/(1+EXP(-SUMPRODUCT(V,Φ)+bv))

​

Step 6. To compute the cost per sample, calculate the sum of all the log likelihood at each output node.

\(L=\sum _{\ }^{\ }\lbrace y\log (y')+(1-y)\log (1-y')\rbrace \)

L = SUM(IF(y=1, log(y'), log(1-y')))

​

Step 7. The objective J to minimize is the negative sum over the all the sample cost L.

\(\min J=\min \sum _{\ }^{\ }(-L)\)

Step 8. Execute the Solver with the setting below.​

​

Step 9 After completion of the Solver, the prediction is the output node with the highest likelihood.

Even though the sample data is prepared by 7x7 down-sampling, when we split the data 70:30 for the training data and for the testing data using 10000 sample size, as shown in the Confusion matrix, the accuracy is 100%.

​