Updated on October 6, 2016

# Theano demo with Docker help

### What is Theano ?

From the Theano website:

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

This is a demo of the library Theano commonly used in machine learning due to its efficiency and simplicity to distribute computations over multi-core CPUs and GPU. Here we only use the CPU version

### Installation

Installing Theano library and dependencies can be painful for a novice like me. Furthermore, I have no idea of how to remove it once installed, and for a simple, quick and dirty trial, installing theano on my machine is not acceptable. Fortunately, genius invented Docker. Docker is a containerization platform which is much more light to use than a virtual machine. All in a container uses your machine resources and is directly plugged into your kernel. Great ! But what does Docker come doing in this post ? Well, simple, we'll pull a pre-built Docker image which already contains full Theano (and much more) installed ! There are several images you can fin in the Docker hub, and I've personaly used this one. The image I use also incorporate Jupyter Notebooks so that you can directly use a notebook to try Theano. So let's go !

*Important note:*

As state in the Docker image github repo, a convenient way to work with Theano is to use a Jupyter Notebook. For that, you have to run the container with the command

1 2 3 4 5 6 7 |
docker run -it --rm \ -p 8888:8888 \ -p 6006:6006 \ -v /notebooks \ -v /local/directory/to/notebooks:/notebooks \ floydhub/dl-docker:cpu \ jupyter notebook |

The command is as follow:

- -it: run the container in interactive mode
- --rm: remove the container after exit
- -p: port forwarding
- -v /notebooks: creates a directory
`/notebook`

at the root of the container. It will be removed on container exit - -v /local/directory/to/notebooks:/notebooks: map the local directory containing notebooks to the directory just created
- floydhub/dl-docker: name of the docker image
- jupyter notebook: command to be executed inside the container when it starts

However, if we use this command, we experience several kernel restart trials and finally it stops. A work around has been suggested in a github issue and the command should now be

1 2 3 4 5 6 7 |
docker run -it --rm \ -p 8888:8888 \ -p 6006:6006 \ -v /notebooks \ -v /home/guillaume/src/notebooks:/notebooks \ floydhub/dl-docker:cpu \ sh -c "jupyter notebook --ip=0.0.0.0 --no-browser --notebook-dir='/notebooks'" |

### First import needed packages

1 2 3 4 |
import numpy as np import theano.tensor as T from theano import function import time |

### Simple function

For useful doc on the various tensor types, see http://deeplearning.net/software/theano/library/tensor/basic.html

1 2 3 4 5 6 7 8 9 |
# declares a x variable which is a float64 vector x = T.dvector() # define a symbolic expression out = 1 + 2*x**2 + 3*x**3 # compile the function f = function([x], out) # compute t = np.array([0, 1, 2, 3]) print(f(t)) |

[ 1. 6. 33. 100.]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def npf(x): return 1 + 2*x**2 + 3*x**3 N = 100000000 t = np.random.rand(N) tic = time.time() npf(t) elapsed_numpy = time.time()-tic print('Elapsed time for numpy command: {}'.format(elapsed_numpy)) tic = time.time() f(t) elapsed_theano = time.time()-tic print('Elapsed time for theano command: {}'.format(elapsed_theano)) print('----------------------------------------------') print('Gain factor: {}'.format(elapsed_numpy/elapsed_theano)) |

Elapsed time for numpy command: 7.60342407227

Elapsed time for theano command: 0.10725903511

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

Gain factor: 70.8884250584

Waaah ! That's a great improvement ! Very exciting...... Theano is a very promissing library to help you improve your code execution speed. Let your imagination do the rest !

### Notes

The Docker container I propose also contains the Caffe library which has been precisely developped to help data scientists building deep learning applications. As it is defined on their website, Caffe is a "Deep learning framework" which uses BLAS and CUDA, just as Theano does.

## Recent Comments