Step by Step Ubuntu下GPU测试Theano

来源:互联网 发布:电脑语音同声翻译软件 编辑:程序博客网 时间:2024/06/03 18:31

刚刚开始学习Deep Learning


OS:Ubuntu 14.04 CPU:I7-4700MQ GPU:GT 750M

是在这个网站下学习的:http://deeplearning.net

安装Theano:http://deeplearning.net/software/theano/install.html#install安装过程很简单,一步一步做就行了。


使用GPU测试Theano,复制下面的python代码,自建一个py文件:

from theano import function, config, shared, sandboximport theano.tensor as Timport numpyimport timevlen = 10 * 30 * 768  # 10 x #cores x # threads per coreiters = 1000rng = 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', rif numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):    print 'Used the cpu'else:    print 'Used the gpu'

 以下是执行结果:

THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python check1.py[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)]Looping 1000 times took 2.16374897957 secondsResult is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761  1.62323284]Used the cpu
</pre><pre name="code" class="plain">sudo THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python check1.pyUsing gpu device 0: GeForce GT 750M[GpuElemwise{exp,no_inplace}(<CudaNdarrayType(float32, vector)>), HostFromGpu(GpuElemwise{exp,no_inplace}.0)]Looping 1000 times took 0.532066106796 secondsResult is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761  1.62323296]Used the gpu

对比以上结果:

1)执行命令中:前者device=cpu,或者device=gpu

2)显示结果中,比较明显的不同是:Used the cpu计算时间超过2秒,同样的文件Used the gpu计算时间仅用了0.53秒


另外,在我自己执行的过程中,遇到一个问题:

THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python check1.pyWARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available  (error: Unable to get the number of gpus available: unknown error)[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)]Looping 1000 times took 2.19189310074 secondsResult is [ 1.23178029  1.61879337  1.52278066 ...,  2.20771813  2.29967761  1.62323284]Used the cpu
执行命令是device=gpu,但是执行结果出现error,并且计算使用的是cpu。

error显示是不能获得可用的gpu,其实是权限问题,只要在执行命令前加上sudo,就可以了。




0 0
原创粉丝点击