Theano里配置GPU的新方法


这几天在研究pylearn2,里面有要用到GPU,之前一直很疑惑:明明显卡是支持CUDA的,CUDA也成功安装了,也可以检测得到GPU,为什么在python程序里面就是没法运用GPU

运行下面这个检测程序

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 08:09:11 2015

@author: Administrator
"""
import theano
import theano
from theano import function, config, shared
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 100

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 %f seconds" % (iters, t1 - t0))
print theano.config.device
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print isinstance(x.op, T.Elemwise)
    print('Used the cpu')
else:
    print('Used the gpu')


显示的也是CPU

因此,我只能怀疑是.theanorc.txt的配置出了问题,好在还有另外一种方式来配置GPU,那就是设置如下的环境变量


里面的具体内容为:

nvcc.compiler_bindir=E:\\Microsoft Visual Studio 12.0\\VC\\bin,base_compilerdir=path_to_a_directory_without_such_characters,floatX=float32,device=gpu,nvcc.fastmath=True,nvcc.flags=-LE:\\Anaconda\\libs,gcc.cxxflags = -IE:\\Anaconda\\MinGW,blas.ldflags=,flags =  -arch=sm_30 
nvcc fatal:Cannot find compiler 'cl.exe' in PATH

再在path里面加入cl.exe的路径,这样就配置完成了,在cmd里面运行一下



大功告成!

你可能感兴趣的:(环境搭建)