1.2 Install Theano
The steps fetched from the tutorial website, http://deeplearning.net/software/theano/, be implemented on 32-bit Ubuntu 14.04 server. Since Lasagne and Nolearn we will install later, we are here to setup the required version for Lasagne and Nolearn.
You also can see Easy Installing on Ubuntu to get more information.
Prep. work
Before working, there are some packages and software should be installed first:
$ sudo apt-get install python-numpy cython python-scipy python-dev python-pip python-nose g++ libopenblas-dev git libblas-dev liblapack-dev libatlas-base-dev gfortran
Since on 14.04, this will install Python 2 by default. If you want to use Python 3:
$ sudo apt-get install python3-numpy python3-scipy python3-dev python3-pip python3-nose g++ libopenblas-dev git libblas-dev liblapack-dev libatlas-base-dev gfortran
- If it appears the error message like this:
try to rename 'lock' file, for example:E: Could not get lock /var/cache/apt/archives/lock - open (11: Resource temporarily unavailable) E: Unable to lock directory /var/cache/apt/archives/
$ mv /var/cache/apt/archives/lock /var/cache/apt/archives/lock_bak
Install Numpy, Scipy
In order to work successfully with Lasagne and Nolearn, there are required version should be installed:
$ pip intsall Numpy==1.10.4
$ pip install Scipy==0.16.1
- If there is a very long time stopping when installing Scipy, please just wait for it. It will be completed after the long time waiting. Or try to install 0.10.1 version:
$ pip install Scipy==0.10.1
Install Theano
Then use pip to install Theano (or add --user
to install in own directory):
For more information about
pip
, please click here.
$ pip install Theano==0.8.0
Or, if you would like, instead, to install the bleeding edge Theano (from github) such that you can edit and contribute to Theano, replace the pip install Theano
command with:
$ git clone git://github.com/Theano/Theano.git
$ cd Theano
$ python setup.py develop --user
$ cd ..
Now the installation is complete. You can start with Theano! Or you can get testing for it.
Test installed packages
Try commands below to test the packages whether are installed correctly or not:
- NumPy (~30s):
python -c "import numpy; numpy.test()"
- SciPy (~1m):
python -c "import scipy; scipy.test()"
- Theano (~30m):
python -c "import theano; theano.test()"
( test()
is an unit-test function for these packages to test correctness)
Let's take a look the result on our machine:
[ Numpy version: 1.8.2 ] @ 32-bit Ubuntu 14.04 server
$ python -c "import numpy; numpy.test()"
OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.
Running unit tests for numpy
NumPy version 1.8.2
NumPy is installed in /usr/lib/python2.7/dist-packages/numpy
Python version 2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4.8.2]
nose version 1.3.1
..............................S.........K.............................
(etc.)
----------------------------------------------------------------------
Ran 4963 tests in 116.095s
OK (KNOWNFAIL=5, SKIP=7)
[ Numpy version: 1.10.4 ] @ 32-bit Ubuntu 14.04 serevr
$ python -c "import numpy; numpy.test()"
OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.
Running unit tests for numpy
NumPy version 1.10.4
NumPy relaxed strides checking option: False
NumPy is installed in /usr/local/lib/python2.7/dist-packages/numpy
Python version 2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4.8.2]
nose version 1.3.1
.................................................................(etc.)
----------------------------------------------------------------------
Ran 5978 tests in 64.813s
OK (KNOWNFAIL=3, SKIP=5)
[ Numpy version: 1.11.1 ] @ 32-bit Ubuntu 14.04 serevr
$ python -c "import numpy; numpy.test()"
Running unit tests for numpy
NumPy version 1.11.1
NumPy relaxed strides checking option: False
NumPy is installed in /usr/local/lib/python2.7/dist-packages/numpy
Python version 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]
nose version 1.3.1
............................(etc.)
----------------------------------------------------------------------
Ran 6151 tests in 178.781s
OK (KNOWNFAIL=6, SKIP=11)
Other two commands shows the similar result.
On the other hand, if you get the error after python -c "import theano; theano.test()"
, like this:
ERROR: Failure: ImportError (No module named nose_parameterized)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 411, in loadTestsFromName
addr.filename, addr.module)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/usr/local/lib/python2.7/dist-packages/theano/tests/test_rop.py", line 16, in <module>
from theano.tests import unittest_tools as utt
File "/usr/local/lib/python2.7/dist-packages/theano/tests/unittest_tools.py", line 7, in <module>
from nose_parameterized import parameterized
ImportError: No module named nose_parameterized
----------------------------------------------------------------------
Please download nose-parameterized
to fix this problem:
$ pip install nose-parameterized
In addition, another way to run the testing above in-place from the Git checkout directory by typing:
$ theano-nose
NOTICE:
The tests should be run with the configuration option device set to cpu (default). If you need to change this value, you can do that by setting the THEANO_FLAGS environment variable, by prefixing the theano-nose command with
THEANO_FLAGS=device=cpu
. If you have a GPU, it will automatically be used to run GPU-related tests.If you want GPU-related tests to run on a specific GPU device, and not the default one, you should use init_gpu_device. For instance:
THEANO_FLAGS=device=cpu,init_gpu_device=gpu1
.See config – Theano Configuration for more information on how to change these configuration options.
Usage
Here is an example of how to use Theano. It doesn’t show off many of Theano’s features, but it illustrates concretely what Theano is.
import theano
from theano import tensor
# declare two symbolic floating-point scalars
a = tensor.dscalar()
b = tensor.dscalar()
# create a simple expression
c = a + b
# convert the expression into a callable object that takes (a,b)
# values as input and computes a value for c
f = theano.function([a,b], c)
# bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'
assert 4.0 == f(1.5, 2.5)
- If
import theano
does not work in Python, you may need modify the environment variable PYTHONPATH accordingly. In bash, you may do this:
In csh:export PYTHONPATH=<new location to add>:$PYTHONPATH
To make this change stick you will usually need to add the above command to your shell’s startup script, i.e.setenv PYTHONPATH <new location to add>:$PYTHONPATH
~/.bashrc
or~/.cshrc
. Consult your shell’s documentation for details.
Update Theano
The following command will update only Theano:
$ sudo pip install --upgrade --no-deps theano
The following command will update Theano and Numpy/Scipy:
$ sudo pip install --upgrade theano
If you installed NumPy/SciPy with yum/apt-get, updating NumPy/SciPy with pip/easy_install might make Theano crash. (more detail see here)
To fix the crash, you can clear the Theano cache like this:
$ theano-cache clear
We have introduced the quick steps including prepared work, installing Theano, testing packages, usage examples, and how to update Theano, which are enough to start the work.
If you hope to use more machine cores to speedup, and use the GPU, then there are some actions below you can follow.
Manual Openblas instruction
The openblas included in some older Ubuntu version is limited to 2 threads. Ubuntu 14.04 do not have this limit. If you want to use more cores at the same time, you will need to compile it yourself. Here is some code that will help you:
# remove openblas if you installed it
sudo apt-get remove libopenblas-base
# Download the development version of OpenBLAS
git clone git://github.com/xianyi/OpenBLAS
cd OpenBLAS
make FC=gfortran
sudo make PREFIX=/usr/local/ install
# Tell Theano to use OpenBLAS.
# This works only for the current user.
# Each Theano user on that computer should run that line.
echo -e "\n[blas]\nldflags = -lopenblas\n" >> ~/.theanorc
Contributed GPU instruction
The first thing you’ll need for Theano to use your GPU is Nvidia’s GPU-programming toolchain. You should install at least the CUDA driver and the CUDA Toolkit, as described here. On Ubuntu 14.04:
$ sudo apt-get install nvidia-current
$ sudo apt-get install nvidia-cuda-toolkit # As of October 31th, 2014, provide cuda 5.5, not the latest cuda 6.5
If you want cuda 6.5, you can download packages from nvidia for Ubuntu 14.04.
If you downloaded the run
package (the only one available for CUDA 5.0 and older), you install it like this:
$ chmod a+x XXX.sh
$ sudo ./XXX.sh
Since CUDA 5.5 provied a DEB packages, you need to run this on Ubuntu 14.04:
$ sudo apt-get update
$ sudo apt-get install cuda
Then reboot the machine to launch it. To test that it was loaded correctly after reboot, try the command:
$ nvidia-smi
Now, you must then tell Theano where the CUDA root folder is, and there are three ways to do it. Any one of them is enough.
- Define a $CUDA_ROOT environment variable to equal the cuda root directory, as in
CUDA_ROOT=/path/to/cuda/root
, or - add a
cuda.root
flag to THEANO_FLAGS, as inTHEANO_FLAGS='cuda.root=/path/to/cuda/root'
, or - add a
[cuda]
section to your .theanorc file containing the optionroot = /path/to/cuda/root
.
Once that is done, the only thing left is to change the device
option to name the GPU device in your computer, and set the default floating point computations to float32. For example: THEANO_FLAGS='cuda.root=/path/to/cuda/root,device=gpu,floatX=float32'
. You can also set these options in the .theanorc file’s [global]
section:
[global]
device = gpu
floatX = float32
Basic configuration for the GPU Using the GPU.