我在这里讲解教程http://deeplearning.net/software/theano/tutorial/using_gpu.html
我使用的代码
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 %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')
在测试GPU的Theano章节中: 有一些命令行可以设置Theano标志以在CPU或GPU上运行。问题是我不知道如何将这些命令放入其中。
我已经尝试过在Windows cmd中执行。
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python theanogpu_example.py
然后我收到了。
'THEANO_FLAGS' is not recognized as an internal or external command,
operable program or batch file.
然而,我能够使用以下命令在CPU上运行代码:
python theanogpu_example.py
我希望在GPU上运行代码,使用教程中的这些命令,应该怎么做?
解决方案
感谢@Blauelf提出的Windows环境变量的想法。
然而,参数必须分开。
set THEANO_FLAGS="mode=FAST_RUN" & set THEANO_FLAGS="device=gpu" & set THEANO_FLAGS="floatX=float32" & python theanogpu_example.py
set THEANO_FLAGS=
行将替换先前的内容(这就是为什么脚本有时包含像set PATH=%PATH%;C:\myDir
这样的代码,其中%PATH%
会自动替换为先前的值),因此在该命令行中,只有最后一个set
实际上会产生影响。 - Blauelf