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


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

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


First import needed packages

Simple function

For useful doc on the various tensor types, see

[ 1. 6. 33. 100.]

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 !


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.