Deep Learning:Windows 10 64-bit安装Theano,配置GPU攻略
by.sunzh6-sysu
操作系统:Windows10 64位
显卡:GeForce GTX 960M
第一步:安装Anaconda
下载地址:https://www.continuum.io/downloads
我使用的是:Windows 64-Bit Python 2.7 Graphical Installer
使用conda list命令(列出Anaconda安装的所有应用包),我们可以看到Anaconda已经安装了numpy,nose, pip, python, scipy等。
第二步:安装Theano
使用pip install theano命令安装Theano,安装条件(numpy>=1.5.0,scipy>=0.7.2)会被检查,安装完成后会显示successfullyinstalled theano。
第三步:安装mingw和libpythoon
使用conda install (or pip install)安装mingw和libpython。(非常重要!)
第四步:安装Visual Studio 2013和CUDA 7.5
很多教程推荐的CUDA 5.5一直提示显卡不兼容,折腾半天果断下了最新版(⊙o⊙)…:https://developer.nvidia.com/cuda-downloads
(当然,最重要的是显卡要支持CUDA哦…)
问题又来了,VS2008明明在那里,CUDA7.5说它找不到…果断装了VisualStudio 2013:https://www.visualstudio.com/downloads/download-visual-studio-vs#
先装VS再装CUDA,一路NEXT~
第五步:配置.theanorc.txt文件(非常重要!)
为了使用GPU,我们需要在cmd的根目录下(打开cmd时显示的目录)新建一个.theanorc.txt文件,内容如下
[global]
floatX =float32
device =gpu0
[cuda]
root =C:\CUDA\v7.5\bin
[blas]
ldflags=
[gcc]
cxxflags =-IC:\Users\***\Anaconda\MinGW
[nvcc]
fastmath =True
flags=-LC:\Users\***\Anaconda\libs
base_compiledir=path_to_a_directory_without_such_characters
compiler_bindir=C:\ProgramFiles (x86)\Microsoft Visual Studio 12.0\VC\bin
保存后请重启~
第六步:验证numpy等库是否使用了BLAS加速
import numpy
id(numpy.dot) == id(numpy.core.multiarray.dot)
结果为False表示成功依赖了BLAS加速。
第七步:验证能否使用GPU加速
官方提供的验证代码:
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 %f seconds" % (iters, t1 - t0))
print("Result is %s" % (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')
运行结果:
Using gpudevice 0: GeForce GTX ***
[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32,vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]
Looping 1000times took 0.638810873032 seconds
Result is [1.23178029 1.61879349 1.52278066 ..., 2.20771813 2.29967761
1.62323296]
Used the gpu
大功告成!
参考:
1、 http://deeplearning.net/software/theano/tutorial/using_gpu.html#using-gpu
2、 http://blog.163.com/yuyang_tech/blog/static/216050083201469101518900
3、 http://blog.csdn.net/chlele0105/article/details/40557259