ubuntu14.04安装theano

基本上按照官网来就行:

先是

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

再是

sudo pip install Theano

(这一步骤里,如果你默认是python3,则要改为pip-2.7)

测试下:

Test the newly installed packages

python -c "import numpy; numpy.test()"   约~30s
python -c "import scipy; scipy.test()"   约~1m
python -c "import theano; theano.test()" 约~30m

Speed test Theano/BLAS

python `python -c "import os, theano; print os.path.dirname(theano.__file__)"`/misc/check_blas.py

测试成功,基本上就能用了。

以下是GPU设置:

检查自己的电脑是否有cuda支持的GPU

lspci | grep -i nvidia

一般得到类似下面的结果

01:00.0 VGA compatible controller: NVIDIA ……

另外,也可用

lspci | grep -i vga

 显示所有显卡,用

lspci -v -s 01:00.0

显示特定显卡详细信息。

如果支持,那么安装就行了:

sudo apt-get install nvidia-current
sudo apt-get install nvidia-cuda-toolkit

然后主要是设置参数,让优先使用GPU

参见http://deeplearning.net/software/theano/install.html里Using the GPU一节。

主要有两种方法,一种设置THEANO_FLAGS,一种编辑.theanorc文件,以后者为例,

在$home目录(用户主目录,一般是/home/用户名)下创建“ .theanorc”文件,编辑为

[global]
device = gpu
floatX = float32

[cuda]
root = /usr/lib/nvidia-cuda-toolkit    //cuda目录一项设置为自己的cuda安装目录即可(内含bin,lib,include子文件夹)

注意,这个文件你创建了,一般会变为不可见,可用"ls -a"命令列出所有文件从而可以看到,可以用gedit编辑。

此外,还要设置cuda的lib子文件夹(64位下可能还要设置lib64子文件夹)位置到环境变量$LD_LIBRARY_PATH,参见http://blog.csdn.net/xsc_c/article/details/23470565(这篇博客cuda的几个入门文章也很不错)。或者这篇http://www.linuxidc.com/Linux/2012-04/58913.htm 。以设置/etc/profile为例,在其中添加

export PATH=$PATH:/usr/lib/nvidia-cuda-toolkit/bin  %这句不加一般也行
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia-cuda-toolkit/lib

测试例子在http://deeplearning.net/software/theano/tutorial/using_gpu.html#using-gpu

from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print f.maker.fgraph.toposort()
t0 = time.time()
for i in xrange(iters):
    r = f()
t1 = time.time()
print 'Looping %d times took' % iters, t1 - t0, 'seconds'
print 'Result is', r
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print 'Used the cpu'
else:
    print 'Used the gpu'

如果执行时有类似“unable to get the number of gpus avaliable”的错误,用sudo执行即可。

如果有权限问题,如"Permission denied"类似错误,删除主目录下的文件夹".theano"或者其子文件夹,

比如,在我的电脑上,存在这样的文件夹"/home/用户名/.theano/compiledir_Linux-3.16--generic-x86_64-with-Ubuntu-14.04-trusty-x86_64-2.7.6-64/",将其删除即可.

注意,".theano"是隐藏的文件夹,用"ls -a"命令可以看到.