Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Keras Deep Learning Cookbook

You're reading from   Keras Deep Learning Cookbook Over 30 recipes for implementing deep neural networks in Python

Arrow left icon
Product type Paperback
Published in Oct 2018
Publisher Packt
ISBN-13 9781788621755
Length 252 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Sujit Pal Sujit Pal
Author Profile Icon Sujit Pal
Sujit Pal
Manpreet Singh Ghotra Manpreet Singh Ghotra
Author Profile Icon Manpreet Singh Ghotra
Manpreet Singh Ghotra
Rajdeep Dua Rajdeep Dua
Author Profile Icon Rajdeep Dua
Rajdeep Dua
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Keras Installation FREE CHAPTER 2. Working with Keras Datasets and Models 3. Data Preprocessing, Optimization, and Visualization 4. Classification Using Different Keras Layers 5. Implementing Convolutional Neural Networks 6. Generative Adversarial Networks 7. Recurrent Neural Networks 8. Natural Language Processing Using Keras Models 9. Text Summarization Using Keras Models 10. Reinforcement Learning 11. Other Books You May Enjoy

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.

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 with miniconda.

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
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

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.

You have been reading a chapter from
Keras Deep Learning Cookbook
Published in: Oct 2018
Publisher: Packt
ISBN-13: 9781788621755
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image