最近在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)下。