Developing a simple DNN for Handwritten Digit Classification.

Source: Deep Learning on Medium

Developing a simple DNN for Handwritten Digit Classification.

Being an engineering student, I must tell you Deep Learning is such a fascinating word. This fascinating word will soon turn into a fascinating world if the interest persists till the end.

This is how I started to learn and know more about the subject, just dive in and create a simple model by learning about the terms of deep learning.

If you are new to this field then please read this story before proceeding. If you are completely new then you must read this one.

Github link to this work:


The development of Deep Neural Network(DNN) model consists of four parts:

  • To fetch and visualize MNIST dataset.
  • Defining the layers of the DNN model.
  • Training of the model.
  • Prediction using the trained model.

Ground Work:

To begin with, in your jupyter-notebook import all the necessary modules and files.

Import all the Required Modules

Fetch and Visualize the Data:

Then for fetching the data we will use the MNIST dataset from the tensorflow. We will read the dataset and store it locally first.

Fetching the Data

Then for visualization we will use pyplot and display each one of the image. In the above example we are reading the data from test images, we can change this just by replacing mnist.test.images[…] to mnist.train.images[…]

Output from the Above Lines of Code

Defining the model and its layers:

For defining the model we should define number of neurons in each layer, should specify the inputs and declare placeholder variables. That part will be available in this github link.

For defining the model we can create our own function or we can use fully_connected function provided by tensorflow.

neuron_layer Function to Define the Model
Using fully_connected Function

For using the function we defined, fully_connected should be replaced with neuron_layer. The only difference is the activation function. In fully_connected function ReLU will be used as activation function by default, but in the function we defined the activation function should be specified.

Training :

Training will be done using Gradient Descent Optimizer provided by tensorflow. Followed by the iterative method to run the session and training operation.

Iterative Training
Model Undergoing Training


The above trained model should be saved. The saved model will be restored and used from the last checkpoint to predict the digit from the image. y_pred is the array which contains the predictions of the input image set.

Code to Predict the Output

The accuracy of the model can be increased if the model is trained on GPU. Also the learning rate can be tweaked in order to attain highest accuracy. But care should be taken to avoid over-fitting of the model on the training dataset. Training the model with huge dataset may help in over-fitting.

The complete code of the above model is available in my github repository. This is how I started my journey. I hope that this article/story will be of some use. Thank you!!!