Digit recogniser — Artificial Neural Network (ANN)

Using the Artificial Neural Network (ANN) to make a churn model, we will create a model that predicts a handwritten digit. (with source code)

--

as in the previous article, I have given you an introduction to Artificial Neural Network (ANN) now I will tell you how to make a basic ANN model in this article with some lines of codes.

In this article we will take input of handwritten digits and we will predict the number using Artificial Neural Network (ANN).

If you want to learn more about Artificial Neural Network (ANN), follow this link -

Importing the libraries

Now we will import pandas, NumPy, Keras, Matplotlib, and Tensorflow as shown below. If your system does not have these libraries installed, you may get them using the pip command.

Dataset

Loading the dataset

Keras have provided the data and we can import the data like this.

Here we have imported train and test dataset from Keras

shape of the dataset

Now, we will be looking at the shape of train and test data to know how many images we will be training on model.

The dataset contains 60,000 images in train variable and 10,000 images in test variable

Plotting

Because the data is in a numpy array, we won’t be able to see any images because it’s an array, so we’ll have to plot the image on matplotlib with this command.

Scaling

as the values in array ranges from 0 to 255 where 0 means black and 255 means white so if we take this data into our model this would affect our model training so to overcome this problem we will feature scale our dataset and here we will be dividing by 255 so we can get a range from 0 to 1

Flattened

Now we are flattening our train and test dataset.

Model

Making an ANN model is far more difficult than making machine learning models.

First, I’m starting a sequential model with tensorflow keras. The second step is to add the hidden layers to the model. The layers are added like this: here in this model, there are two hideen layers and one layer to compile the above layers.

  • In the first hidden layer, I’m using 100 neurons and an activation function relu. I’ll explain these concepts in a later article, so just remember them for now.
  • The second hidden layer is similar to the previous one, and I’m using 10 neuron (because we have numbers 0–9 so only 10 numbers can be our output so that’s why we we are using 10 neuron in last layer) and the activation function sigmoid.

Here comes the compiler; now we must compile all three layers into the model. I am using the optimizer function as Adam, loss as sparse categorical crossentropy, and metrics as accuracy.

Now we must use the fit command to fit the entire dataset into the model.

batch size means number of examples in one iteration.

epochs means the number of passes of the entire training dataset the machine learning algorithm has completed.

Predicting

now here i am making an predictions variable which will predict the test dataset on trained model

Single value

we can use this line of code to predict a single value. here you can see that clearly the number is 1 and our trained model also predict 1

we can also make a function for predicting a single value

Accuracy

Now we’ll look at our model’s accuracy. Our model is 97 percent accurate, which means it correctly predicted 97 values out of a possible 100.

source code

you can go check the link for full code

Conclusion

in the article I have given you information and codes on how to make a Artificial Neural Network (ANN) with source code I would be making more exciting models for you so stay connected.

--

--