Open Source For You — December 2017

(Steven Felgate) #1
How To Developers

http://www.OpenSourceForU.com | OPEN SOURCE FOR YOU | DECEMBER 2017 | 63

Figure 1: Primary reasons for using Keras


Instant Prototyping

CPU and GPU Support

Python at Core User Friendliness

Modular Design

# 3. Fit Model

# 4. Perform Prediction# 2. Compile Model

#1. Define Model

Extensible

Support for Convolutional, Recurrent Network

Keras - Top Reasons

Keras - Design Philosophy

Keras Flow

Figure 2: Keras’ design philosophy

Figure 3: The sequence of tasks

ƒ Keras is extensible. If you are a researcher trying to bring
in your own novel functionality, Keras can accommodate
such extensions.
ƒ Keras is all Python, so there is no need for tricky
declarative configuration files.

Installation
It has to be remembered that Keras is not a standalone
library. It is an API and works on top of existing libraries
(TensorFlow, CNTK or Theano). Hence, the installation of
Keras requires any one of these backend engines. The official
documentation suggests a TensorFlow backend. Detailed
installation instructions for TensorFlow are available at https://
http://www.tensorflow.org/install/. From this link, you can infer
that TensorFlow can be easily installed in all major operating
systems such as MacOS X, Ubuntu and Windows (7 or later).
After the successful installation of any one of the backend
engines, Keras can be installed using Pip, as shown below:

$sudo pip install keras

An alternative approach is to install Keras from the source
(GitHub):

#1 Clone the Source from Github
$git clone https://github.com/fchollet/keras.git

#2 Move to Source Directory
cd keras

#3 Install using setup.py
sudo python setup.py install

The three optional dependencies that are required for
specific features are:
ƒ cuDNN (CUDA Deep Neural Network library): For
running Keras on the GPU
ƒ HDF5 and h5py: For saving Keras models to disks
ƒ Graphviz and Pydot: For visualisation tasks

The way Keras works
The basic building block of Keras is the model, which is a
way to organise layers. The sequence of tasks to be carried
out while using Keras models is:

ƒ Model definition
ƒ Compilation of the model
ƒ Model fitting
ƒ Performing predictions
The basic type of model is sequential. It is simply a
linear stack of layers. The sequential model can be built as
shown below:

from keras.models import Sequential
model = Sequential()

The stacking of layers can be done with the add() method:

from keras.layers import Dense, Activation
model.add(Dense(units=64, input_dim=100)) model.
add(Activation(‘relu’))
model.add(Dense(units=10))
model.add(Activation(‘softmax’))

Keras has various types of pre-built layers. Some of the
prominent types are:
ƒ Regular Dense
ƒ Recurrent Layers, LSTM, GRU, etc
ƒ One- and two-dimension convolutional layers
ƒ Dropout
ƒ Noise
ƒ Pooling
ƒ Normalisation, etc
Similarly, Keras supports most of the popularly used
activation functions. Some of these are:
ƒ Sigmoid
ƒ ReLu
ƒ Softplus
ƒ ELU
ƒ LeakyReLu, etc
Free download pdf