What Is Neural Network | Beginners Guide

Hi everyone, in this post I will be introducing you about neural network, how it works and how you can create your own neural network, Many of you already seen or heard what amazing things people are doing using neural networks, many of you already know the theory also, but struggling in practical implementations.

In one of my YouTube tutorials I showed how you can implement a neural network from scratch without using any modules or external machine learning libraries. So check that if you want a deep level understanding of neural network.

But In this post I am going to discuss on how to instantly build a neural network model and get started with it almost with no efforts.

So let’s get started

Initial Setup

We are going to use a python library called “Keras” which is a wrapper class for neural network library “theano/tensorfow”. In this tutorial we are going to use theano as backend library for Keras, Which is available for Win/Linux/Mac so that everyone can follow along.

Lets set it up

Open your terminal (cmd for windows), and type “pip install theano” install the theano python package, and then hit “pip install keras” and now we have all the packages we need for this tutorial.

Some Things To Check

Before proceeding we have to make sure that keras is indeed trying to use theano as back-end not tensorflow as we have installed theano only

to check that we need to check a file which is present at<user>/.keras/keras.json

for windows user “.keras” folder is visible and can be opened using directly using windows explorer

for linux/mac user use terminal to open the file type “nano ~/.keras/keras.json” this will directly open the file in the terminal using nano editor.
make sure the file look following

That’s all for the setup. We can now start coding our very won neural network.

Design First

First lets talk about our design of the network, In this tutorial we are going to keep things simpler. So we are creating a super simple design, We will make more complex designs in our later tutorials.

It’s always better to have some visual understanding of what we are going to do. So the design looks as in the below image.

As you can see we are going to have 3 layers 1st one is called the input layer and last one is called the output layer. what ever in between is called hidden layer(s). In this case we are going to create input layer with 2 input neurons and one hidden layer with 5 neurons and 1 output neuron in output layer.

We will start by importing keras libraries. To build the network he need two modules, 1st is model which will contain the network structure, We will be using “Sequencial” which is the most common. and “layers” which will be the building block of the network. in this simple tutorial we are going to use Dense layer which is the basic layers that we have discussed previously in the diagram. And we need numpy also.

Lets Define The Model

Now that we have all the components, we need to define the model structure as discussed in the design section.

we will be having 1 input layer one hidden layer and one output layer, So it looks like this.

model = Sequential()

In the above code we first initialized a Sequential model, called “model” Now we just have to attach layers like a layers in a sandwich, We start with adding the first hidden layer and there we also define the number of input neurons and number of neurons in the layer.

model.add(Dense(5, input_dim=2, activation='tanh'))

We don’t have to create the input layer, The model will create it automatically. So we Start with first hidden layer. It’s a Dense layer and we just need to specify the input dimension (number of input neuron) of the input layer. the hidden layer itself contain 5 neurons, and we need an activation function for each layer (check out my neural network from scratch for more detailed overview), we are using ‘tanh’ function as activation function.

model.add(Dense(1, activation='tanh'))

Now we will added the output layer. we don’t have to worry about input dimension in this as it’s not the first layer in the sequence. and this is the output layer and contain only one output, with “tanh” as activation function.

Compile The Model

What do I mean by compile the model. Actually in the above steps we just defined the structure of the model. next we will compile the model where the library will actually build the network according to the instruction we gave, as most optimised form as possible

Training The Model

Now that we have prepared the training set, we can start training the Model

model.fit(input,output,shuffle=True, nb_epoch=200, batch_size=4)

In the above code “nb_epoch” indicates how many times we want to run the total set again, “batch_size” is the number of samples to train in one shot. rest of the parameters are self-explanatory. Now we can test out results.