本机之前已经安装了cuda+opencv+cudnn+Anaconda+caffe,在此基础上在安装Theano
方法:
首先是安装theano
conda install theano
配置
sudo gedit ~/.theanorc
配置文件内容:
注:如果不知道cuda的位置,可以通过whereis cuda
进行查找
[global]
floatX=float32
device=gpu
base_compiledir=~/external/.theano/
allow_gc=False
warn_float64=warn
[mode]=FAST_RUN
[nvcc]
fastmath=True
[cuda]
root=/usr/local/cuda
配置完成之后保存
python
然后
import theano
出现关于显卡的信息说明正确
Using gpu device 0: GeForce GTX 1080 Ti (CNMeM is disabled, cuDNN 6021)
/home/andy/anaconda2/lib/python2.7/site-packages/theano/sandbox/cuda/__init__.py:631: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.1.
warnings.warn(warn)
当然你也可以通过运行一个小demo进行测试
代码如下
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 range(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')
结果如下:
Using gpu device 0: GeForce GTX 1080 Ti (CNMeM is disabled, cuDNN 6021)
/home/andy/anaconda2/lib/python2.7/site-packages/theano/sandbox/cuda/__init__.py:631: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.1.
warnings.warn(warn)
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
('Looping 1000 times took', 0.17880511283874512, 'seconds')
('Result is', array([ 1.23178029, 1.61879349, 1.52278066, ..., 2.20771813,
2.29967761, 1.62323296], dtype=float32))
Used the gpu
显示
Using gpu device 0: GeForce GTX 1080 Ti (CNMeM is disabled, cuDNN 6021)
/home/andy/anaconda2/lib/python2.7/site-packages/theano/sandbox/cuda/__init__.py:631: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.1.
warnings.warn(warn)
使用下面的语句进行更新
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
出现了下面的错误
'You are tring to use the old GPU back-end. '
ValueError: You are tring to use the old GPU back-end. It was removed from Theano. Use device=cuda* now. See https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29 for more information.
然后我更改了theanorc文件
https://stackoverflow.com/questions/44765376/valueerror-you-are-trying-to-use-the-old-gpu-back-end-when-importing-keras
http://deeplearning.net/software/theano/library/config.html
[global]
floatX=float32
#device=gpu
device=cuda0
base_compiledir=~/external/.theano/
allow_gc=False
warn_float64=warn
[mode]=FAST_RUN
[nvcc]
fastmath=True
[cuda]
root=/usr/local/cuda
接下来报错如下
ValueError: Your installed version of pygpu(0.6.9) is too old, please upgrade to 0.7.0 or later (but below 0.8.0)
然后只能
conda install pygpu
再次import theano
>>> import theano
Using cuDNN version 6021 on context None
Mapped name None to device cuda0: GeForce GTX 1080 Ti (0000:06:00.0)