Applying Convolutional Neural Network on mnist dataset:

Convolutinal Neural Network is also known as CNN, and it has become popular in recent times because of its usefulness. It consists of multilayer perceptrons for performing computational works. CNN is known to use very less pre-processing compared to other image classifying algorithms. It learns from filters, whereas the traditional ones uses hand-engineered algorithms. It is widely accepted that for image processing purposes CNN is the best option in python for data science.

MNIST dataset:

It is a dataset of handwritten images as shown below.

Applying Neural Network on mnist dataset in Python for Data Science

It is possible to achieve 99.6% accuracy by the use of CNN along with functional model. The reason we use functional model is because it maintains easiness while connecting the layers.

  • We should include the libraries first.Applying Neural Network on mnist dataset in Python for Data Science
  • Creation of test data and training data.

When we proceed further, we will use img_rows and img_cols as image dimensions. In case of mnist dataset, it is 28 and 28. It is also there to check the data format i.e., ‘channels_first’ or ‘channels_last’. In CNN, it is possible to normalize data and reduce the large terms of the calculation to smaller terms beforehand. Here we will normalize the x_train and x_test data by dividing it with 255.

Checking the data-format:

Applying Neural Network on mnist dataset in Python for Data Science

  • Description of output classes:

As the output of the model can have any number from digits 0 to 9 so, 10 classes are there in the output. For making output for 10 classes we will use keras.utils.to_categorical function that will provide us with 10 columns. Out of the 10 columns one value will be 1, and the rest will be 0. The column with 1 will denote the class of the digit.

Applying Neural Network on mnist dataset in Python for Data Science

Now as the dataset is ready, we will proceed toward the CNN model.

Applying Neural Network on mnist dataset in Python for Data Science

  • Calling of compile and fit function:
    Applying Neural Network on mnist dataset in Python for Data Science
  • Evaluate function:

The model.evaluate will provide the score for the test data. The model will now predict the class of the data, and the predicted class will be matched to y_test label for giving accuracy.

OUTPUT:

So, to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.