ubuntu16.04 Anaconda虚拟环境配置theano gpu版本

配置过程主要分为下面几个步骤
1. 安装theano1.0.2
2. 安装Lasagne 0.2 dev1
3. 安装OpenCV 3.4
4. 安装cuda和cudnn
5. 配置theanorc文件
下面展开列出详细达安装过程

安装theano1.0.2、lasagne0.2、Opencv

创建并激活环境,然后使用conda安装

#python使用2.7,环境名为theano
conda create -n theano python=2.7

#激活环境
source activate theano

#安装theano和lasagne
#下面两行命令会安装最新版本达theano和lasagne
pip install --upgrade https://github.com/Theano/Theano/archive/master.zip
pip install --upgrade https://github.com/Lasagne/Lasagne/archive/master.zip

#安装Opencv
pip install opencv-python

安装cuda和cudnn

下面这篇文章写得很详细,注意cudnn要求5.1版本,cuda我配的是8.0
https://blog.csdn.net/allenlzcoder/article/details/78702279

配置theanorc文件

sudo gedit ~/.theanorc

在里面写入

[global]
device=gpu
floatX=float32
root=/usr/local/cuda-8.0
[nvcc]
fastmath = True
[blas]
ldflags = -lopenblas
[cuda]
root = /usr/local/cuda-8.0
#下面是内存分配,可以注释掉
[lib]
cnmem =3000

测试

测试文件

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'

输出结果

(theano) cyc@cyc-Inspiron-5577:~$ python a.py
WARNING (theano.sandbox.cuda): The cuda backend is deprecated and will be removed in the next release (v0.10).  Please switch to the gpuarray backend. You can get more information about how to switch at this URL:
 https://github.com/Theano/Theano/wiki/Converting-to-the-new-gpu-back-end%28gpuarray%29

Using gpu device 0: GeForce GTX 1050 (CNMeM is disabled, cuDNN 5110)
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000 times took 0.335947036743 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu

配置完成,可以用啦

猜你喜欢

转载自blog.csdn.net/cyc651130/article/details/81412358