Introduction
Deep learning is one of the subsets of machine learning.
Nowadays deep learning is becoming more and more popular in the field of image detection, recognition, audio/video recognition, NLP, biometric, image segmentation, object detection, object distancing, and many more booming applications. To implement artificial neural networks for all these applications we can use libraries like Tensorflow, Theano, and PyTorch. But the easiest library to implement Neural Networks is Keras.
Keras is a high-level API to create artificial neural networks over Tensorflow, Theano, CNTK, etc. which is written in Python.
Fig 1. Keras blocks
Keras installation:
Keras is a high-level API. It requires some backend engine to run. So before installing Keras install one of the backend libraries like TensorFlow, CNTK, Theano etc.. I am here focusing on Keras so not describing how to install other backend libraries.
Install Keras with PyPI:
“sudo pip install keras” |
For Linux or Mac:
“pip install keras” |
For Windows:
“git clone https://github.com/keras-team/keras.git” “cd keras” “sudo python setup.py install” |
Install keras from GitHub source:
TensorFlow vs.Keras(with tensorflow in back end)
Actually comparing TensorFLow and Keras is not good because Keras itself uses tensorflow in the backend and other libraries like Theano, CNTK, etc. So we can say that Kears is the outer cover of all libraries. The logic behind keras is the same as tensorflow so the thing is, keras are just wrapping of tensorflow logic with fewer lines of code. Let’s now compare tensorflow and keras code. Code for the same model in tensorflow and keras are as follows.
Model building with TensorFlow:
Let’s build a model for the Boston housing prices data set which has 13 features.
You can download the full code
In this code snippet x is input feature .y_ is actual output, W is weights and b is bias and shape of input feature is [None,13] which means all input rows of 13 features. Now let’s consider W weights having shape [13,1 ] which means there are 13 weights and as result it gives only 1 output. There is only 1 bias. Number of biases is equal to number to output, so the shape of that variable is [1].
Here y is prediction. In this first we do matrix multiplication of x & W, after that we added bias b into the result of that matrix multiplication which gives predicted target value. Here shape of x is [None,13] and W is [13,1], So after matrix multiplication shape of target value is [None,1] where None means all the data rows and after adding bias in this which has shape [1] gives predicted value with shape shape of [None,13].
Next step is to define loss. In this first of all find MSE, for that first find difference between predicted and actual output and calculate square of that. And so reduce_mean which is MSE. We can say this as a loss of the model.
At last apply gradient descent optimizer with learning rate of 0.03 and try to minimize loss by optimizer.
Model building with Keras(with TensorFlow backend):
I am going to use the same data set of Boston housing prices.
With just this 4 line of code, the model is built. Here tf.keras means Keras with TensorFlow in the back end. The 1st line of code generates a Sequential model. The 2nd line of code generates the Dense layer(/hidden layer). Here we generate only 1 Dense layer where Keras automatically declares weights and bias. SGD is Stochastic gradient descent if we do not define optimizer learning rate then it will take 0.01 as default we can change by defining optimizer learning rate. And at the end compile the model by giving the optimizer and loss function which we want to use, here I use the SGD optimizer and MSE loss function.
Model execution with TensorFlow:
Execute already built model for Boston housing prices data set.
To execute any model with tensorflow first you need to start a session. The 2nd line of code in the above code snippet is to initialize the global variables which we use at the time of building the model. And epochs are number to time we need to give training data to the model for best accuracy and less loss.
Output:
In the above code snippet ,execute model and train that model for 100 epoch and get minimum loss at the last epoch. After model training we must close the session which we started, with sess.close().
Model execution with Keras:
Execute already built model for Boston housing prices data set.
Output:
To execute a model with Keras just needs one line of code. Where we should give features, actual_prices, and number of epochs. The output is also there in the above snippet and gets minimum loss at the last epochs.
This is how we can build and execute models with Keras efficiently and less coding compared to complex code in TensorFlow.
Subscribe To Our Newsletter
Join our mailing list to receive the latest news and updates from our team.
You have Successfully Subscribed!