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.