2017-08-01 50 views
2

我有一個在CPU上動態生成的數據集。樣本是通過函數make_sample在python中計算的,它非常複雜,並且不能被翻譯成tensorflow操作。由於樣例生成非常耗時,因此我想從多個線程調用函數來填充輸入隊列。在CPU預處理期間使用多線程時Tensorflow速度較慢

我從example given in the documentation開始,得出了以下的玩具例子:

import numpy as np 
import tensorflow as tf 
import time 

def make_sample(): 
    # something that takes time and needs to be on CPU w/o tf ops 
    p = 1 
    for n in range(1000000): 
    p = (p + np.random.random()) * np.random.random() 
    return np.float32(p) 

read_threads = 1 

with tf.device('/cpu:0'): 
    example_list = [tf.py_func(make_sample, [], [tf.float32]) for _ in range(read_threads)] 
    for ex in example_list: 
    ex[0].set_shape(()) 
    batch_size = 3 
    capacity = 30 
    batch = tf.train.batch_join(example_list, batch_size=batch_size, capacity=capacity) 

with tf.Session().as_default() as sess: 
    tf.global_variables_initializer().run() 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
    try: 
    # dry run, left out of timing 
    sess.run(batch) 
    start_time = time.time() 
    for it in range(5): 
     print(sess.run(batch)) 
    finally: 
    duration = time.time() - start_time 
    print('duration: {0:4.2f}s'.format(duration)) 
    coord.request_stop() 
    coord.join(threads) 

最讓我驚訝的是,增加read_threads時,CPU使用率永遠不會超過50%。更糟的是,計算時間驟降:我的電腦上,

  • read_threads=1duration: 12s
  • read_threads=2duration: 46s
  • read_threads=4duration: 68s
  • read_threads=8duration: 112s

是否有一個解釋,最重要的是,獲得高效多線程的解決方案在tensorflow上使用自定義python函數生成數據?

回答

2

tf.py_func重用現有的Python解釋器。不幸的是,Python支持併發而不是並行。換句話說,你可以有多個Python線程,但只有一個可以隨時執行Python代碼。標準解決方案是將生成管道移動到TensorFlow/C++中,或使用多個Python進程和附加層來聚合其結果(即,使用ZMQ彙總來自多個Python進程的結果)

相關問題