googleads
How To Build A Simple Neural Network In 9 Lines of Python Code
preloder
Guide For Freshers Python & AI Tips & Tricks

How To Build A Simple Neural Network In 9 Lines of Python Code

In this post, You going to learn how you can easily build a Neural network with just 9 lines of Python code. If you are new to this subject, I highly recommend you to get a basic understanding of Deep Learning.

Let’s get started!

What is a Neural Network?

A series of algorithms, specially designed to recognize patterns. The sensory data is interpreted through machine perception. It may be clustering or labeling raw inputs.  Numbers, vectors, such patterns are interpreted into real-world data. Images, sound text, time series must be translated.

The link between two nodes is called a Synaptic link. Initially, the input data will find the right link to provide the output node which is called “Thinking”(sense). 

Learn how it is made mathematically from here.

We’re going to build Neural Network with fewer data. 

Data for Training

Three inputs and one output accordingly, We have four sets of I/O data is below, 

  1. 0 0 1 0
  2. 1 1 1 1
  3. 1 0 1 0
  4. 0 1 1 1

Input for the Test

  1. 1 1 0 ?

Now, We have the sense to find the output for E 

How?

As you can see the output of the training data is just as same as the middle of input data but multiple combinations of inputs are also there. Now let’s take a look with the lines of code, 

Line #01

For matrices calculation, we call the package numpy in python language.

You can also see a single node(neuron) mathematical computation from here.

But how to give a sense for our model with the correct answer?

  1. Get the Training data as input(train_i) and do the dot matrics with the random link(synapses) weight(link_w) to find the output by sigmoid activation function (output)
  2. Calculate the error with the simple mathematical operation.
  3. The output is delivered.

Training I/O data

Line #02

Line #03

You have to give the link_w(link weight) to the input node, which can be either a positive or a negative number. Each node-link with the next layer along with a random number is link_w(synapses). 

To generate random weight using a random function (numpy).

Line #04

Line #05

Now, create the initial link_w(synapses)  with random numbers both negative and positive.

Line #06

Line #07     

Line #08    

Using the Dot product in matrices, you should do a basic calculation to find the next layer with the sense node(neuron). Here is how you can do it.

1. dot(train_i, link_w)2. 1 / (1 + exp(-(dot(train_i, link_w))))

3. output = 1 / (1 + exp(-(dot(train_i, link_w))))

The Output is 11+e-( Wi.Ii)

Output for the next node(neuron) repeats iteration to make sense to identify the correct answer (output) by calculating an error.

Adjust weight depends on error.

In numpy ‘.T’ function represents, transposes the matrix so our input should be like this for calculation.

INPUTOUTPUT
0 0 10
1 1 11
1 0 10
0 1 11

 

Line #09

Output – 0.98807249

Result of input test data – 1 1 01

Congratulations on getting this far! Happy learning.

Agira provides various Mobile and Web Development Services. You can hire our dedicated Developers to transform your Business.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
The following two tabs change content below.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call