# Introduction To Keras: Tutorial and Implementation

In this article, I will take you through the Keras Tutorial and Introduction to its Implementation.

**What is Keras?**

Keras is a high-level neural networks API, written in Python and capable of running on top of **TensorFlow**, **CNTK**, or **Theano**. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.

It runs on Python 2.7 or 3.5 and can seamlessly execute on GPUs and CPUs have given the underlying frameworks.

**Guiding Principles**

**User friendliness.** Keras is an API designed for human beings, not machines. It offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear and actionable feedback upon user error.

**Modularity.** A model is understood as a sequence or a graph of standalone, fully-configurable modules that can be plugged together with as few restrictions as possible. In particular, neural layers, cost functions, optimizers, initialization schemes, activation functions, regularization schemes are all standalone modules that you can combine to create new models.

**Easy extensibility.** To be able to easily create new modules allows for total expressiveness, making Keras suitable for advanced research.

**Work with Python**. No separate models configuration files in a declarative format. Models are described in Python code, which is compact, easier to debug, and allows for ease of extensibility.

**The Advantage of Keras Over Tensorflow**

We see that Deep Learning frameworks operate at 2 levels of abstraction:

**Lower Level:**This is where frameworks like Tensorflow, MXNet, Theano, and PyTorch sit. This is the level where mathematical operations like Generalized Matrix-Matrix multiplication and Neural Network primitives like Convolutional operations are implemented.**Higher Level:**This is where frameworks like Keras sit. At this Level, the lower level primitives are used to implement Neural Network abstraction like Layers and models. Generally, at this level, other helpful APIs like model saving and model training are also implemented.

**You cannot compare Keras and Tensorflow because they sit on different levels of abstraction. I also want to take this opportunity to share my experience of using Keras:**

**I do not agree that Keras is only useful for basic Deep Learning work**. Keras is a beautifully written API. The functional nature of the API helps you completely and gets out of your way for more exotic applications. Keras does not block access to lower level frameworks.- Keras results in much more readable and succinct code.
- Keras model Serialization/Deserialization APIs, callbacks, and data streaming using Python generators are very mature.
- Keras has been declared the official high-level abstraction for Tensorflow.

**Installation of keras**

Before installing Keras, please install one of its backend engines: TensorFlow, Theano, or CNTK. We recommend the TensorFlow backend.

- TensorFlow installation instructions.
- Theano installation instructions.
- CNTK installation instructions.

You may also consider installing the following optional dependencies:

- cuDNN (recommended if you plan on running Keras on GPU).
- HDF5 and h5py (required if you plan on saving Keras models to disk).
- graphviz and pydot (used by visualization utilities to plot model graphs).

Then, you can install Keras itself. There are two ways to install Keras:

**Install Keras from PyPI (Recommended)**

1 2 |
sudo pip install keras |

if you are using a virtual env, you may want to avoid using sudo:

1 2 |
pip install keras |

**Alternatively: install Keras from the GitHub source:**

First, clone Keras using git:

1 2 |
git clone https://github.com/keras-team/keras.git |

Then, cd to the Keras folder and run the install command:

1 2 3 |
cd keras sudo python setup.py install |

**Getting started with the Keras (Sequential model)**

** **In this section, we will learn, how to use keras (sequential model) for building the deep learning Models.

The **Sequential** model is a linear stack of layers. You can create a **Sequential** model by passing a list of layer instances to the constructor:

1 2 3 4 5 6 7 8 9 10 |
from keras.models import Sequential from keras.layers import Dense, Activation model = Sequential([ Dense(32, imput_shape=(784,)), Activation('relu'), Dense(10), Activation('softmax'), ]) |

You can also simply add layers via the **.add()** method:

1 2 3 4 |
model = Sequential() model.add(Dense(32, input_dim=784)) model.add(Activation('relu')) |

**Specifying the Input Shape**

The model needs to know what input shape it should expect. For this reason, the first layer in a **Sequential** model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape. There are several possible ways to do this:

- Pass an
**input_shape**argument to the first layer. This is a shape tuple (a tuple of integers or**None**entries, where**None**indicates that any positive integer may be expected). In**input_shape**, the batch dimension is not included. - Some 2D layers, such as
**Dense**, support the specification of their input shape via the argument**input_dim**, and some 3D temporal layers support the arguments**input_dim**and**input_length**. - If you ever need to specify a fixed batch size for your inputs (this is useful for stateful recurrent networks), you can pass a
**batch_size**argument to a layer. If you pass both**batch_size=32**and**input_shape=(6, 8)**to a layer, it will then expect every batch of inputs to have the batch shape**(32, 6, 8)**.

As such, the following snippets are strictly equivalent:

1 2 3 |
model = Sequential() model.add(Dense(32, input_shape=(784,))) |

1 2 3 |
model = Sequential() model.add(Dense(32, input_dim=(784,))) |

**Compilation**

Before training a model, you need to configure the learning process, which is done via the **compile** method. It receives three arguments:

- An optimizer. This could be the string identifier of an existing optimizer (such as rmsprop or
**adagrad**) or an instance of the**Optimizer** - A loss function. This is the objective that the model will try to minimize. It can be the string identifier of an existing loss function (such as
**categorical_crossentropy**or**mse**), or it can be an objective function. - A list of metrics. For any classification problem, you will want to set this to
**metrics=[‘accuracy’]****.**A metric could be the string identifier of an existing metric or a custom metric function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# For a multi-class classification problem model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # For a binary classification problem model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # For a mean squared error regression problem model.compile(optimizer='rmsprop', loss='mse') # For custom metrics import keras.backend as K def mean_pred(y_true, y_pred): return K.mean(y_pred) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy', mean_pred]) |

**Training**

Keras models are trained on Numpy arrays of input data and labels. For training a model, you will typically use the **fit** function.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# For a single-input model with 2 classes (binary classification): model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # Generate dummy data import numpy as np data = np.random.random((1000,100)) labels = np.random.randint(2, size=(1000,1)) # Train the model, iterating on the data in batches of 32 samples model.fit(data, labels, epochs=10, batch_size=32) |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# For a single-input model with 2 classes (binary classification): model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(10, activation='sigmoid')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) # Generate dummy data import numpy as np data = np.random.random((1000,100)) labels = np.random.randint(10, size=(1000,1)) # Train the model, iterating on the data in batches of 32 samples model.fit(data, labels, epochs=10, batch_size=32) |

**Building Deep Learning Model Using Keras (Example)**

To understand the **keras**, how we use **keras** in deep learning models. I am taking **“Pima-Indians-diabetes-dataset”. **This is a simple Neural Network Implementation using **Keras** to understand that how actually **keras model** works.

You will find a complete **Jupyter notebook** on **“Pima-Indians-diabetes-dataset”. **

**Please visit my Github link: ****Code-neverends**