Home Data Keras Deep Learning Cookbook

Keras Deep Learning Cookbook

By Rajdeep Dua , Sujit Pal , Manpreet Singh Ghotra
books-svg-icon Book
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Keras Installation
About this book
Keras has quickly emerged as a popular deep learning library. Written in Python, it allows you to train convolutional as well as recurrent neural networks with speed and accuracy. The Keras Deep Learning Cookbook shows you how to tackle different problems encountered while training efficient deep learning models, with the help of the popular Keras library. Starting with installing and setting up Keras, the book demonstrates how you can perform deep learning with Keras in the TensorFlow. From loading data to fitting and evaluating your model for optimal performance, you will work through a step-by-step process to tackle every possible problem faced while training deep models. You will implement convolutional and recurrent neural networks, adversarial networks, and more with the help of this handy guide. In addition to this, you will learn how to train these models for real-world image and language processing tasks. By the end of this book, you will have a practical, hands-on understanding of how you can leverage the power of Python and Keras to perform effective deep learning
Publication date:
October 2018
Publisher
Packt
Pages
252
ISBN
9781788621755

 

Chapter 1. Keras Installation

In this chapter, we will cover the following recipes:

  • Installing Keras on Ubuntu 16.04
  • Installing Keras with Jupyter Notebook in a Docker image
  • Installing Keras on Ubuntu 16.04 with GPU enabled
 

Introduction


In this chapter, we look at how Keras can be installed on Ubuntu and CentOS. We will use Ubuntu 16.04, 64-bit (Canonical, Ubuntu, 16.04 LTS, and amd64 xenial image build on 2017-10-26) for the installation.

 

Installing Keras on Ubuntu 16.04


Before installing Keras, we have to install the Theano and TensorFlow packages and their dependencies. Since it is a fresh OS, make sure Python is installed. Let's look at the following section for Python installation.

Note

Conda is an open source package management system and environment management system that runs on multiple OSes: Windows, macOS, and Linux. Conda installs, runs, and updates packages and their dependencies. Conda creates, saves, loads, and switches between environments on a local computer. It has been created for Python environments.

 

Getting ready

First you need to make sure you have a blank Ubuntu 16.04 OS locally or remotely available in the cloud and with root access. 

How to do it...

In the following sections, we take a at the installation of each component that needs to be done before we can go ahead with the installation of Keras.

Installing miniconda

Before we proceed further, let's install miniconda to install the rest of the packages. Miniconda is a smaller version of the conda package manager. Python is bundled along withminiconda.

Note

It is recommended that users choose either Python 2.7 or Python 3.4. Python = 2.7* or ( >= 3.4 and < 3.6 ). The Python development package (python-dev or python-devel on most Linux distributions) is recommended. We will focus on Python 2.7.

  1. To install miniconda, let's first download the sh installer from the continuum repository:
wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh
chmod 755 Miniconda2-latest-Linux-x86_64.sh 
./Miniconda2-latest-Linux-x86_64.sh
  1. Once conda has been installed, we can use it to install the dependencies of Theano, TensorFlow, and Keras.

Installing numpy and scipy

The numpy and scipy packages are prerequisites for Theano installation. The following versions are recommended:

  • NumPy >= 1.9.1 <= 1.12
  • SciPy >= 0.14 < 0.17.1: Highly recommended for sparse matrix and support for special functions in Theano, SciPy >=0.8 would do the work
  • BLAS installation (with Level 3 functionality) the recommended: MKL, this is free through conda with the mkl-service package

Note

Basic Linear Algebra Subprograms (BLAS) is a specification that defines a set of low-level routines for performing common linear algebra operations such as vector addition, scalar multiplication, dot products, linear combinations, and matrix multiplication. These are the de facto standard low-level routines for linear algebra libraries; the routines have bindings for both C and Fortran. Level 3 is referred to as matrix -to-matrix multiplications.

  1. Execute the following command to install numpy and scipy. (Make sure conda is in your PATH):
conda install numpy
conda install scipy

The output of the scipy installation is shown as follows. Notice that it installs libgfortran as part of the scipy installation:

Fetching package metadata ...........
Solving package specifications: .
Package plan for installation in environment /home/ubuntu/miniconda2:
  1. The following new packages will also be installed:
