TensorFlow dataset.shuffle() behavior when used with repeat() and batch()

拥有回忆 提交于 2020-01-23 20:45:06

问题


What exactly will this do?

dataset = tf.data.Dataset.from_tensor_slices([0, 0, 0, 1, 1, 1, 2, 2, 2])
dataset.shuffle(buffer_size=5).repeat().batch(3)

I've noticed several related questions but none of them answered exactly my concern. I'm confused with what shuffle(buffer_size) is doing. I understand it will take 5 first examples [0, 0, 0, 1, 1] into memory, but what will it do next with this buffer? And how does this buffer interact with repeat() and batch()?


回答1:


The way shuffle works is complicated, but you can pretend it works by first filling a buffer of size buffer_size and then, every time you ask for an element, sampling a uniformly random position in that buffer and replacing that with a fresh element.

Batching before shuffling means you'll shuffle pre-made minibatches (so the minibatches themselves won't change, just their order) while batching after shuffling lets you change the contents of the batches themselves randomly. Similarly, repeat before shuffling means you will shuffle an infinite stream examples (so the second epoch will have a different order than the first epoch) while repeating after shuffling means you'll always see the same examples in each epoch.



来源:https://stackoverflow.com/questions/51650033/tensorflow-dataset-shuffle-behavior-when-used-with-repeat-and-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!