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.
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.
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.
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.
- To install
miniconda
, let's first download thesh
installer from thecontinuum
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
- Once
conda
has been installed, we can use it to install the dependencies of Theano, TensorFlow, and Keras.
The numpy
and scipy
packages are prerequisites for Theano installation. The following versions are recommended:
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.
- Execute the following command to install
numpy
andscipy
. (Make sureconda
is in yourPATH
):
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:
- 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
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
- Once all the prerequisites are installed, let's install TensorFlow.
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:
- 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
- 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
- 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
- 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))
- 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!
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
- Let's verify the Keras installation with the following code:
$ python Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19)
- 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.
vi .keras/keras.json
The default file has the following content:
{ "image_data_format": "channels_last", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" }
- 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" }
- Run the Python console and import
Sequential
fromkeras.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.
In this recipe, we learn how to install and use a Docker container running Keras inside a container and access it using Jupyter.
Install the latest version of the Docker CLI from https://docs.docker.com/engine/installation/.
In the following section, we will be learning how to install the Docker container.
docker run -d -p 8888:8888 rajdeepd/jupyter-keras start-notebook.sh --NotebookApp.token=''
- 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
.
- 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.
In this section, we look at how to map the local volume $(pwd)/keras-samples
to the work directory in the container.
- 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.
- If you got
/$(pwd)/keras-samples
, you will notice that the Notebooks are available in thehost
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.
In this recipe, we will install Keras on Ubuntu 16.04 with NVIDIA GPU enabled.
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
sudo apt-get update sudo apt-get upgrade
- Install the
gcc
compiler and make the tool:
sudo apt install gcc sudo apt install make
sudo apt-get install -y cuda
- 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
- 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
- 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)
- Next we install
cudnn
, which is a deep learning library from NVIDIA. You can find more information at https://developer.nvidia.com/cudnn.
- 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:
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
- 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*
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