libgfortran-ng: 7.2.0-h9f7466a_2 
scipy: 1.0.0-py27hf5f0f52_0
Proceed ([y]/n)?
libgfortran-ng 100% |#############################################################| Time: 0:00:00 36.60 MB/s
scipy-1.0.0-py 100% |#############################################################| Time: 0:00:00 66.62 MB/s

Installing mkl

  1. mkl is a math library for Intel and compatible processors. It is a part of numpy, but we want to make sure it is installed before we install Theano and TensorFlow:
conda install mkl

 

 

 

 

 

 

 

 

 

 

The output of the installation is given as follows. In our case, miniconda2 has already installed the latest version of mkl:

Fetching package metadata ...........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /home/ubuntu/miniconda2:
#
mkl 2018.0.1 h19d6760_4
  1. Once all the prerequisites are installed, let's install TensorFlow.

Installing TensorFlow

  1. Execute the following command to install tensorflow using conda:
conda install -c conda-forge tensorflow

The output of this command will fetch metadata and install a list of packages, as follows:

Fetching package metadata .............
Solving package specifications: .
Package plan for installation in environment /home/ubuntu/miniconda2:
  1. The following new packages will also be installed:
bleach: 1.5.0-py27_0 conda-forge
funcsigs: 1.0.2-py_2 conda-forge
futures: 3.2.0-py27_0 conda-forge
html5lib: 0.9999999-py27_0 conda-forge
markdown: 2.6.9-py27_0 conda-forge
mock: 2.0.0-py27_0 conda-forge
pbr: 3.1.1-py27_0 conda-forge
protobuf: 3.5.0-py27_0 conda-forge
tensorboard: 0.4.0rc3-py27_0 conda-forge
tensorflow: 1.4.0-py27_0 conda-forge
webencodings: 0.5-py27_0 conda-forge
werkzeug: 0.12.2-py_1 conda-forge
  1. A higher-priority channel will supersede the following packages, as follows:
conda: 4.3.30-py27h6ae6dc7_0 --> 4.3.29-py27_0 conda-forge
conda-env: 2.6.0-h36134e3_1 --> 2.6.0-0 conda-forge
Proceed ([y]/n)? y
conda-env-2.6. 100% |#############################################################| Time: 0:00:00 1.67 MB/s
...
mock-2.0.0-py2 100% |#############################################################| Time: 0:00:00 26.00 MB/s
conda-4.3.29-p 100% |#############################################################| Time: 0:00:00 27.46 MB/s
  1. Once TensorFlow has been installed, let's test it with a simple program. Create a new file called hello_tf.py with the following command:
vi hello_tf.py
  1. Add the following code to this file and save the file:
import tensorflow as tf
hello = tf.constant('Greetings, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
  1. Execute the file created from the command line:
python hello_tf.py

The output will make sure the library has been successfully installed:

Greetings, TensorFlow!

Installing Keras

Note

conda-forge is a GitHub entity with a repository of conda recipes.

  1. Next, we will install Keras using conda from conda-forge
  2. Execute the following command on the Terminal:
conda install -c conda-forge keras

The following listed output will confirm that Keras is installed:

Fetching package metadata .............
Solving package specifications: .
Package plan for installation in environment /home/ubuntu/miniconda2:

The following new packages will also be installed:

h5py: 2.7.1-py27_2 conda-forge
hdf5: 1.10.1-1 conda-forge
keras: 2.0.9-py27_0 conda-forge
libgfortran: 3.0.0-1 
pyyaml: 3.12-py27_1 conda-forge
Proceed ([y]/n)? y
libgfortran-3. 100% |#############################################################| Time: 0:00:00 35.16 MB/s
hdf5-1.10.1-1. 100% |#############################################################| Time: 0:00:00 34.26 MB/s
pyyaml-3.12-py 100% |#############################################################| Time: 0:00:00 60.08 MB/s
h5py-2.7.1-py2 100% |#############################################################| Time: 0:00:00 58.54 MB/s
keras-2.0.9-py 100% |#############################################################| Time: 0:00:00 45.92 MB/s
  1. Let's verify the Keras installation with the following code:
$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19)
  1. Execute the following command to verify that Keras has been installed:
> from keras.models import Sequential
Using TensorFlow backend.
>>>

Notice that Keras is using the TensorFlow backend.

Using the Theano backend with Keras

  1. Let's modify the default configuration and change TensorFlow to Theano as the backend of Keras. Modify the keras.json file:
