我正在探索google-collaboratory。我正在运行一个基于theano的程序,由于theano正在使用CPU,因此该程序会花费很多时间。
还已将环境变量设置为使用cuda。
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=cuda*,floatX=float32"
当我检查theano print(theano.config.device)
使用的设备时,它会给我cuda*
作为输出。
但是当我运行以下代码时:
# Test if Theano is using the GPU
from theano import function, config, shared, tensor
print("\n\nDevice:")
print theano.config.device
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([], tensor.exp(x))
print(f.maker.fgraph.toposort())
print(theano.config.device)
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, tensor.Elemwise) and
('Gpu' not in type(x.op).__name__)
for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Used the gpu')
它显示下面的输出:
Device:
cuda*
[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)]
cuda*
Looping 10000 times took 11.660804 seconds
Result is [1.2317803 1.6187934 1.5227807 ... 2.2077181 2.2996776 1.6232328]
Used the cpu
我应该如何使theano在GPU上运行?我还使用以下命令设置了theonarc文件:
# set Theano config
!echo -e '[global]\ndevice = cuda\nfloatX = float32\n' > ~/.theanorc
!cat ~/.theanorc
[global]
device = cuda
floatX = float32
但是我不确定该文件应在google-collaboratory上的确切位置。当前位于以下位置:
! find / -name ".theanorc"
/root/.theanorc