Build your first Deep Learning Basic model using Keras, Python and Tensorflow step by step approach
By: Akash Deep
Tags: Python, Keras, Optimizer, TensorFlow, Deep Learning

The above code states that we have 1 hidden layer with 2 neurons. The no of neurons we used to specify as a unit and we used to pass as a parameter in the created layer in keras. Similarly for output layer we had specified unit as 1 that states that we have 1 neurons in the output layer. After creation of the hidden layer and output layer we generally build the model by passing the hidden layers and output layers as a parameter in Sequential API. If you want to know more about the Keras i will suggest just go through my earlier bogs i had explained it step by step.

We had understood the Dense layer. Now will try to understand what exactly this dense layer is doing in the network. To understand this will try to recall the the concepts that we had studied in the previous article, the concept of weights and biases. Consider we have x1, x2, x3 as the real input, a1 and a2 are the neurons at hidden layer and a3 are the neurons at output layers, in the previous neural network that we had discussed. As we discussed in fully connected layer each neuron is connected to the neurons of the previous layer so there will be weights associated with all the hidden and output layers so we will have 8 weights, 3 for 1 hidden layers a1 as we have 3 inputs and 3 for other hidden layers a2 and 2 for output layer as we have 2 hidden neurons. This can be understood by looking the below figure. During the execution of neural networks the weights and biases will be getting adjusted to best mach for the given input to the output. In the process of training the neural network only the weights and biases gets changes to fit the best value to fit the input and output that we used to feed the neural networks.

Weights and biases in neural network

We understood the basics of the dense layer and the neurons, how these neurons are connected with each other. now we are good to go towards building of our first Deep learning model using the Dense layer of Keras.

We are aware of the example that we need to build a model which will learn to give the Fahrenheit when the model will be given the degree in Celsius. For quick refresher below are the formulae for the same.

f=c*1.8+32

f is Fahrenheit and c is Celsius. This looks like a straight line equation. So we will be using a type of linear regression to approach this problem. Dont worry as of now if you are not aware of Linear regression we will cover the types of Machine learning along with the approach sometime in the next article. Of course it would be simple enough to create a conventional python function that directly performs this calculation but our motive here is to solve the same problem using the deep learning

To approach this problem we definitely need input and output. So in this case the input would be the some sample celsius value and the output will be some Fahrenheit value corresponding to the input celsius value and the end goal will be our model will learn the weight close to 1.8 and bias close to 32, so that if anybody will pass the celsius value our model should be in position to give Fahrenheit value corresponding to that celsius value. Try to understand the this very clearly.

Now we will be directly moving towards the building of first model using Tensorflow, Keras and Python. To build the model we will be using below code. First we will import tensorflow, numpy, logger and basic that we will need to build our model

import tesnsorflow, numpy and logger

Now we will prepare input and output. As we discussed input will be some sample of Celsius value and the output will be corresponding Fahrenheit value. Input will be celsius_q=[-40, -10, 0, 8, 15, 22, 38] and output will be fahrenheit_a=[-40, 14, 32, 46, 59, 72, 100].

Input and output

In the above example we have celsius as the feature and Fahrenheit as label if we will talk in deep learning terminologies.

Now, its time to create our model, as we had already specified the input and corresponding labels. We will be creating a model with the single dense layer. After creation of dense layer we will assign that layer to the model. For this we will use the below python code.