How can I use tf.data Datasets in eager execution mode?
在2018年TensorFlow开发峰会上的tf.data演讲中,Derek Murray提出了一种将
1 2 3 4 5 6 7 | import tensorflow as tf tf.enable_eager_execution() dataset = tf.data.Dataset.from_tensor_slices(tf.random_uniform([50, 10])) dataset = dataset.batch(5) for batch in dataset: print(batch) |
引起
1 | TypeError: 'BatchDataset' object is not iterable |
我还尝试使用
1 | RuntimeError: dataset.make_one_shot_iterator is not supported when eager execution is enabled. |
和
1 | RuntimeError: dataset.make_initializable_iterator is not supported when eager execution is enabled. |
TensorFlow版本:1.7.0,Python版本:3.6
如何在急切执行中使用
1 2 3 4 5 6 | import tensorflow.contrib.eager as tfe dataset = tf.data.Dataset.from_tensor_slices(tf.random_uniform([50, 10])) dataset = dataset.batch(5) for batch in tfe.Iterator(dataset): print(batch) |
有了TF 2.1,
您可以这样创建一个迭代器:
1 | iterator = iter(dataset) |
并获取下一批值:
1 | batch = iterator.get_next() |