簡體   English   中英

TensorFlow輸入管道性能

[英]TensorFlow input pipeline performance

TL; DR:帶有tfrecords的GPU使用不足。 問題以粗體顯示。


我看到100%的CPU使用率和14%的GPU使用率。 我認為我的輸入管道是瓶頸。 硬件:

  • Intel i5-4460 @ 3.20GHz(4核)
  • NVIDIA GeForce GTX 1050 Ti

我使用自定義軟件構建了一個6GB的tfrecord -file。 但是我沒有使用默認的tf.train.Example協議緩沖區方法來對tfrecord -file的記錄進行形式編碼。

相反,我自己做一些位魔術,看起來像這樣:

def parse_fn(record):
    record = tf.decode_raw(record, tf.uint8, little_endian=True)
    record = tf.reshape(record, (1, 8 + 12 + 4 * num_features + 4 * num_labels))
    time, pair, features, labels = tf.split(record, [8, 12, 4 * num_features, 4 * num_labels], axis=1)
    time = tf.bitcast(time, tf.int64, name="time")
    features = tf.bitcast(tf.reshape(features, (num_features, 4)), tf.float32, name="features")
    labels = tf.bitcast(tf.reshape(labels, (num_labels, 4)), tf.float32, name="features")

    time = tf.reshape(time, ())
    pair = tf.reshape(pair, (-1, 12))

    return time, pair, features, labels

這是TFRecordDataset的映射器函數,我通過這種方式創建該函數:

def create_dataset(filename):
    ds = tf.data.TFRecordDataset(filename)
    ds = ds.map(map_func=parse_fn, num_parallel_calls=2)
    ds = ds.prefetch(buffer_size=16 * 128)
    ds = ds.shuffle(buffer_size=8 * 128)
    ds = ds.batch(batch_size=128)
    return ds

我對此有兩個問題:

  • 就速度而言,這個基於decode_raw / bitcast / reshape / reshape的映射器功能是否存在問題? 示例協議緩沖區格式示例會更快嗎?
  • create_dataset()中的調用順序( mapprefetchshufflebatch create_dataset()最優?

最后,我擔心,由於我的小批量大小為128,並且每個訓練時期運行±64000個小批量,因此Python在訓練循環中花費了很多時間。 在TensorFlow C ++后端運行火車循環的情況下,還有其他更好的選擇嗎? 我當前的Python訓練循環如下所示:

with sess.as_default():
    for k in range(0, 400): #epoch loop
        sess.run(iterator.initializer, feed_dict={filenames: ["train.tfrecord"]})
        sum_tl = 0
        sum_ll = 0
        sum_tll = 0
        count = 0
        while True:
            try:
                lspeed = 5e-5
                _, _, r_tl, r_ll, r_tll, r_summary = sess.run([dataset_next, optimizer, target_loss, label_loss, tweaked_label_loss, merged_summary_op],
                                                        feed_dict={is_training: True, dropout: 0.15, feature_noise_stddev: 0.07, learning_speed: lspeed, l2reg_strength: 2e-5})
                sum_tl += r_tl
                sum_ll += r_ll
                sum_tll += r_tll
                count += 1
                if count % 100 == 0:
                    train_writer.add_summary(r_summary, super_k)
                if count % 5000 == 1:
                    train_writer.flush()
                    print("Epoch " + str(k) + " / mini-batch " + str(count-1) + " : " + str(sum_tl/count) + " / " + str(np.sqrt(sum_ll/count)) + " / " + str(np.sqrt(sum_tll/count)))
            except tf.errors.OutOfRangeError:
                  break
            super_k += 1
        batch_rmse = tf.Summary(value=[
            tf.Summary.Value(tag="loss/target_batch",  simple_value=sum_tl/count), 
            tf.Summary.Value(tag="rmse/batch",         simple_value=np.mean(np.sqrt(sum_ll/count))), 
            tf.Summary.Value(tag="rmse/batch_0",       simple_value=np.sqrt(sum_ll[0]/count)), 
            tf.Summary.Value(tag="rmse/batch_1",       simple_value=np.sqrt(sum_ll[1]/count)), 
            tf.Summary.Value(tag="rmse/batch_2",       simple_value=np.sqrt(sum_ll[2]/count)), 
            tf.Summary.Value(tag="rmse/batch_3",       simple_value=np.sqrt(sum_ll[3]/count)), 
            tf.Summary.Value(tag="rmse/batch_4",       simple_value=np.sqrt(sum_ll[4]/count)), 
            tf.Summary.Value(tag="tweaked_rmse/batch", simple_value=np.mean(np.sqrt(sum_tll/count))), 
        ])
        train_writer.add_summary(batch_rmse, super_k)
        print("Epoch " + str(k) + " : " + str(sum_tl/count) + " / " + str(np.sqrt(sum_ll/count)) + " / " + str(np.sqrt(sum_tll/count)))
        save()
        predict_test(super_k)

就速度而言,這個基於解碼/原始/位廣播/重塑的映射器功能是否存在問題? 示例協議緩沖區格式示例會更快嗎?

不是從它的外觀。 我不太了解內部Tensorflow的投放情況如何; 您可以安排時間確定操作。 但是我會說你在那方面很好。

我要看的是這個回購 ,它可以用更少的轉換來簡化解析過程。 我不知道您的.tfrecords文件的組成,但也許您可以適應它。

create_dataset()中的調用順序(映射,預取,隨機播放,批處理)是否最佳?

為此,您應該閱讀TF Wiki上的《 輸入管道性能指南》 根據我在SO上閱讀的內容,最后調用prefetch表現出最佳性能。 在那里也建議使用所有(4個)CPU內核作為map方法。


就個人而言,我也將您的.tfrecords文件分片成較小的塊,以使讀取成本更低。 我認為您的CPU可以全力讀取6GB的文件,以至於在執行您真正關心的操作時,它的速度變慢了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM