Sam.Wang

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

最近在implement DeepLearning.net上面的程序。对于开源的python,最头疼的就是各种package和各种configuration. 而且还是在windows下。

想要让theano在windows下能GPU并行,总结配置如下:

1. 下载CUDA,安装,重启(重要)!

2. 下载Canopy, 申请academic free lincense.

3. Canopy中下载package, 由于numpy, scipy都集成了,主要下载:pip, mingw, libpython, urllib, theano这几个

4. 在C:\Users\<User name>\ (所谓的Home,或根目录)下建立一个文件 .theanorc.txt或.theanorc, 里面复制如下内容:

[global]
device = gpu
floatX=float32

[nvcc]
flags=-LC:\Users\Sam\AppData\Local\Enthought\Canopy32\User\libs
compiler_bindir=D:\Program Files\Microsoft Visual Studio 10.0\VC\bin

[blas]
ldflags = -LD:\Program Files\Enthought\Canopy32\App\appdata\canopy-1.1.0.1371.win-x86\Scripts -lmk2_core -lmk2_intel_thread -lmk2_rt

  注意:由于windows下创建文件必须有文件名,这里我们用canopy创建一个文件,然后保存为.theanorc.txt或.theanorc在根目录下(C:\Users\<User name>\).

5. 测试theano环境。

import theano

  如果不报错,恭喜你已经完成了测试第一步。

6. 运行下面程序。(来自http://deeplearning.net)

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 = 10000

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'

  这时,CUDA的nvcc不断的出现,说明我们的GPU运行已经调试可运行。之后就是本段程序的运行。如果还是CPU,时间大约在20秒左右(跟机器配置相关,我的是E8400);但如果是GPU,时间大约是0.6秒左右(我的是9800GT)。可见GPU的速度不是一般的快。

还有几个例子下面一起给大家:

数据:MNIST http://deeplearning.net/data/mnist/mnist.pkl.gz

模型:Logistic Regression using Stochastic Gradient Descent http://deeplearning.net/tutorial/code/logistic_sgd.py

   Multilayer Perceptron http://deeplearning.net/tutorial/code/mlp.py

     Restricted Boltzmann Machine http://deeplearning.net/tutorial/code/rbm.py

     Deep Belief Network http://deeplearning.net/tutorial/code/DBN.py

PS1:上面模型的代码中注意改data的路径,这样就可以直接运行了!

PS2:记得把utils.py(http://deeplearning.net/tutorial/code/utils.py)文件拷贝到一个系统环境目录(如C:\Program Files\Enthought\Canopy32\App\appdata\canopy-1.1.0.1371.win-x86\Lib\site-packages)下。

posted on 2013-11-11 23:07  Sam.Wang  阅读(4185)  评论(0编辑  收藏  举报