Keras
Keras
Library
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 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 is compatible with Python2 (starting from v2.7) and Python3 (till version 3.6).
Install Keras
With this little introduction to Keras, let us now get started with development using Keras library.
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 :
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
$ cd keras
$ sudo python setup.py install
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.
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.
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.
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
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
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.
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'])
Fitting the model takes some time. 150 Epochs has to be completed and once done, our model is trained and
ready.
Evaluation
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.
Consolidating all the above steps, we get the following python program.
Python Program
# load dataset
dataset = numpy.loadtxt("input-data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
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')
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.
Deep Learning
Environment Setup
✦ Install Anaconda
TensorFlow Tutorial
Theano Tutorial
Keras Tutorial
➩ Keras Tutorial