Py学习  »  Python

即使设备设置为“cuda*”-Python2.7之后,该节点也不使用GPU

poPYtheSailor • 4 年前 • 150 次点击  

我正在探索谷歌合作。我正在运行一个基于theano的程序,由于theano正在使用CPU,这需要很多时间。

  1. 还将环境变量设置为使用cuda。

    os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=cuda*,floatX=float32"
    

当我检查他们使用的设备时 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上工作?我还用以下命令设置了theonar文件:

# 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
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53035
 
150 次点击