张量流随着时期的增加,消耗数据的数据集变得越来越慢

时间:2017-11-25 08:38:46

标签: tensorflow dataset

在张量流1.4中,tf.data.Dataset类提供了一个repeat()函数来制作纪元,但随着纪元的增加,它的性能变得越来越慢!

以下是代码:

num_data = 1000
num_epoch = 50
batch_size = 32
dataset = tf.data.Dataset.range(num_data)
dataset = dataset.repeat(num_epoch).batch(batch_size)
iterator = dataset.make_one_shot_iterator()

with tf.Session() as sess:
    for epoch in xrange(num_epoch):
        t1 = time.time()
        for i in xrange(num_data/batch_size):
            a = sess.run(iterator.get_next())
        t2 = time.time()
        print 'epoch %d comsuming_time %.4f'%(epoch,t2-t1)

及其产出:

epoch 0 comsuming_time 0.1604
epoch 1 comsuming_time 0.1725
epoch 2 comsuming_time 0.1839
epoch 3 comsuming_time 0.1942
epoch 4 comsuming_time 0.2213
epoch 5 comsuming_time 0.2430
epoch 6 comsuming_time 0.2361
epoch 7 comsuming_time 0.2512
epoch 8 comsuming_time 0.2607
epoch 9 comsuming_time 0.2936
epoch 10 comsuming_time 0.3282
epoch 11 comsuming_time 0.2990
epoch 12 comsuming_time 0.3105
epoch 13 comsuming_time 0.3239
epoch 14 comsuming_time 0.3393
epoch 15 comsuming_time 0.3518
epoch 16 comsuming_time 0.3673
epoch 17 comsuming_time 0.3859
epoch 18 comsuming_time 0.3928
epoch 19 comsuming_time 0.4090
epoch 20 comsuming_time 0.4206
epoch 21 comsuming_time 0.4333
epoch 22 comsuming_time 0.4479
epoch 23 comsuming_time 0.4631
epoch 24 comsuming_time 0.4774
epoch 25 comsuming_time 0.4923
epoch 26 comsuming_time 0.5533
epoch 27 comsuming_time 0.5187
epoch 28 comsuming_time 0.5319
epoch 29 comsuming_time 0.5470
epoch 30 comsuming_time 0.5647
epoch 31 comsuming_time 0.5796
epoch 32 comsuming_time 0.6036

1 个答案:

答案 0 :(得分:0)

我想我发现了这个问题。它是sess.run(iterator.get_next())。在循环外预测get_next_op = iterator.get_next()并运行sess.run(get_next_op)就行了。