MNIST case study

Alessandro Aere

22nd May 2020

Case study: image classification

Problem description

A comparison of models will be made using the MNIST dataset, a collection of images representing handwritten digits.

Sample of the dataset

Sample of the dataset

More details of the datasets here.

In this report only the code concerning neural networks will be reported, not that of all the other statistical models, as they are not a topic of interest. In the conclusion you will find, however, the comparison of the results.

Import the Keras library

Run the code below if you wish to set in seed and obtain reproducible results.

Data ingestion

Data will be downloaded automatically with the following commands, which are available thanks to the keras library.

Data preparation

Normalize the input data, rescaling them between 0 and 1.

Transform the target variable into a categorical variable (using one-hot encoding).

Deep Neural Network

Specify the neural network’s architecture:

## Model
## Model: "sequential"
## ________________________________________________________________________________
## Layer (type)                        Output Shape                    Param #     
## ================================================================================
## dense (Dense)                       (None, 256)                     200960      
## ________________________________________________________________________________
## dense_1 (Dense)                     (None, 128)                     32896       
## ________________________________________________________________________________
## dense_2 (Dense)                     (None, 64)                      8256        
## ________________________________________________________________________________
## dense_3 (Dense)                     (None, 10)                      650         
## ================================================================================
## Total params: 242,762
## Trainable params: 242,762
## Non-trainable params: 0
## ________________________________________________________________________________

Compile the model:

Train the neural network.

The plot, representing the loss function and the accuracy in relation to the number of epochs, is shown below.

Evaluating the model on the test set.

## [1] "Loss on test data: 0.0877719819545746"
## [1] "Accuracy on test data: 0.979099988937378"

Convolutional neural network

Specify the neural network’s architecture:

## Model
## Model: "sequential_1"
## ________________________________________________________________________________
## Layer (type)                        Output Shape                    Param #     
## ================================================================================
## conv2d (Conv2D)                     (None, 26, 26, 32)              320         
## ________________________________________________________________________________
## max_pooling2d (MaxPooling2D)        (None, 13, 13, 32)              0           
## ________________________________________________________________________________
## conv2d_1 (Conv2D)                   (None, 11, 11, 64)              18496       
## ________________________________________________________________________________
## max_pooling2d_1 (MaxPooling2D)      (None, 5, 5, 64)                0           
## ________________________________________________________________________________
## conv2d_2 (Conv2D)                   (None, 3, 3, 64)                36928       
## ________________________________________________________________________________
## flatten (Flatten)                   (None, 576)                     0           
## ________________________________________________________________________________
## dense_4 (Dense)                     (None, 64)                      36928       
## ________________________________________________________________________________
## dense_5 (Dense)                     (None, 10)                      650         
## ================================================================================
## Total params: 93,322
## Trainable params: 93,322
## Non-trainable params: 0
## ________________________________________________________________________________

Compile the model:

Train the neural network.

The plot, representing the loss function and the accuracy in relation to the number of epochs, is shown below.

Evaluating the model on the test set.

## [1] "Loss on test data: 0.0345294252038002"
## [1] "Accuracy on test data: 0.991999983787537"

Results

Model Accuracy
Convolutional neural network 99.2%
Deep neural network 97.9%
Support vector machine 97.6%
Gradient boosting 97.1%
K-nearest neighbours 96.3%
Random forest 96.1%
Classification tree 87.6%