TensorFlow:当批次完成训练后,tf.train.batch是否会自动加载下一个批次? [英] TensorFlow: does tf.train.batch automatically load the next batch when the batch has finished training?

查看:426
本文介绍了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,高度,宽度,num_channels]的单个批次大小,但是

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?

谢谢您的帮助.

推荐答案

... tf.train.batch是否自动将另一批数据输入会话?

... does tf.train.batch automatically feeds in another batch of data to the session?

不.什么也不会自动发生.您必须再次调用sess.run(...)才能加载新批次.

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

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

Does this mean even without a loop, the next batch could be automatically fed?

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

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是否会自动加载下一个批次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