您的位置:首页 > 其它

测试theano是提示使用CPU而不是gpu问题

2016-10-05 21:28 423 查看
测试theano是提示使用CPU而不是gpu问题:

cuda 和theano配置完成后,进行theano测试 

为了检查你的GPU是否启用了,可以剪切下面的代码然后保存成一个Python文件(我命名为test_gpu1.py),运行看看。

[python] view
plain copy

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'  

直接在终端命令行敲:python test_gpu1.py 

运行结束后提示显示的是Used the cpu!!(后来经过查找资料这种情况其实是由于theano的默认配置中不是使用GPU而是CPU)

反复在根目录下添加环境变量也没能解决。最后解决方法是在命令行上指定模式  并运行test_gpu1.py:

THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python test_gpu1.py

同理如果指定用CPU的话就在终端上敲:

THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python test_gpu1.py

在theano 的官网教程中,配置环境变量有两种方法:

第一种方法是THEANO_FLAGS方法(即上面使用的)

第二种方法是在根目录(home/hf(用户名为hf))下建立.theanorc文件并添加例如类似下面:

[global]

floatX=float32

device=gpu0

[lib]

cnmem=1

但我用第二种方法没能成功,第一种方法确实可行!

****第二种方法没能成功 的原因是:当时我在控制台用的是root身份,而.theanorc 所创建的根目录为home/hf(这是hf用户的根目录!!)所以我在root的根目录(cd $HOME)下创建.theano 就好了!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