Book Image

Keras 2.x Projects

By : Giuseppe Ciaburro
Book Image

Keras 2.x Projects

By: Giuseppe Ciaburro

Overview of this book

Keras 2.x Projects explains how to leverage the power of Keras to build and train state-of-the-art deep learning models through a series of practical projects that look at a range of real-world application areas. To begin with, you will quickly set up a deep learning environment by installing the Keras library. Through each of the projects, you will explore and learn the advanced concepts of deep learning and will learn how to compute and run your deep learning models using the advanced offerings of Keras. You will train fully-connected multilayer networks, convolutional neural networks, recurrent neural networks, autoencoders and generative adversarial networks using real-world training datasets. The projects you will undertake are all based on real-world scenarios of all complexity levels, covering topics such as language recognition, stock volatility, energy consumption prediction, faster object classification for self-driving vehicles, and more. By the end of this book, you will be well versed with deep learning and its implementation with Keras. You will have all the knowledge you need to train your own deep learning models to solve different kinds of problems.
Table of Contents (13 chapters)


Previously, we have already said that Keras is written in Python, so in order for it to work, it is necessary to have a previously installed version of Python (Keras is compatible with Python 2.7-3.6). Platforms that support Python development environments can support Keras as well. Furthermore, before installing Keras, it is necessary to provide for the installation of the backend engine, and some optional dependencies useful for the implementation of machine learning models.

Optional dependencies

Some useful optional dependencies are listed in the following list:

  • NumPy: This is an open source library of the Python programming language, which adds support for multidimensional and large vectors, and even matrices with high-level mathematical functions to work with.
  • SciPy: This is an open source library of mathematical algorithms and tools. It contains modules for optimization, linear algebra, integration, special functions, fast fourier transform (FFT), signal and image processing, ordinary differential equation (ODT) solvers, and other common tools in science and engineering.
  • Scikit-learn: This is an open source machine learning library for the Python programming language. It contains classification, regression, clustering algorithms, support vector machines, logistic regression, bayesian classifier, k-means, and DBSCAN, and is designed to work with the NumPy and SciPy libraries.
  • cuDNN: This is a GPU accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines, such as forward and backward convolution, pooling, normalization, and activation layers.
  • HDF5: This is a data model, library, and file format for storing and managing data. It supports an unlimited variety of datatypes, and is designed to be flexible and efficient.
  • H5py: This is a Python interface to the HDF5 binary data format.
  • Graphviz: This is an open source program used to draw graphs described in the DOT language. It provides libraries for applications using the tools provided. Graphviz is free software licensed under the Common Public License (CPL).
  • Pydot: This is a Python interface for Graphviz and the DOT language.

We can now proceed with the installation of every single library or install all the dependencies with a single line of code. Alternatively, you can install the Anaconda Python module, which will automatically install these libraries and a lot of other libraries that are needed for scientific computing.

Installing the backend engine

After installing the dependencies, it is necessary to select the backend engine and proceed with its installation. Keras developers recommend the TensorFlow backend. This is the one set by default.

Links to installation instructions for all the available backends are listed in the following list:

In all cases, we can use the pip command to install the correct packages.

Keras installation and configuration

At this point, the environment is ready for Keras installation. If all the dependencies and the backend engine have been correctly installed, we can proceed. Actually, the work that remains to be done is very simple and immediate.

There are two ways to install Keras as follows:

The first step involves the use of the Python Package Index (PyPI).

This is a repository of software for the Python programming language. PyPI helps you find and install software developed and shared by the Python community. This is the recommended option.

After installing the PyPI package, simply type the following command:

$ sudo pip install keras

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

$ pip install keras

Alternatively, we can install Keras from the GitHub source. To do this, we first need to clone Keras using git:

$ git clone

Then, add cd to the keras folder and run the install command:

cd keras
sudo python install

We have the Keras environment available to implement our deep learning models. Let us look at some more information about the configuration. As we have already mentioned, Keras uses TensorFlow as the default backend. If we want to use the other two options, Theano and CNTK, we must modify the Keras configuration file. This is a file with the .json extension, and is named keras.json..

The file's position depends on the operating system that is in use in our PC:

$ HOME/.keras/keras.json

We can use the following command for Unix-like OSes:


And, for Windows OS, the contents of the configuration file are of the following type:

"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"

The parameters have the following meaning:

  • image_data_format: String, either channels_last or channels_first. It specifies which data format convention Keras will follow.
  • epsilon: Float, a numeric fuzzing constant used to avoid dividing by zero in some operations.
  • floatx: String, float16, float32, or float64. Default float precision.
  • backend: String, tensorflow, theano, or cntk.

Switching from one backend to another is very fast. Simply change the field backend to theano, tensorflow, or cntk, and Keras will use the new configuration the next time you run any Keras code.