ubuntu 16.04 theano GPU加速測試程式碼過程示例
阿新 • • 發佈:2018-12-22
在看這篇部落格之前,作者的執行環境首先要裝好cuda和cudnn。然後我的環境為:
ubuntu 16.04
anaconda
python 3.6
theano 1.0.2 py36h6bb024c_0
首先安裝pygpu
conda install -c conda-forge pygpu
然後進行theano-gpu測試,我的測試程式碼為theano-gpu-test.py
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 range(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')
執行的命令gpu版本:
THEANO_FLAGS=mode=FAST_RUN,device=cuda,floatX=float32 python theano-gpu-test.py
我的輸出是:
Using cuDNN version 6021 on context None Mapped name None to device cuda: GeForce GTX 1080 Ti (0000:03:00.0) [GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float32, vector)>), HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)] Looping 1000 times took 0.23069453239440918 seconds Result is [1.2317803 1.6187935 1.5227807 ... 2.2077181 2.2996776 1.623233 ] Used the cpu #這行沒有參考意義,看上面的用時
執行命令CPU版本:
python theano-gpu-test.py
我的電腦輸出為:
[Elemwise{exp,no_inplace}(<TensorType(float64, vector)>)]
Looping 1000 times took 26.147013664245605 seconds
Result is [1.23178032 1.61879341 1.52278065 ... 2.20771815 2.29967753 1.62323285]
Used the cpu #這行沒有參考意義,看上面的用時
用GPU執行程式花了0.23秒,用CPU執行程式花了26秒
如果不想每次用gpu的時候加上前面的一長串,則需要配置.theanorc檔案,然後就直接輸入python theano-gpu-test.py命令就行了,我這裡把我配置的過程分享一下:
vim ~/.theanorc
然後加上
[global]
floatX=float32
device=cuda
按esc :wq退出就行了,親測可用。
參考文獻
[1].Theano2.1.12-基礎知識之使用GPU.https://www.cnblogs.com/shouhuxianjian/p/4590224.html
[2].Theano坑--無法呼叫GPU. https://www.jianshu.com/p/12af936e20ef
[3].gpuarray. http://deeplearning.net/software/libgpuarray/installation.html