Predicting Handwritten Digits using MNIST
MNIST is a collection of handwritten digits that is commonly used for training various image processing systems. MNIST contains about 60000 training images and 10000 testing images.
Since we are working with multiple layers and networks of layers here, we are using KERAS
Keras is an open-source software library that provides a python interface for artificial neural networks. Let’s have a look at the terms and functions that we are going to use in this project.
— ] models- A machine learning model is an algorithm that is trained over a set of input and output data so that it can predict the output when a set of test data is provided as input.
— ] layers- A layer in ML receives a bulk amount of input data, processes it using certain functions, and provides a set of output data. This output can be passed to the upcoming hidden layers. At the final layer, these outputs are approximated to provide a final output.
— ]optimizers- Optimizers are algorithms or methods used to change the attributes of the neural network such as weights and learning rate in order to reduce the losses. Optimizers help to get results faster.
— ] sequential- It’s used to arrange the layers in sequential order. In this project, we use multiple layers including many hidden layers to improve the accuracy of the algorithm. Hence, an ordered layer is better than an unordered one. Thus, we are using the sequential() method to arrange the layers.
In this project, we are using the MNIST dataset. It’s split into two halves. One part is used to train the algorithm and the remaining part is used to test the trained algorithm.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
The above statement loads data from the mnist dataset into the four variables namely x_train, y_train, x_test, and y_test.
x_train.shape, y_train.shape, x_test.shape, y_test.shape
The above statement returns the dimensions of the image from the mnist dataset.
Also, x_train.reshape can be used to change the dimensions of the image.
Consider the snippet,
>>> (60000, 28, 28)
>>> (60000, 784)
y_train = keras.utils.to_categorical(y_train, num_classes)
The above statement converts the class vectors to binary class matrices where the num_classes contain 10 classes each representing digits from 0 to 9.
To take a look at the model, the summary() function is used.
Compile the model using the code,
Train the model so that it can learn from the data provided.
Here, we use the counter like a variable called the epoch.
One epoch means that the ML algorithm has completed passing through the entire training set.
More number of epoch ensures that the algorithm will be more accurate as the entire training process is repeated epoch number of times.
Finally, evaluate the model using the code,
At last, the model predicts the handwritten image of digits with good accuracy.