Build a neural network in 9 lines of code

You must have heard about deep learning once and you might felt curious about how to implement deep learning. The first thing that comes to me is a Neural Network whenever I hear the word deep learning. Now, what is a neural network, and what is deep learning?

Let's understand a little about them and then without taking much time, we will build our Neural network in just 9 lines of code.


Personally, I call it The Hello World of Deep Learning with Neural Networks.

If you want to work on some projects, kindly refer to this link:

MACHINE LEARNING PROJECTS


Neural Network: In the easiest terms, we can understand neural networks as the advanced version of Machine learning algorithms. 

Neural networks work in the same way as the human brain. They find the underlying pattern that exists with the data.

RELATED POST: UNDERSTANDING MACHINE LEARNING ALGORITHMS


Deep Learning: Deep learning is a subset of Machine Learning. It has the capability to learn unlabeled data very easily.

There are many algorithms in Machine learning which fails when there is a huge amount of unlabeled data. Deep learning provides the best accuracy when we have a huge amount of either labeled or unlabeled data.

You can learn more about machine learning algorithms to understand them better provided in the above link.

Let's begin modeling.


Here, we will build a very basic neural network without any convolutions to understand how does it work and to learn how to get started with Deep Learning and Neural networks.

Step 1: Importing necessary libraries.






Tensorflow: It is created by the Google Brain team. It is a symbolic library which is used to deal with Neural Networks. It is a combination of few machine learning and deep learning algorithms which makes computation much easier.

KERAS: In simple words, it is an API for the Tensorflow library in Python.
Now after importing the above libraries, let's move on to the next step.

Step 2: Creating a neural network.




It is the simplest neural network with only 1 neuron. It consists of only a Dense layer
Sequential is the easiest way to build a neural network layer by layer. We can easily add more layers to our neural network.
After successfully creating a neural network, let's move on to the next step.

Step 3: Compiling a neural network.



There are certain functions used while compiling the model. These functions work together simultaneously.

Parameters of compile function:

  • loss: It is the duty of a neural network to calculate how much the loss occurred while guessing the relationship with the data. So, 'mean_squared_error' measures the loss occurred and tells the loss to the optimizer.
  • optimizer: The optimizer used here is sgd - Stochastic Gradient Descent. The work of optimizer is to map the relationship correctly in order to reduce the loss that occurred earlier.
  • metrics: If you want to see the accuracy of the model then you can use metrics=['accuracy'] as the additional function in the compile function.

Now as the model is compiled, our neural network is ready to be trained. Now you must be wondering where is the data. So, to make it easier to understand, we will use simple Numpy arrays as our dataset.

Step 4: Preparing data.




It would be good if you will try to find out the relationship between data yourself first. 
Look closely, you will see that there is a relationship mapping y=2x-2
It is the work of the optimizer function to map the values as closely as possible.

Now let's move to the final step.

Step 5: Fitting the Neural Network.



This is the phase that we had talked about above. Here, the model will guess a relationship such as y=3x-5, calculate the loss, and then the optimizer will make another guess based on the loss calculated by mean_squared_error. It will continue to happen until the training is completed.

xs and ys are data values. 

Epochs: It is the function that specifies how many times you want the model to learn the values. You can increase or decrease the value of epochs to find the difference between loss occurred.
Here, once the model learns values 150 times, it will stop fitting the values.

One thing to keep in mind is that don't use too many epochs like 500 or more. Epochs vary with the underlying datasets. Here, if you will use epochs more than 500, there is a high chance that the loss stops to decrease and become a constant value
Sometimes, too many epochs lead to overfitting.


Now we are done with fitting the model. It is time to test our neural network. But before testing I want you to calculate yourself what would be the value of y when x=10? 




Yes, according to our equation y=2x-2, y would be 18.

You must be wondering why not 18?

You can see that the model has predicted value as 17.968 but now exactly 18. 

Neural networks deal with probabilities. We have only provided 6 data points and it almost predicted it correctly. There must be a chance that the relationship would not be y=2x-2. It may change to y-2x-1 when we use 100 data points instead of 6 as used here. Therefore, Neural networks deal with probabilities.

Source Code

Here is the complete code for you to practice. You have successfully build a neural network in just 9 lines of code.



Hope it helped you to understand the basics of a Neural Network. Kindly subscribe to the Telegram channel for more updates.

Soon I will be uploading a complex neural network with convolutions and models explaining overfitting and other parameters as mentioned in this blog.

Kindly give your choice for the next blog to serve you better. Next Blog?

Thanks!

Post a Comment

0 Comments