dataset.shuffle()
时间: 2023-10-16 11:11:03 浏览: 49
This method shuffles the dataset randomly, meaning the order of the examples in the dataset will be randomized. This is often useful when training machine learning models, as it can help prevent the model from overfitting to a specific order of examples in the dataset.
Example usage:
```python
import tensorflow as tf
dataset = tf.data.Dataset.range(10)
dataset = dataset.shuffle(buffer_size=10) # shuffles the dataset randomly
```
In this example, `buffer_size` specifies the number of elements from the dataset that should be loaded into a buffer and randomly sampled from to create the shuffled dataset. In this case, since the dataset only has 10 elements, we set the buffer size to 10 to ensure that all elements are included in the shuffling process.