Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Keras Deep Learning Cookbook
Keras Deep Learning Cookbook

Keras Deep Learning Cookbook: Over 30 recipes for implementing deep neural networks in Python

By Rajdeep Dua , Sujit Pal , Manpreet Singh Ghotra
£22.99 £15.99
Book Oct 2018 252 pages 1st Edition
eBook
£22.99 £15.99
Print
£28.99
Subscription
£13.99 Monthly
eBook
£22.99 £15.99
Print
£28.99
Subscription
£13.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now
Table of content icon View table of contents Preview book icon Preview Book

Keras Deep Learning Cookbook

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.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Understand different neural networks and their implementation using Keras
  • Explore recipes for training and fine-tuning your neural network models
  • Put your deep learning knowledge to practice with real-world use-cases, tips, and tricks

Description

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

What you will learn

Install and configure Keras in TensorFlow Master neural network programming using the Keras library Understand the different Keras layers Use Keras to implement simple feed-forward neural networks, CNNs and RNNs Work with various datasets and models used for image and text classification Develop text summarization and reinforcement learning models using Keras

Product Details

Country selected

Publication date : Oct 31, 2018
Length 252 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781788621755
Vendor :
Google
Category :
Concepts :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Oct 31, 2018
Length 252 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781788621755
Vendor :
Google
Category :
Concepts :

Table of Contents

17 Chapters
Title Page Chevron down icon Chevron up icon
Copyright and Credits Chevron down icon Chevron up icon
Packt Upsell Chevron down icon Chevron up icon
Contributors Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Keras Installation Chevron down icon Chevron up icon
2. Working with Keras Datasets and Models Chevron down icon Chevron up icon
3. Data Preprocessing, Optimization, and Visualization Chevron down icon Chevron up icon
4. Classification Using Different Keras Layers Chevron down icon Chevron up icon
5. Implementing Convolutional Neural Networks Chevron down icon Chevron up icon
6. Generative Adversarial Networks Chevron down icon Chevron up icon
7. Recurrent Neural Networks Chevron down icon Chevron up icon
8. Natural Language Processing Using Keras Models Chevron down icon Chevron up icon
9. Text Summarization Using Keras Models Chevron down icon Chevron up icon
10. Reinforcement Learning Chevron down icon Chevron up icon
1. Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Top Reviews
No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.