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

来源:互联网 发布:个人隐私 知乎 编辑:程序博客网 时间:2024/06/05 21:16

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

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

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

[python] view plain copy
  1. from theano import function, config, shared, sandbox  
  2. import theano.tensor as T  
  3. import numpy  
  4. import time  
  5.   
  6. vlen = 10 * 30 * 768  # 10 x #cores x # threads per core  
  7. iters = 1000  
  8.   
  9. rng = numpy.random.RandomState(22)  
  10. x = shared(numpy.asarray(rng.rand(vlen), config.floatX))  
  11. f = function([], T.exp(x))  
  12. print f.maker.fgraph.toposort()  
  13. t0 = time.time()  
  14. for i in xrange(iters):  
  15.     r = f()  
  16. t1 = time.time()  
  17. print 'Looping %d times took' % iters, t1 - t0, 'seconds'  
  18. print 'Result is', r  
  19. if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):  
  20.     print 'Used the cpu'  
  21. else:  
  22.     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 就好了!!!

2 0