2017-04-06 59 views
2

我已经编写了一些基本的Deep Learning代码,其中有2个LSTM层。我使用KerasTheano作为我的后端。我的机器上的这段代码在AWS上比在AWS上的另一台机器上花费的时间太长。在运行速度更快的机器上,每个纪元需要640秒,而在运行速度较慢的机器上,每个纪元需要超过10,000秒。确保Python代码是否在GPU或CPU上运行

我开始认为较慢机器上的代码没有在GPU上运行。两台机器上运行的代码完全相同。机器配置也是一样的。

看起来像是Theano安装在较慢的机器上。我跑了下面的代码,并得到了结果:

enter image description here

有没有一种方法来检查,如果我的代码是在GPU或CPU上运行?

在这方面的任何帮助将不胜感激。

TIA。

编辑

按照从@Marcin提醒,我添加以下代码:

enter image description here

但是当我运行下面的代码,我仍然得到Used the cpu结果:

enter image description here

回答

1

有s everal方法可以检查:

  1. 入住Theano标志:

    import os 
    print(os.environ["THEANO_FLAGS"]) 
    

    ,看看device设置。

  2. 尝试运行该代码段提供here

代码:

from theano import function, config, shared, tensor 
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([], tensor.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, 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') 

编辑:

尝试增加这个片段作为前两个(重要)线您的代码:

import os 
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32" 
+0

非常感谢回复。我运行了你的一段代码,并且确实得到了'使用cpu'。你会碰巧知道如何改变它以使用'GPU'? – Patthebug

+0

你能打印出'theano'标志的结果吗? –

+0

哦,上帝 - 看来你还没有安装'Theano'。这也可以解释巨大的训练时间。 –