Keras Tutorial

About Keras

Keras is a python deep learning library. The main focus of Keras library is to aid fast prototyping and experimentation. It helps researchers to bring their ideas to life in least possible time.

Keras with Deep Learning Frameworks

Keras does not replace any of TensorFlow (by Google), CNTK (by Microsoft) or Theano but instead it works on top of them. Infact, Keras needs any of these backend deep-learning engines, but Keras officially recommends TensorFlow.

Keras & Python Version Compatibility

Keras is compatible with Python2 (starting from v2.7) and Python3 (till version 3.6).

Features of Keras library

  1. Keras is an user friendly API. It has consistent and simple APIs. For regular use cases, it requires very less of user effort.
  2. Keras gives a very useful feedback about user actions in case of any error. It provides with the actionable feedback which helps developers to pinpoint the line or error and correct it.
  3. Keras does not require separate configuration files for models. You can describe the model configuration in Python code itself.
  4. Keras can run seamlessly on both CPU and GPU with required libraries installed.
  5. Keras is extensible, which means you can add new modules as new classes and functions.
  6. When it comes to support for development with Keras Library, Keras provides good number of examples for the existing models.

Install Keras

With this little introduction to Keras, let us now get started with development using Keras library.

Lets not complicate any of the configurations and take things smoothly. To do that, we shall install TensorFlow first, because Keras will use TensorFlow, by default, as its tensor manipulation library.

To install TensorFlow on your machine, go to [https://www.tensorflow.org/versions/] and click on the latest stable release available. In the left menu, you will see a link for installation steps. Example url would be [https://www.tensorflow.org/versions/r1.9/install/]. Identify your OS and follow the respective steps.

Or if you have pip already installed, just run the following command :

With TensorFlow installed, now its time to install Keras.

Install Keras from PyPI (recommended)

If you are using a virtualenv, you may want to avoid using sudo:

If you would like experiment with the latest Keras code available there, clone Keras using Git

and install it using python

Basic Example Using Keras Library

Following is a basic example to demonstrate how easy it is to train a model and do things like evaluation, prediction etc. Do not worry if you do not understand any of the steps described below. It is meant only for introducing development with Keras to you. We shall go in deep in our subsequent tutorials, and also through many examples to get expertise in Keras.

Import from Keras

Sequential() is a simple model available in Keras. It adds layers one on another sequentially, hence Sequential model. For layers we use Dense() which takes number of nodes and activation type.

Dataset

We shall consider a csv file as dataset. Following is a sample of it containing three observations.

First eight columns are features of an experiment while the last(ninth) column is output label.

You can download the dataset from here.

In this example, we shall train a binary classifier. Output labels are either 1 or 0.

Model

Now, we define model using Keras Sequential() and Dense() classes.

The code is simple and easy to read. We created a Sequential() model and added three Dense() layers to it. The first Dense layer consists of 10 nodes, each node receives input from eight input nodes and the activation used for the node is relu (rectified linear unit). The second layer has 5 nodes and the activation function used is relu. The third layer is our output node and has only one node, whose activation is sigmoid, to output 1 or 0. So, apart from input and output, we have two layers in between them. You can add some more layers in between with different activation layers. The selection has to be done by considering type of data, and can also be done on a trail and error basis.

Compile and Fit the Model

During compilation, we specify how the error has to calculated and what type of optimizer has to be used to reduce that error, and what are the metrics we are interested in.

Fitting builds the compiled model with the dataset. During fitting, we specify the number of epochs (number of reruns on the dataset) and batch_size.

Fitting the model takes some time. 150 Epochs has to be completed and once done, our model is trained and ready.

Evaluation

We can evaluate the build model.

During model compilation, we added accuracy as a metric, along with the default loss metric.

Complete Python Program – Keras Binary Classifier

Consolidating all the above steps, we get the following python program.

Conclusion

In this Keras Tutorial, we have learnt what Keras is, its features, installation of Keras, its dependencies and how easy it is to use Keras to build a model with the help of a basic binary classifier example.