Setup for Keras (Tensorflow Backend) and for Keras (Theano Backend)

Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano.

In order to install Keras, it requires  miniconda on python 2.x . Also, Keras uses the following dependencies:

  1. NumPy
  2. SciPy
  3. HDF5 and h5py
  4. Theano
  5.  TensorFlow

Step 0: Install miniconda

$ wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-`uname -p`.sh
$ bash Miniconda-latest-Linux-`uname -p`.sh
$ source ~/.bashrc

See also the install guide.

Step 1: Numpy, scipy, nose

a: Numpy:

NumPy is the fundamental package needed for scientific computing with Python. This package contains:

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities.

b: Scipy

SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. In particular, these are some of the core packages: NumPy,SciPy library,Matplotlib,IPython,Sympu,pandas

c:Nose:

nose extends unittest to make testing easier

d:PIP

pip is the preferred installer program. pip is a package management system used to install and manage software packages written in Python.

Install numpy, scipy, nose

$ sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose libopenblas-dev git

Step 2: Install h5py

$ conda install -y h5py

Step 3: Install Theano OrInstall TensorFlow

Please install either Theano or TensorFlow.  If you want to use Karas with backend as theano , so don't install TensorFlow and viceversa.

A: Theano

Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently.

  • tight integration with NumPy – Use numpy.ndarray in Theano-compiled functions.
  • transparent use of a GPU – Perform data-intensive calculations up to 140x faster than with CPU.(float32 only)
  • efficient symbolic differentiation – Theano does your derivatives for function with one or many inputs.
  • speed and stability optimizations – Get the right answer for log(1+x) even when x is really tiny.
  • dynamic C code generation – Evaluate expressions faster.
  • extensive unit-testing and self-verification – Detect and diagnose many types of errors.

Install Theano

$ pip install git+git://github.com/Theano/Theano.git

Ref: http://deeplearning.net/software/theano/

B: TensorFlow

TensorFlow is an open source software library for numerical computation using data flow graphs.

Install TensorFlow

$ pip install --upgrade -I setuptools
$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0rc

This is the workaround the bug for install tensorflow. See this for more information.

Ref:

Step 4: Install Keras

Keras is a minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano

$ pip install git+git://github.com/fchollet/keras.git

Step 5: spaCy: Industrial-strength NLP

spaCy is a library for advanced natural language processing in Python and Cython.
Documentation and details: https://spacy.io/

Install spaCy with conda

$ conda install -c https://conda.anaconda.org/spacy spacy
$ conda install spacy
$ python -m spacy.en.download all --force

ref: https://github.com/spacy-io/spaCy

Step 6: scikit-learn (Machine Learning in Python)

scikit-learn is a Python module for machine learning built on top of SciPy.

  • Simple and efficient tools for data mining and data analysis
  • Built on NumPy, SciPy, and matplotlib

Install scikit-learn with conda

$ conda install scikit-learn

ref:

http://scikit-learn.org/stable/

https://github.com/scikit-learn/scikit-learn

Step 7: Python Progressbar

It is s text progress bar library for Python. Typically used to display the progress of a long running operation, providing a visual cue that processing is underway.

Install progressbar with pip

$ pip install progressbar

Ref:

https://github.com/WoLpH/python-progressbar

Step 8: Test Keras

$ wget https://raw.githubusercontent.com/fchollet/keras/master/examples/mnist_mlp.py
$ python mnist_mlp.py

The result here.

60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
8s - loss: 0.4356 - acc: 0.8716 - val_loss: 0.1863 - val_acc: 0.9421
Epoch 2/20
7s - loss: 0.1961 - acc: 0.9414 - val_loss: 0.1274 - val_acc: 0.9601
Epoch 3/20
7s - loss: 0.1451 - acc: 0.9564 - val_loss: 0.1010 - val_acc: 0.9691
Epoch 4/20
8s - loss: 0.1189 - acc: 0.9642 - val_loss: 0.0847 - val_acc: 0.9752
Epoch 5/20
8s - loss: 0.1019 - acc: 0.9691 - val_loss: 0.0850 - val_acc: 0.9735
Epoch 6/20
8s - loss: 0.0903 - acc: 0.9721 - val_loss: 0.0749 - val_acc: 0.9777
Epoch 7/20
8s - loss: 0.0822 - acc: 0.9745 - val_loss: 0.0753 - val_acc: 0.9762
Epoch 8/20
7s - loss: 0.0758 - acc: 0.9762 - val_loss: 0.0743 - val_acc: 0.9796
Epoch 9/20
7s - loss: 0.0705 - acc: 0.9780 - val_loss: 0.0720 - val_acc: 0.9784
Epoch 10/20
8s - loss: 0.0648 - acc: 0.9790 - val_loss: 0.0688 - val_acc: 0.9793
Epoch 11/20
8s - loss: 0.0592 - acc: 0.9819 - val_loss: 0.0663 - val_acc: 0.9797
Epoch 12/20
8s - loss: 0.0567 - acc: 0.9824 - val_loss: 0.0677 - val_acc: 0.9815
Epoch 13/20
8s - loss: 0.0536 - acc: 0.9833 - val_loss: 0.0711 - val_acc: 0.9796
Epoch 14/20
8s - loss: 0.0520 - acc: 0.9834 - val_loss: 0.0684 - val_acc: 0.9806
Epoch 15/20
9s - loss: 0.0500 - acc: 0.9837 - val_loss: 0.0664 - val_acc: 0.9807
Epoch 16/20
7s - loss: 0.0471 - acc: 0.9850 - val_loss: 0.0683 - val_acc: 0.9809
Epoch 17/20
7s - loss: 0.0449 - acc: 0.9856 - val_loss: 0.0682 - val_acc: 0.9812
Epoch 18/20
8s - loss: 0.0433 - acc: 0.9860 - val_loss: 0.0675 - val_acc: 0.9813
Epoch 19/20
7s - loss: 0.0401 - acc: 0.9869 - val_loss: 0.0683 - val_acc: 0.9819
Epoch 20/20
8s - loss: 0.0383 - acc: 0.9874 - val_loss: 0.0705 - val_acc: 0.9820
Test score: 0.0704572771238
Test accuracy: 0.982

Step 9: Setup the backend of Keras

$ mkdir -p ~/.keras
$ echo '{"epsilon":1e-07,"floatx":"float32","backend":"tensorflow"}' > ~/.keras/keras.json

Step 10:Make sure that Keras runs with TensorFlow

$ curl -sSL https://github.com/fchollet/keras/raw/master/examples/mnist_mlp.py | python

------------------------------------------------------------------------------------

Reference

http://keras.io/

http://ermaker.github.io/blog/2015/09/08/get-started-with-keras-for-beginners.html

http://ermaker.github.io/blog/2016/06/22/get-started-with-keras-for-beginners-tensorflow-backend.html

https://gajumaru4444.github.io/2015/11/10/Visual-Question-Answering-2.html

 

Advertisements

One thought on “Setup for Keras (Tensorflow Backend) and for Keras (Theano Backend)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s