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.

You may also consider installing the following optional dependencies:

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

Install Keras from PyPI (Recommended)

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

Alternatively: install Keras from the GitHub source:

First, clone Keras using git:

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

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:

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

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:


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.


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

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

You might also like More from author