2017-04-06 58 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'。這也可以解釋巨大的訓練時間。 –