温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Tensorflow dataset.shuffle seems not shuffle without repeat()
tensorflow tensorflow-datasets tensorflow2.0

其他 - Tensorflowdataset.shuffle似乎没有repeat()就没有洗牌

发布于 2020-03-27 11:46:39

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

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)
...

似乎数据集并没有在每个时期都随机播放。我应该为每个时期调用.shuffle()吗?

查看更多

查看更多

提问者
EyesBear
被浏览
188
nessuno 2019-07-03 23:13

是的,您应该.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的是将打印对象)。