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.
Installing Keras on Ubuntu 16.04
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.
- 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
- 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
- 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:
- 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
- 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
- Once all the prerequisites are installed, let's install TensorFlow.
Installing TensorFlow
- 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:
- 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!
Installing Keras
- Next, we will install Keras using conda from conda-forge
- 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
- 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.
Using the Theano backend with Keras
- 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"
}
- 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 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.