我想做一个简单的测试,看看我使用的是theano的GPU版本还是CPU版本的代码:
# Code to test if theano is using GPU or CPU
# Reference: https://stackoverflow.com/questions/34328467/how-can-i-use-my-gpu-on-ipython-notebook
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()
# if you're using Python3, rename `xrange` to `range` in the following line
for i in range(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')
然后这就是我执行它时看到的内容:
Matt @ limbo> THEANO_FLAGS = device = cuda0 python check_theano.py
Using
cuDNN version 5105 on context None
Mapped name None to device cuda0:
Tesla M40 24GB (0000:04:00.0)
[GpuElemwise{exp,no_inplace}(<GpuArrayType<None>(float64, (False,))>),
HostFromGpu(gpuarray)(GpuElemwise{exp,no_inplace}.0)] Looping 1000
times took 0.391111 seconds Result is [1.23178032 1.61879341
1.52278065 ... 2.20771815 2.29967753 1.62323285] Used the cpu
看起来一切都很好,关于pygpu,cudnn的错误都没有,找到了我的gpu卡,所以为什么最后的消息是“使用了cpu”?
我正在使用Theano 0.9.0,pygpu 0.6.2,python 3.5.6,cudnn / 5.1-cuda-8.0.44。
请不要告诉我检查Theano 1. *,因为我需要执行的代码可以在Theano 0.9上运行