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

配置完成,可以用啦

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值