本文介绍了TensorFlow:tf.train.batch 是否在批次完成训练后自动加载下一个批次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在我创建了我的操作后,通过操作将批处理数据馈入并运行操作,tf.train.batch 是否会自动将另一批数据馈入会话?

For instance, after I have created my operations, fed the batch data through the operation and run the operation, does tf.train.batch automatically feed in another batch of data to the session?

我问这个是因为 tf.train.batch 有一个 allow_smaller_final_batch 属性,这使得最后一批加载的大小小于指定的批大小.这是否意味着即使没有循环,下一批也可以自动进料?从教程代码我很困惑.当我加载单个批次时,我实际上得到了单个批次大小的形状 [batch_size, height, width, num_channels],但是 文档 说它在张量中创建批量张量.此外,当我阅读 tf-slim 演练教程,里面有个函数叫load_batch,只返回了3个张量:图像,images_raw,标签.文档中解释的批次"数据在哪里?

I ask this because tf.train.batch has an attribute of allow_smaller_final_batch which makes it possible for the final batch to be loaded as a size lesser than the indicated batch size. Does this mean even without a loop, the next batch could be automatically fed? From the tutorial codes I am rather confused. When I load a single batch, I get literally a single batch size of shape [batch_size, height, width, num_channels], but the documentation says it Creates batches of tensors in tensors. Also, when I read the tutorial code in the tf-slim walkthrough tutorial, where there is a function called load_batch, there are only 3 tensors returned: images, images_raw, labels. Where are 'batches' of data as explained in the documentation?

感谢您的帮助.

推荐答案

没有.没有什么会自动发生.您必须再次调用 sess.run(...) 以加载新批次.

No. Nothing happens automatically. You must call sess.run(...) again to load a new batch.

这是否意味着即使没有循环,下一批也可以自动喂食?

没有.tf.train.batch(..) 将始终加载 batch_size 张量.例如,如果您有 100 张图像和 batch_size=30,那么您将有 3*30 个批次,因为您可以在输入之前调用 sess.run(batch) 三次队列将从头开始(如果 epoch=1 则停止).这意味着您会从训练中错过 100-3*30=10 个样本.如果您不想错过它们,您可以执行 tf.train.batch(..., allow_smaller_final_batch=True) 所以现在您将拥有 3x 30-sample-batches 和 1x 10-sample-在输入队列重新启动之前批处理.

No. tf.train.batch(..) will always load batch_size tensors. If you have for example 100 images and a batch_size=30 then you will have 3*30 batches as in you can call sess.run(batch) three times before the input queue will start from the beginning (or stop if epoch=1). This means that you miss out 100-3*30=10 samples from training. In case you do not want to miss them you can do tf.train.batch(..., allow_smaller_final_batch=True) so now you will have 3x 30-sample-batches and 1x 10-sample-batch before the input queue will restart.

让我也用一个代码示例来详细说明:

Let me also elaborate with a code sample:

queue = tf.train.string_input_producer(filenames,
        num_epochs=1) # only iterate through all samples in dataset once

reader = tf.TFRecordReader() # or any reader you need
_, example = reader.read(queue)

image, label = your_conversion_fn(example)

# batch will now load up to 100 image-label-pairs on sess.run(...)
# most tf ops are tuned to work on batches
# this is faster and also gives better result on e.g. gradient calculation
batch = tf.train.batch([image, label], batch_size=100)

with tf.Session() as sess:
    # "boilerplate" code
    sess.run([
        tf.local_variables_initializer(),
        tf.global_variables_initializer(),
    ])
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        # in most cases coord.should_stop() will return True
        # when there are no more samples to read
        # if num_epochs=0 then it will run for ever
        while not coord.should_stop():
            # will start reading, working data from input queue
            # and "fetch" the results of the computation graph
            # into raw_images and raw_labels
            raw_images, raw_labels = sess.run([images, labels])
    finally:
        coord.request_stop()
        coord.join(threads)

这篇关于TensorFlow:tf.train.batch 是否在批次完成训练后自动加载下一个批次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:05