2016-12-23 88 views
0

我想找到一種方法來加快我的代碼。TensorFlow並行CPU與Numpy

總之,我有一個訓練有素的模型,我使用類似的東西來獲得預測,對它們進行排序並輸出排名。

def predict(feed_dict, truth): 
    # Feed dict contains about 10K candidates to obtain scores 
    pred = self.sess.run([self.mdl.predict_op], feed_dict) 
    pred = np.array(pred) 
    # With the scores, I sort them by likelihood 
    sort = np.argsort(pred)[::-1] 
    # I find the rank of the ground truth 
    rank = np.where(sort==truth)[0][0] + 1 
    return rank 

但是,這個過程非常緩慢。我有大約10K個測試樣本。我相信會話不適合python中的標準多處理庫,而multi-cpu/gpu支持僅適用於tensorflow操作。

是否有任何優雅的方式來加速通過多處理?或者我必須將它作爲TF中計算圖的一部分來實現。

非常感謝!

+0

哪一部分是慢? –

+0

順便提一下,'tf.nn.top_k(pred)[1]'和你的'np.argsort'行是一樣的。如果您將所有內容都轉換爲TF圖,則不需要多處理 - 並行'session.run'調用可以在同一進程中從不同的Python線程啓動。 –

+0

緩慢來自我必須每次在有效或測試集上調用10K +次的事實。 – op10no4

回答

3

您可以翻譯整個事情到TensorFlow圖:

pred_op = tf.constant([1,2,0]) 
truth = [0, 1, 2] 
sess = tf.Session() 
sort = tf.nn.top_k(pred_op)[1] # same as np.argsort(x)[::-1] 
rank = tf.where(tf.equal(sort,truth))[0][0] + 1 
print(sess.run(rank)) # => 2