https://valueml.com/handwritten-digit-prediction-using-mnist-dataset-in-keras/
Handwritten Digit Prediction using MNIST Dataset in Keras
In this tutorial, we will learn to predict handwritten digit using MNIST dataset in Keras with the corresponding Python code. We will use Keras API for this purpose.
There will be the following sections:
- Importing Libraries
- Importing Dataset
- Data Preprocessing
- Building the model
- Training the model on the Dataset
- Predicting the test results
We will be using CNN(Convolutional Neural Network).
What is Convolutional Neural Network?
Convolutional Neural Networks are a special type of neural network which comes under Deep Learning. This neural network is majorly used for image-related datasets. CNNs are used in a lot of domains, especially in the medical field. eg: The respiratory detection system can be built using CNN.
HANDWRITTEN DIGIT PREDICTION Using MNIST DATASET
We will build a model predicting the handwritten digit.
1. IMPORTING LIBRARIES
Pandas: A Python package which is a fast, powerful, and open-source data manipulation tool.
Numpy: A Python package used for scientific computing.
Sequential: The model used will be sequential.
Dense, Conv2D, Flatten, MaxPooling2D: These are the layers we will use.
So, these are the initial libraries you need to have. You can find documentation of pandas, NumPy, and keras.layers libraries at the end of this tutorial.
2. IMPORTING DATASET
We have already imported the mnist dataset from keras. We need to load the data.
OUTPUT:
(60000, 28, 28)
3. DATA PREPROCESSING
First, we will transform our dataset,i.e. reshape.
OUTPUT:
(60000, 28, 28, 1)
We will normalize our data between 0 and 1.
Now, we need to take care of the class variables. For that, we will use to_categorical function from np_utils class.
Originally:
OUTPUT:
(60000,) [5 0 4 1 9 2 1 3 1 4]
After using the function:
OUTPUT:
(60000, 10) [[0. 0. 0. ... 0. 0. 0.] [1. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 1. 0.]]
4. BUILDING THE MODEL
We will build a CNN model.
We have used one Conv2D layer, one Maxpooling layer, and two dense layers. In between, we have used a flatten layer.
Next, we will compile the model:
Thus, the model has been built.
5. TRAINING THE MODEL
To train our model with the dataset, we will pass x_train and y_train into the fit() function.
I have given just five epochs. You can change according to your convenience.
OUTPUT:
Thus, we have built a model using mnist dataset from Keras that will predict the handwritten digits.
Also, you may visit:
- Getting Started with Keras
- Fundamentals of layers and models in keras
- Building A Neural Network using KERAS
- Prediction using LSTM
Thank you for reaching till here.
No hay comentarios:
Publicar un comentario