Theano is a python library for manipulating and evaluating expressions, especially matrix-valued ones.
What does Theano do that Python and numpy do not?
- *execution speed optimizations*: Theano can use `g++` to compile parts your expression graph into native machine code, which runs much faster than python.
- *symbolic differentiation*: Theano can convert a symbolic graph build symbolic graphs for computing gradients.
- *stability optimizations*: Theano can recognize numerically unstable expressions and compute them with more stable algorithms.
Here's a very simple example of how to use Theano. It doesn't show off many of Theano's features, but it illustrates concretely what Theano is.
.. code-block:: python
import theano
from theano import tensor
a = tensor.fscalar() # declare a symbolic floating-point scalar.
b = tensor.fscalar() # declare a symbolic floating-point scalar.
c = a + b # create a simple expression
f = theano.function([a,b], [c]) # convert the expression into a callable object
# that takes (a,b) values as input and computes a value for c
assert 4.0 == f(1.5, 2.5) # bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'
Theano is not a programming language in the normal sense because you write a program in Python that builds expressions for Theano. Still it is like a programming language in the sense that to use theano, you have to
- declare variables ({{{a,b}}}) and give their types
- build expressions for how to put those variables together
- compile expression graphs to functions in order to use them for computation.
It is good to think of `theano.function` as the interface to a compiler which builds a callable object from a purely symbolic graph.
License
License
-------
-------
Theano is licensed under a BSD-like license. See the LICENSE file in the project root folder.
- docutils, pygments (optional, to build documentation)
- mercurial (optional, to download the source)
- g++, python-dev (optional, to compile generated C code)
- `psyco <http://psyco.sourceforge.net/>`__ can make your python code much faster, if you are on a 32-bit x86 architecture. If you use compiled C code, this can be less important.
Downloading Theano
Downloading Theano
------------------
------------------
There are two ways to get the source: mercurial (required for library developers) and unix tar.
There are no stable releases yet.
*To get the source via mercurial,* you must have `mercurial <http://www.selenic.com/mercurial/wiki/>`__ installed.
Get the source and run the auto-tests like this:
.. code-block::
hg clone http://pylearn.org/hg/theano theano
cd theano
python autotest.py
To update your library to the latest on pylearn.org, change directory (`cd`) to this `theano` folder and type
.. code-block::
hg pull -u
*To get the source via unix tar*, you can download the latest source directly as a gzip'd tar file:
Two environment variables are used to control automatic code generation.
(It is possible to use theano in a way that avoids all automatic code generation, but the functions you make using {{{theano.function}}} will execute more slowly.)
- `THEANO_BLAS_LDFLAGS`:
a space-separated list of library names to link against for BLAS functions. Default: `-lblas`
- `THEANO_COMPILEDIR`:
a directory with read/write access permissions, where theano will store
autogenerated code and c modules. Default: `$HOME/.theano`. If this
directory does not exist, or does not have the correct permissions, then
theano will try to create it with the correct permissions. If that fails,
an exception will be raised and no C code will be compiled.
Setup on Linux
Setup on Linux
++++++++++++++
++++++++++++++
...
@@ -40,6 +120,21 @@ Setup on Linux
...
@@ -40,6 +120,21 @@ Setup on Linux
Setup on OS-X
Setup on OS-X
+++++++++++++
+++++++++++++
- Install [http://www.macports.org/ MacPorts]
- `sudo port install gcc42 py25-zlib py25-numpy py25-scipy mercurial`.
Note that compiling gcc42 takes a significant time (hours) so it's probably
not the best solution if you're in a rush! In my (Doomie) experience, scipy
failed to compile the first time I tried the command, but the second time
it compiled just fine. Same thing with py25-zlib.
- Install some kind of BLAS library (TODO: how?)
- Set THEANO_BLAS_LDFLAGS to something which will link against said BLAS
Use the fast BLAS library that Fred installed, by setting
`THEANO_BLAS_LDFLAGS=-lgoto`.
Tips for running on a cluster
Tips for running on a cluster
+++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++
WRITEME.
Use something like the following in your .bashrc:
.. code-block::
#use the intel math-kernel library for BLAS routines
THEANO_BLAS_LDFLAGS=-lmkl
# use up to two threads in the MKL routines
OMP_NUM_THREADS=2
# IMPORTANT!
# Use the local-temporary directory as a cache.
# If several jobs start simultaneously and use a common
# cache, then the cache may be corrupted.
# Theano is not process-safe or thread-safe in this sense.
THEANO_COMPILEDIR=/ltmp/<username>_theano
Running the Test Suite
Running the Test Suite
----------------------
======================
Test your installation by running the autotests. Type at the shell:
.. code-block::
WRITEME
cd theano
python2.5 autotest.py
All tests should pass.
Using Theano
Using Theano
------------
============
WRITEME
Now that you've got theano installed and running, check out the `n00b tutorial <n00b.html>`__ for how to use it.
Getting Help
Getting Help
------------
============
WRITEME
If these installation instructions don't work, search the theano-users archive for similar cases. If you don't find a solution, write to theano-users and explain the situation.
@@ -53,8 +53,8 @@ are symbolic because they stand for variables and have a type_, but
...
@@ -53,8 +53,8 @@ are symbolic because they stand for variables and have a type_, but
do not necessarily store actual values. Not yet, at least. (To give them
do not necessarily store actual values. Not yet, at least. (To give them
values, we will have to `evaluate` them. More on this below.)
values, we will have to `evaluate` them. More on this below.)
.. _result: WRITEME.html
.. _result: glossary.html#result
.. _type: WRITEME.html
.. _type: glossary.html#type
Since we are using the addition operator (`x + c`) here on symbolic results, the
Since we are using the addition operator (`x + c`) here on symbolic results, the
output `y` is also symbolic. The `+` corresponds to an ''operation'' in theano
output `y` is also symbolic. The `+` corresponds to an ''operation'' in theano
...
@@ -65,10 +65,10 @@ symbolic because we declare what it computes, but do not actually perform any
...
@@ -65,10 +65,10 @@ symbolic because we declare what it computes, but do not actually perform any
computation. Some type-checking is done on while we build our graphs, so if you
computation. Some type-checking is done on while we build our graphs, so if you
try to do something really crazy you'll see an exception right away.
try to do something really crazy you'll see an exception right away.
.. _symbolic graph: WRITEME.html
.. _symbolic graph: glossary.html#symbolicgraph
To actually use our graph for computation, we have to compile (or build) it into
To actually use our graph for computation, we have to compile (or build) it into
a function `f`. The compiled function is actually capable of performing
a function `f`. The compiled function is actually capable of performing
computation. So after we have built f, we use it to compute the value of y from
computation. So after we have built f, we use it to compute the value of y from
a `value input` x. Some argument checking is only possible at run-time, so if
a `value input` x. Some argument checking is only possible at run-time, so if
you ask for impossible things (i.e. logarithm of a negative number, sum of
you ask for impossible things (i.e. logarithm of a negative number, sum of
...
@@ -126,7 +126,8 @@ In the following example, we will build a function to evaluate the dot product `
...
@@ -126,7 +126,8 @@ In the following example, we will build a function to evaluate the dot product `
*TODO: Explain the matrix and other interesting things going on here.*
*TODO: Explain the matrix and other interesting things going on here.*
*TODO: Explain that we have a lot of numpy functionality reimplemented. Link to numpy docs and say familiarity won't hurt. Also link to list of available ops.*
*TODO: Explain that we have a lot of numpy functionality reimplemented. Link to
numpy docs and say familiarity won't hurt. Also link to list of available ops.*
Broadcasting example
Broadcasting example
====================
====================
...
@@ -150,7 +151,76 @@ You may now wish to review some
...
@@ -150,7 +151,76 @@ You may now wish to review some
State example
State example
=============
=============
*WRITEME: A simple logistic regression example, with implicit weights.*
In this example, we'll look at a complete logistic regression model, with