【问题标题】:Tensorflow dataset.shuffle seems not shuffle without repeat()Tensorflow dataset.shuffle 似乎没有重复()
【发布时间】:2019-07-03 11:44:26
【问题描述】:

我的代码与tensorflow 2.0 tutorial 具有相似的模式。 我希望我的数据集对象在每个时期都重新洗牌。

dataset = tf.data.Dataset.from_tensor_slices(['a','b','c','d'])
dataset = dataset.shuffle(100)

for epoch in range(10):
    for d in dataset:
        print(d)

结果:

tf.Tensor(b'c', shape=(), dtype=string)
tf.Tensor(b'a', shape=(), dtype=string)
tf.Tensor(b'b', shape=(), dtype=string)
tf.Tensor(b'd', shape=(), dtype=string)
tf.Tensor(b'c', shape=(), dtype=string)
tf.Tensor(b'a', shape=(), dtype=string)
tf.Tensor(b'b', shape=(), dtype=string)
tf.Tensor(b'd', shape=(), dtype=string)
...

似乎数据集并没有为每个时期洗牌。 我应该为每个 epoch 调用 .shuffle() 吗?

【问题讨论】:

    标签: tensorflow tensorflow-datasets tensorflow2.0


    【解决方案1】:

    是的,您应该在内循环期间调用.shuffle。此外,当有与 Python 语句等效的纯 tf.* 方法可用时,最好不要将 Python 代码和 TensorFlow 代码混用。

    import tensorflow as tf
    
    dataset = tf.data.Dataset.from_tensor_slices(["a", "b", "c", "d"])
    # dataset = dataset.shuffle(2)
    
    
    @tf.function
    def loop():
        for epoch in tf.range(10):
            for d in dataset.shuffle(2):
                tf.print(d)
    
    
    loop()
    
    

    循环调用每次都会产生不同的值(tf.print 打印 tf.Tensor 的内容,与打印对象的 print 不同)。

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2014-03-11
      • 2013-09-11
      相关资源
      最近更新 更多