Keras Tutorial
What is Keras?
Keras is an open-source library which is written in python language. It is designed to enable fast experimentation with the deep Neural Network. Keras is user-friendly, modular, and extensible deep learning framework. It is also a high- level neural network API which can wrap the low-level API.
The Keras supports multiple backend neural network computation engines. It is not able to handle the low-level computation. It uses another library for this purpose. The Keras is designed to be modular, faster, and easier to use. Keras also compiles our model with loss and optimizer functions, training process with proper function.
Figure: Concept of Keras.
The Keras supports multiple backend engines. Its primary backend is Tensor Flow, and its primary supporter is Google. If we want to change the backend in Keras edit our $HOME/.Keras /Keras.json file and specify the different backend name such as Theano or CNTK. The backend is a term in Keras that performs all low- level computation such as Tensor products, convolution.
History of Keras
The "François Chollet developed the Keras." He is currently working as an AI researcher at Google. He is also working at the core of Keras development. Keras is a powerful and easy-to-use, python library for developing and evaluating deep learning models. It wraps the efficient numerical computation libraries: Theano and Tensor Flow. It allows us to define and train neural network models in just a few lines of code.
The Keras is the high –level API of Tensor Flow. It is used in building models, and it is as simple as stacking layers and connecting graphs. The Contributors across the world actively develop Keras. It has a large amount of documentation.
The high-performance API used to specify and train differentiable programs. The Keras has over 4,800 contributors during its launch and initial stages.
Nowadays, the number has gone up to 250,000 active developers. There is a 2x growth every year since it has launched. Keras also holds a perfect amount of traction among multiple startups; big players like Microsoft, Google Nvidia, and Amazon actively contribute to the development of Kears.
Who uses Keras
The Keras has incredible industry grip, and it is used in the development of famous firms like Netflix, Uber, Expedia Yelp, and more. So, if Keras is getting all this attention, what makes it unique and what makes Keras stand out among the top frameworks. There are some snippets of Keras user experience which are given below:
- Keras follows best practices for reducing cognitive load.
- It offers consistent and simple APIs.
- Keras minimized the number of user actions required for common use cases.
- It provides clear feedback upon user error.
- Keras is easy to learn and easy to use.
- It integrates with lower-level with deep learning languages like Tensor Flow.
- Keras can implement anything which was built in the base language.
Features of Keras
Here, top ten features are given that makes Keras so special:
1. The focus on user experience has always been a major part of Keras.
2. Broad adoption in the industry and we just checked out all the industry pull it gets, and this holds well.
3. Keras is multi-backend and supports platform as well. It helps all the coders and codes quickly.
4. The research community present for Keras is impressive, along with the production community.
5. All the concepts are straightforward to grasp with Keras.
6. The Keras has fast prototyping.
7. It runs flawlessly on CPU (Central Processing Unit) and GPU (Graphics Processing Unit) both.
8. It has the freedom to design any model's architecture and then implement it as an API for the case of our projects. This is a major advantage of Keras.
9. The Keras is simple to all the beginners to getting started with.
10. The easy production of models makes Keras so much unique.
Keras vs. Tensor Flow
The Keras is a neural network library while Tensor Flow is an open-source library for several tasks in machine learning. The Tensor flow provides both high-level and low-level API, but the Keras provides only high-level APIs. Keras is built in python; that's why it is more user-friendly than Tensor Flow.
The TensorFlow is an end-to-end open-source platform of machine learning. It is the comprehensive and flexible ecosystem of tools, libraries, and other resources which provides workflow with high-level APIs. This framework offers several levels of concepts for us and chooses according to the user’s need to build and deploy the machine learning model.
If we require to do some large machine learning tasks, we can use the distributed strategy API to perform distributed hardware configuration. If we need a full production machine learning pipeline, we can use TensorFlow extended for this purpose.
There are some features of TensorFlow, which are given below:
1. Easy Model Building
The Tensor Flow offers multiple levels of abstraction to build and train the models of machine learning.
2. Robust ML Production Anywhere
We can train and deploy our model quickly, no matter what language or platform we use.
3. Powerful Experimentation for Research
TensorFlow gives us flexibility and control with features. It can model the subclassing API for the creation of complex topologies.
On the other hand, the Keras is a high-level neural network library that is running on the top of TensorFlow, CNTK, and Theano. The Keras is written in python code that is easy to debug and allows for the extensibility.
Keras Installation
Step1: Go to the official website of the Anaconda, www.anaconda.org, and download the Anaconda set up.
Step2: Click on the Run button to install the Anaconda.
Step3: Click on the next button to move forward.
Step4: Click on I Agree Button, after reading the terms and conditions.
Step5: Click on the Next button.
Step6: Select the destination folder where the user wants to install Anaconda and click the next button.
Step7: Click on the install button for further processing.
Step8: The installation of Anaconda is started.
Step9: The Anaconda is installed, click on the finish button.
Step10:
After installation of Anaconda, we need to launch the Anaconda Prompt.
Step11: Now, we need to type the given command on the Anaconda prompt.
>python - -version
Step12:
We need to install the Jupyter notebook with the following command, which is given below:
>Conda install jupyter
Step13:
After the installation of jupyter, we should install the Scipy by writing the given command on the Anaconda prompt.
>conda install scipy
Step14:
Now, we need to write the given command on the Anaconda prompt to install the sklearn package.
>pip install sklearn
Step15:
Here, we install the panda's package by written this command:
>pip install pandas
Step16:
Now, we are going to install the data readers that have given access to some data when the user needs. The command is given below:
>pip install pandas data-reader
Step17:
Install matplotlib by using the given command:
>pip install matplotlib
Step18:
Now, install the pillow to give access to graphic files:
Step19:
Install requests which provide access to web services, and the given command is used to install the request.
>pip install requests
Step20:
Now, we need to install h5py, and it is binary file so that the TensorFlow can load and save binary files. The command of this binary is given below:
>pip install h5py
Step21:
Now, we require installing the TensorFlow in Keras; the command for this purpose is given below:
>pip install tensorflow==1.2.1
Step22:
After the installation of TensorFlow, we can install Keras directly by using the given command:
>pip install keras== 2.0.6
Step23:
The Keras is successfully installed in this step.
Advantage
1. Fast and Easy development
The Keras is very fast to make a network model. If we want to design a simple network model by using a few lines, so Keras framework is the best option for creating short lines of code for any model. The code is given below:
from keras.models import Sequential from keras.layers import Dense, Activation model = Sequential() model.add(Dense(64, activation = ‘relu’, input_dim=50)) # input shape of 50 model.add(Dense(28, activation = ‘relu’)) #input shape of 50 model.add(Dense(10,activation= ‘softmax’))
2. Large Community Support
There are lots of Artificial Intelligence communities which uses Keras. We can select TensorFlow, CNTK, and Theano as our backend with Keras. User can also choose the different backend for different projects, which depends on his need. Each backend has a unique benefit.
3. Cross-Platform
Keras supports a variety of devices and platforms. We can deploy it on any device which is given below:
- iOS with core Machine Learning.
- Android with TensorFlow
- The web browser with .js support
- The cloud engine
- The Raspberry Pi
4. Multiple GPUs support
We can use single or multiple GPUs simultaneously in Keras. It has built-in support to the data parallelism so, the Keras can process the large volume of data and speed up the time which needed to train the model.
Disadvantage of Keras
1. Cannot handle low-level API
The Keras can handle only high-level API which runs on the top of other framework or backend engines such as TensorFlow, Theano or CNTK. Keras is beneficial if we want to make our abstraction layer for the research purpose because Keras already have pre-configured layers.
Implementation of Convolutional Neural Network in Keras
The Keras is simple to use and powerful deep learning library. We can build the simple Convolutional Neural Network and train that Network to solve the real problems with Keras. There are some steps to create the Convolutional Neural Network in Keras which are given below:
1. Set-Up
We must have a basic python installation then, after that we need some packages. The syntax is given below:
$ pip install Keras tensorflow numpy mnist
We also need to install the tensorflow because we are going to run the Keras on TensorFlow backend. Now, after the installation of python and TensorFlow, we would be able to import these packages and poke around the MNIST dataset. The syntax of import the packages are given below:
import numpy as np import mnist import keras train_images = mnist.train_images() train_labels = mnist.train_labels() print (train_images.shape) # (60000,28,28) print (train_labels.shape) # (60000,)
2. Preparing the data to build the Convolutional Neural Network
Before beginning to build the Convolutional Neural Network, we need to normalize the image pixel values from [0,255] to [ -0.5, 0.5] to make the Network easily train. We also need to reshape every image from (28, 28) to (28, 28, 1) because the Keras requires the third dimension to work.
Syntax for preparing the data
import numpy as np import mnist train_images = mnist.train_images() train_labels = mnist.train_labels() test_images = mnist.test_images() test_images = mnist.test_labels() train_images = ( train_images / 255) -0.5 test_images = (test_images / 255 ) – 0.5 train_images = np.expand_dims(train_images, axis =3) test_images =np.expand_dims(test_images, axis=3) print (train_images.shape) #(60000, 28, 28, 1) print (test_images.shape) # (10000, 28, 28, 1)
Now, we are ready to build the Convolutional Neural Network.
3. Building the model for the Network
Each Keras model is built by using the Sequential class. This class represents the linear stack of layers or the functional model class. We initialize the CNN model via the sequential model, and the syntax is given below:
from keras.models import Sequential #WIP model = Sequential ([ # layers……………. ] )
The Sequential constructor takes an array of the Keras layers. We can use three types of layers for Convolutional Neural Network, which is given below:
1. Convolutional
2. Max Pooling
3. Softmax
The output of Softmax layer has ten nodes and one node for each class.
Keras Neural Network Sequential Model
The deep learning models are built by using neural networks. The Neural Network takes an input which is then processed in hidden layers using weights which are adjusted during the training of the model. The Keras is used for this purpose also because it is user-friendly Neural Network library written in python.
There are two ways in kears to build the models which are given below:
1. Sequential Model
2. Functional Model
The sequential API allows the user to create models layer-by-layer for most of the problems by using the strategy of sequential model. It does not allow the user to create models that can share the layers or have multiple inputs or outputs.
In the functional model, the functional API allows the user to create the models. These models have a lot of flexibility as the user can easily define the models where layers are connected to just the previous and next layers. User can connect the layers to any other layer in the functional model.
1. Sequential Model
The Sequential model API is used for developing the deep learning models in most situations, but it also has some limitations.
For example, the sequential API is not straight forward to define the models that may have:
A. Various dissimilar input sources.
B. Produce Several output destinations.
C. Models that can re-use the layers.
Example of the sequential model:
from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense (2, input_dim=1)) model.add(Dense(1))
In the above example, the layers are added piecewise through the sequential object.
2. Functional Model
The Keras functional API provides a more flexible way to define the models. This API allows the user to define multiple inputs or output models. The functional API also allows the user to define the ad hoc acyclic network graphs.
The models are defined by creating instances of layers and connects them directly with each other in pairs. The definition of the model specifies the layers to act as input and output of the model.
Example of the functional model
from keras.models import model from keras.layers import input from keras.layers import Dense visible = Input (shape = (2, )) hidden =Dense (2)(visible) model = Model (inputs = visible, outputs = hidden )
Keras vs. PyTorch
Pytorch and Keras both are very powerful open-source tools in Deep learning framework. The beginners are struggling to decide the framework to work with when it comes to starting the new project.
According to the recent survey, Keras and PyTorch have emerged as the two fastest-growing tools in data science. Here, is a step-by-step comparison of both tools with respect to various aspects, to help to decide what framework to use for user’s next deep learning project.
1. Support the system and language
The PyTorch is developed by the team of facebook researchers and supports windows, Linux, and OSX operating system. The Keras was developed by the Google engineer “Francois Chollet," and works only on the Linux and OSX.
2. Debugging
It is easier and faster to debug in PyTorch than Keras. The Keras has a lot of computational junk in its abstraction, and so it becomes hard to debug. The PyTorch allows easy access to code and easier to focus on the execution of the script of every line.
Debugging can be done by using the standard method by printing any line of the code. In Keras, the opportunity of the code going wrong is less than PyTorch, but it is not fully impossible. So, once the code goes wrong in Keras, it is very difficult to note that where the actual flaw was.
The PyTorch allows easy and direct error identification. In Keras, it is very difficult to find the cause of the error. While in dynamic libraries like PyTorch with a dynamic computational graph, it is easier to track which line is responsible for which error. User can even stop the interpreter in the debugger to see what layer produces the error.
3. Portability
It is very important to have control over the models in terms of RAM, CPU, and file loading while deploying them. The PyTorch saves its models based on python, which is not portable. The PyTorch exporting models are more difficult because of its python code; for this issue, the recommended solution is to convert the PyTorch model into Caffe2 by using ONNX.
4. Maintenance
The Keras was released on March 2015, and PyTorch was released on October 2016. The Keras has more support from an online community such as tutorial and documentation on the internet.
Keras also has many codes on GitHub and more papers on arXiv as compared to PyTorch. The PyTorch has also gained popularity than Keras, but it has comparatively less online support than Keras, which is slightly older. The PyTorch is maintained by the group of Engineers at Facebook and actively provide users with online support.
5. Visualization
The Keras can have the TensorFlow as its backend and has the Tensor board for its Visualization that can be used in Keras. There is nothing which is currently present in deep learning equivalent to PyTorch in this regard.
Both tools have very close competition, and the use of tool depends on the user's background, the tool requirement for the project and which programming language user wants to use.
If the user wants more understanding of the mathematics behind the models and keen to know what the model is actually, doing then, the Keras is highly recommended. On the other hand, if the user is more inclined towards the building, training, and deploying the model without giving much emphasis on understanding and the meaning of the model. In that case, PyTorch is a more suitable option.
Conclusion
The Keras is the python library, and the user can implement the Convolution Neural Network. The user achieved a test accuracy of 97.4% in simple initial Network with the help Keras. The Keras is generally the high-level Neural Network API. It is capable of running on top of the TensorFlow, CNTK or Theano. Keras is also enabled for very fast experimentations, runs seamlessly on both CPU and GPUs. There are several types of carrier opportunities exist in Keras. It is mostly used in Robotic Scientist, Machine Learning Engineering, and Data Scientist. The Keras is an emerging technology in the market also.