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.

ADVERTISEMENT

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 :

$ sudo pip install tensorflow

With TensorFlow installed, now its time to install Keras.

Install Keras from PyPI (recommended)

$ sudo pip install keras

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

$ pip install keras

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

$ git clone https://github.com/keras-team/keras.git

and install it using python

$ cd keras
$ sudo python setup.py install

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.

from keras.models import Sequential
from keras.layers import Dense

Dataset

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

6,148,72,35,0,33.6,0.627,50,1
1,85,66,29,0,26.6,0.351,31,0
8,183,64,0,0,23.3,0.672,32,1

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

You can download the dataset from here.

import numpy

# load dataset
dataset = numpy.loadtxt("input-data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]

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.

model = Sequential()
model.add(Dense(10, input_dim=8, activation='relu'))
model.add(Dense(5, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

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.

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Fit the model
model.fit(X, Y, epochs=150, batch_size=10)

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.

scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

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.

Python Program

from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
numpy.random.seed(7)
print('random seed set')

# load dataset
dataset = numpy.loadtxt("input-data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
print('input data loaded')

# create model
model = Sequential()
model.add(Dense(10, input_dim=8, activation='relu'))
model.add(Dense(5, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
print('model created')

# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print('compiled')

# Fit the model
model.fit(X, Y, epochs=150, batch_size=10)
print('data fit to model')

# evaluate the model
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

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.