【问题标题】:How tf.train.shuffle_batch works?tf.train.shuffle_batch 如何工作?
【发布时间】:2018-01-20 19:02:24
【问题描述】:

它是在一个时期内进行一次洗牌,还是其他?

tf.train.shuffle_batch 和 tf.train.batch 有什么区别?

有人能解释一下吗?谢谢。

【问题讨论】:

  • 我投票结束这个问题,因为我什至没有尝试阅读文档

标签: tensorflow


【解决方案1】:

首先看一下文档(https://www.tensorflow.org/api_docs/python/tf/train/shuffle_batchhttps://www.tensorflow.org/api_docs/python/tf/train/batch)。内部批处理是围绕 FIFOQueue 构建的,shuffle_batch 是围绕 RandomShuffleQueue 构建的。

考虑以下玩具示例,它将 1 到 100 放入一个常量中,该常量通过 tf.train.shuffle_batch 和 tf.train.batch 输入,然后打印结果。

import tensorflow as tf
import numpy as np

data = np.arange(1, 100 + 1)
data_input = tf.constant(data)

batch_shuffle = tf.train.shuffle_batch([data_input], enqueue_many=True, batch_size=10, capacity=100, min_after_dequeue=10, allow_smaller_final_batch=True)
batch_no_shuffle = tf.train.batch([data_input], enqueue_many=True, batch_size=10, capacity=100, allow_smaller_final_batch=True)

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    for i in range(10):
        print(i, sess.run([batch_shuffle, batch_no_shuffle]))
    coord.request_stop()
    coord.join(threads)

产量:

0 [array([23, 48, 15, 46, 78, 89, 18, 37, 88,  4]), array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])]
1 [array([80, 10,  5, 76, 50, 53,  1, 72, 67, 14]), array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20])]
2 [array([11, 85, 56, 21, 86, 12,  9,  7, 24,  1]), array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30])]
3 [array([ 8, 79, 90, 81, 71,  2, 20, 63, 73, 26]), array([31, 32, 33, 34, 35, 36, 37, 38, 39, 40])]
4 [array([84, 82, 33,  6, 39,  6, 25, 19, 19, 34]), array([41, 42, 43, 44, 45, 46, 47, 48, 49, 50])]
5 [array([27, 41, 21, 37, 60, 16, 12, 16, 24, 57]), array([51, 52, 53, 54, 55, 56, 57, 58, 59, 60])]
6 [array([69, 40, 52, 55, 29, 15, 45,  4,  7, 42]), array([61, 62, 63, 64, 65, 66, 67, 68, 69, 70])]
7 [array([61, 30, 53, 95, 22, 33, 10, 34, 41, 13]), array([71, 72, 73, 74, 75, 76, 77, 78, 79, 80])]
8 [array([45, 52, 57, 35, 70, 51,  8, 94, 68, 47]), array([81, 82, 83, 84, 85, 86, 87, 88, 89, 90])]
9 [array([35, 28, 83, 65, 80, 84, 71, 72, 26, 77]), array([ 91,  92,  93,  94,  95,  96,  97,  98,  99, 100])]

【讨论】:

  • 注意,正如你提到的阅读文档,它现在被列为弃用,建议现在使用tf.data.Dataset.shuffle(min_after_dequeue).batch(batch_size)
【解决方案2】:

tf.train.shuffle_batch() 对每个 epoch 进行洗牌。

【讨论】:

  • 请明确。例如,它是否会在不改变每批内容的情况下洗牌?它是否对批次中的所有项目进行洗牌?它会打乱批次的顺序吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2017-05-01
  • 2018-02-09
相关资源
最近更新 更多