W Christian Consulting

What could you be doing better?

Deep Learning: Installing Keras

In my previous post, I mentioned building a neural network using Keras that was able to classify thousands of Reuters documents in under 10 minutes.  Sadly, installing Keras took more effort than I wanted–longer than actually building and training the network.  So, I worked out the kinks and scripted the process to go from a bare box to a document classifier in 8 steps.

The Installation Script

First, here’s the script.  If you run these commands as root, all should go well.  I’ll leave it as an exercise for the reader to figure out what to run as root, what to sudo, and what to run as a normal user for your specific situation, e.g., if you don’t want to install everything for everybody or if you have a python virtual environment.

yum install python-devel gcc gcc-gfortran gcc-c++ blas-devel \
 lapack-devel atlas-devel git
wget https://bootstrap.pypa.io/get-pip.py -O - | sudo python
pip install cython==0.22 nose==1.3.6 numpy==1.6.1 scipy==0.15.1 \
 Theano==0.7.0 six==1.9.0
git clone https://github.com/fchollet/keras.git
cd keras
sed -i ’s/install_requires/# install_requires/' setup.py
python setup.py install
python examples/reuters_mlp.py

The Installation Script Explained

And here’s what it does:

Install some software development packages.  Several of these might already be installed.  If they are, then yum will just let you know that they’re up to date and continue.  Note that yum will determine what needs to be installed and then ask you if that’s what you really want to do.  If you add the “-y” option (i.e., yum -y install) as an “assume yes” flag, it should proceed without waiting for your reply.

yum install python-devel gcc gcc-gfortran gcc-c++ blas-devel \
 lapack-devel atlas-devel git

Install pip, a package installer for python. The installation is actually two steps combined into a single line: download the installation script for pip and execute it in python.

wget https://bootstrap.pypa.io/get-pip.py -O - | python

Use pip to install Keras’s python dependencies. These are the latest stable versions as of this post and they aren’t explicitly required for Keras but I know they work. Feel free to remove the version requirements or update as you see fit.

pip install cython==0.22 nose==1.3.6 numpy==1.6.1 scipy==0.15.1 \
 Theano==0.7.0 six==1.9.0

Download the Keras code from GitHub and switch to the directory. Note that in the future you can update Keras with git.

git clone https://github.com/fchollet/keras.git
cd keras

Edit the setup.py configuration to remove the dependency on theano and h5py because there is a bug that shows up when you try to install them simultaneously. Fortunately, we already installed theano explicitly and h5py is “optional, required if you use model saving/loading functions” according to the Keras documentation.

sed -i ’s/install_requires/# install_requires/' setup.py

Run the Keras installation setup script.

python setup.py install

Run the Reuters example.

python examples/reuters_mlp.py

And there you have it.  Zero to Machine Learning in, what, 15 minutes?  Congratulations.