问题
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