vi .keras/keras.json

The default file has the following content:

{ "image_data_format": "channels_last", 
  "epsilon": 1e-07, 
  "floatx": "float32", 
  "backend": "tensorflow"
}
  1. The modified file will look like the following file. The "backend" value has been changed to "theano":
{ "image_data_format": "channels_last", 
  "epsilon": 1e-07, 
  "floatx": "float32", 
  "backend": "theano"
}
  1. Run the Python console and import Sequential from keras.model using the Theano backend:
$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.models import Sequential

Notice how the backend has changed to Theano.

We have installed miniconda, all the dependencies of TensorFlow, and Theano. This was followed by installing TensorFlow and Theano itself. Finally, we installed Keras. We also learned how to change the backend of Keras from TensorFlow to Theano.

 

Installing Keras with Jupyter Notebook in a Docker image


In this recipe, we learn how to install and use a Docker container running Keras inside a container and access it using Jupyter.

Getting ready

Install the latest version of the Docker CLI from https://docs.docker.com/engine/installation/.

How to do it...

In the following section, we will be learning how to install the Docker container.

Installing the Docker container 

  1. Execute the following command on the Terminal to run the container. The container image is available with the tag rajdeepd/jupyter-keras:
docker run -d -p 8888:8888 rajdeepd/jupyter-keras start-notebook.sh --NotebookApp.token=''
  1. This will install the Notebook locally and start it as well. You can execute the docker ps -a command and see the output in the Terminal, as follows:
CONTAINER IDIMAGE COMMANDCREATED STATUS PORTS NAMES
45998a5eea89rajdeepd/jupyter-keras"tini -- start-not..." About an hour ago Up About an hour 0.0.0.0:8888->8888/tcpadmiring_wing

Please note that the host port of 8888 is mapped to the container port of 8888.

  1. Open the browser at the following URL http://localhost:8888:

You will notice that Jupyter is running. You can create a new Notebook and run Keras-specific code.

Installing the Docker container with the host volume mapped

In this section, we look at how to map the local volume $(pwd)/keras-samples to the work directory in the container.

  1. Execute the note -v flag command, which does the volume mapping:
docker run -d -v /$(pwd)/keras-samples:/home/jovyan/work \
 -p 8888:8888 rajdeepd/jupyter-keras start-notebook.sh --NotebookApp.token=''

If you go to the URL, you will notice the sample page being displayed.

 

 

  1. If you got /$(pwd)/keras-samples, you will notice that the Notebooks are available in the host directory, and they also can be seen being loaded by Jupyter:
rdua1-ltm:keras-samples rdua$ pwd
 /Users/rdua/personal/keras-samples
 rdua1-ltm:keras-samples rdua$ ls
 MNIST CNN.ipynb sample_one.ipynb

If you open MNIST CNN.ipynb, it is a Keras CNN sample, which we will learn more about in the subsequent chapters.

In this recipe, we used the Docker image rajdeepd/jupyter-keras to create a Keras environment and access it from Jupyter running in the host environment.

 

Installing Keras on Ubuntu 16.04 with GPU enabled


In this recipe, we will install Keras on Ubuntu 16.04 with NVIDIA GPU enabled.

Getting ready

We are going to launch a GPU-enabled AWS EC2 instance and prepare it for the installed TensorFlow with the GPU and Keras. Launch the following AMI: Ubuntu Server 16.04 LTS (HVM), SSD Volume Type - ami-aa2ea6d0:

This is an AMI with Ubuntu 16.04 64 bit pre-installed, and it has the SSD volume type.

Choose the appropriate instance type: g3.4xlarge:

Once the VM is launched, assign the appropriate key that you will use to SSH into it. In our case, we used a pre-existing key:

SSH into the instance:

ssh -i aws/rd_app.pem ubuntu@34.201.110.131

How to do it...

  1. Run the following commands to update and upgrade the OS:
sudo apt-get update
sudo apt-get upgrade
  1. Install the gcc compiler and make the tool:
sudo apt install gcc
sudo apt install make

Installing cuda

  1. Execute the following command to execute cuda:
sudo apt-get install -y cuda
  1. Check that cuda is installed and run a basic program:
