Deep Learning & Neural Networks
In this tutorial , you will learn what Deep Learning and Neural Networks are and by the end of these tutorials you will be able to implement your own Neural Nets to Classify Digits from 0 to 9.
So Let’s Dive in,
Basic Introduction
Deep Learning basically is a subfield of machine learning, former traditional machine learning algorithms were based on some mathematical model and most of them were just one shot learning. The Drawback of those algorithms was they weren’t able to learn deep meanings or features from Data.
Deep Learning is different from those shallow learning algorithms, in that Deep Learning algorithm uses many weights and then combines these weights to learn features from data.
These weights are called Learnable Parameters , and are learned during the training of Deep Learning models. And the Architecture which contains all these learnable parameters is known as “Neural Network”.
The Image of Neural Nets is shown above, generally we divide neural nets into several layers.
Input Layer : It has all the data which is to be input.
Output Layer : The data which we need as an output of the model.
Hidden Layer : the Layer which contains all Learnable Parameters.
This theory is enough for basic understanding, Now we will see the computational part of Neural Nets.
Computations in Neural Nets
A single hidden layer which is also called as Dense Layer is consists of :
Weight Matrix.
Bias Vector
Activation Function (Optionally)
We will first perform matrix multiplication of input with a weight matrix then we will add a bias vector to the result, and then if activation is given we will pass the result through activation function. Activation function just transforms our result for example suppose we have received 2.8 as a result then activation function may make it to 0.6 or something else, so it keeps the value smaller which is easy to compute.
In this Image,
Input : [x1,x2,x3,x4]
Weight matrix : W matrix
Bias Vector : [b , b , b]
And [a1, a2, a3] is the output after activation.
So this was our Computational Part of Neural Network , Now we will see the Implementation part of Neural Networks ( In Python ).
Implementation of Neural Nets
Now we will Implement Digit Detection Neural Net model to detect digits from 0 to 9, we will use tensorflow for this.
First you should look at our dataset of digits,
Data Visualization
We have given 28×28 Gray scale Images and we have to predict what digit on those images.
Importing Essential Libraries,
Now we will Download dataset of MNIST images and them we will merge last two dimensions to single dimension,
So the shape of data is something like (N,28*28).
Now we will convert the shape of labels because by default tensorflow need labels in one hot vector, ( if you don’t understand it, you can just copy the code)
So till this point, we have completed all data pre-processing tasks, Now let’s start model Architecture.
In this architecture, we have created an object on Input which will take input from data and it represents the input layer, then we have created a hidden layer which is the Object of Dense class, units in the hidden layer are the size of the hidden_layer’s output.
Then finally there is an output layer sized 10 because we need 10 outputs 0-9, which are the probabilities of each digit.
Now, we have to build a tensorflow model from this Architecture, it is pretty easy.
The Model class takes two arguments, input list and output list, then it will create a Graph which is built by using a doubly linked list.
To assign a loss function and optimizer which will minimize the loss, we have to compile this tensorflow model.
After this we are ready to train this model with some data, so lets train it.
This will take some time but you can continuously see the progress of training.
On my machine the output was like this,
Congratulations, you have achieved 94% Accuracy of the MNIST train-dataset. But we haven’t finished yet, we have to calculate accuracy on the test set because that will be the real measurement.
The output of this cell is,
So we have achieved 93.7% Accuracy on our test set in just a couple of python code lines. you can also check the prediction on your own image by model.predict() method.
Article By : Gajesh Ladhar
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other technical and Non Technical Internship Programs