ls /usr/local/cuda-8.0
bin extras lib64 libnvvp nvml README share targets version.txt
doc include libnsight LICENSE nvvm samples src tools
  1. Let's run one of the cuda samples after compiling it locally:
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64\${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
cd /usr/local/cuda-8.0/samples/5_Simulations/nbody
  1. Compile the sample and run it as follows:
sudo make

./nbody

You will see output similar to the following listing:

Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance.
 -fullscreen (run n-body simulation in fullscreen mode)
 -fp64 (use double precision floating point values for simulation)
 -hostmem (stores simulation data in host memory)
 -benchmark (run benchmark to measure performance)
 -numbodies=<N> (number of bodies (>= 1) to run in simulation)
 -device=<d> (where d=0,1,2.... for the CUDA device to use)
 -numdevices=<i> (where i=(number of CUDA devices > 0) to use for simulation)
 -compare (compares simulation results running once on the default GPU and once on the CPU)
 -cpu (run n-body simulation on the CPU)
 -tipsy=<file.bin> (load a tipsy model file for simulation)
  1. Next we install cudnn, which is a deep learning library from NVIDIA. You can find more information at https://developer.nvidia.com/cudnn.

 

Installing cudnn

  1. Download cudnn from the NVIDIA site (https://developer.nvidia.com/rdp/assets/cudnn-8.0-linux-x64-v5.0-ga-tgz) and decompress the binary:

Note

Please note, you will need an NVIDIA developer account.

tar xvf cudnn-8.0-linux-x64-v5.1.tgz

We obtain the following output after decompressing the .tgz file:

cuda/include/cudnn.h
cuda/lib64/libcudnn.so
cuda/lib64/libcudnn.so.5
cuda/lib64/libcudnn.so.5.1.10
cuda/lib64/libcudnn_static.a
  1. Copy these files to the /usr/local folder, as follows:
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64

sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

Installing NVIDIA CUDA profiler tools interface development files

Install the NVIDIA CUDA profiler tools interface development files that are needed for TensorFlow GPU installation with the following code:

sudo apt-get install libcupti-dev

Installing the TensorFlow GPU version

Execute the following command to install the TensorFlow GPU version:

sudo pip install tensorflow-gpu

 

 

Installing Keras

For Keras, use the sample command, as used for the installation with GPUs:

sudo pip install keras

In this recipe, we learned how to install Keras on top of the TensorFlow GPU hooked to cuDNN and CUDA.

About the Authors
  • Rajdeep Dua

    Rajdeep Dua has over 18 years experience in the cloud and big data space. He has taught Spark and big data at some of the most prestigious tech schools in India: IIIT Hyderabad, ISB, IIIT Delhi, and Pune College of Engineering. He currently leads the developer relations team at Salesforce India. He has also presented BigQuery and Google App Engine at the W3C conference in Hyderabad. He led the developer relations teams at Google, VMware, and Microsoft, and has spoken at hundreds of other conferences on the cloud. Some of the other references to his work can be seen at Your Story and on ACM digital library. His contributions to the open source community relate to Docker, Kubernetes, Android, OpenStack, and Cloud Foundry.

    Browse publications by this author
  • Sujit Pal

    Sujit Pal is a Technology Research Director at Elsevier Labs, an advanced technology group within the Reed-Elsevier Group of companies. His interests include semantic search, natural language processing, machine learning, and deep learning. At Elsevier, he has worked on several initiatives involving search quality measurement and improvement, image classification and duplicate detection, and annotation and ontology development for medical and scientific corpora.

    Browse publications by this author
  • Manpreet Singh Ghotra

    Manpreet Singh Ghotra has more than 15 years experience in software development for both enterprise and big data software. He is currently working at Salesforce on developing a machine learning platform/APIs using open source libraries and frameworks such as Keras, Apache Spark, and TensorFlow. He has worked on various machine learning systems, including sentiment analysis, spam detection, and anomaly detection. He was part of the machine learning group at one of the largest online retailers in the world, working on transit time calculations using Apache Mahout, and the R recommendation system, again using Apache Mahout. With a master's and postgraduate degree in machine learning, he has contributed to, and worked for, the machine learning community.

    Browse publications by this author
Latest Reviews (4 reviews total)
The whole book is bunch of source code snippets without any useful explanation.
very easy for purchasings
Nice start material at least for me :-)
Keras Deep Learning Cookbook
Unlock this book and the full library FREE for 7 days
Start now