Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use tf.data Datasets in eager execution mode?

In the tf.data talk at the TensorFlow Dev Summit 2018, Derek Murray presented a way to combine the tf.data API with TensorFlow's eager execution mode (at 10:54). I tried out a simplified version of the code shown there:

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)

causing

TypeError: 'BatchDataset' object is not iterable

I also tried using dataset.make_one_shot_iterator() and dataset.make_initializable_iterator() to iterate over the dataset, but they result in

RuntimeError: dataset.make_one_shot_iterator is not supported when eager execution is enabled.

and

RuntimeError: dataset.make_initializable_iterator is not supported when eager execution is enabled.

TensorFlow version: 1.7.0, Python version: 3.6

How can you use the tf.data API with eager execution?

like image 787
Kilian Batzner Avatar asked Apr 04 '18 19:04

Kilian Batzner


People also ask

What is TF eager execution?

With eager execution enabled, TensorFlow functions execute operations immediately (as opposed to adding to a graph to be executed later in a tf. compat. v1. Session ) and return concrete values (as opposed to symbolic references to a node in a computational graph).

Which of the following is the correct description for eager execution in TensorFlow 27?

In eager execution, TensorFlow operations are executed by the native Python environment with one operation after another.

How do I iterate over a TensorFlow dataset?

To iterate over the dataset several times, use . repeat() . We can enumerate each batch by using either Python's enumerator or a build-in method.


2 Answers

With TF 2.1,

You can create an iterator like so:

iterator = iter(dataset)

And get the next batch of values:

batch = iterator.get_next()
like image 114
domin thomas Avatar answered Sep 21 '22 16:09

domin thomas


make_one_shot_iterator() should work in TensorFlow 1.8, but for now (i.e., for TensorFlow 1.7), do the following:

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)
like image 24
Akshay Agrawal Avatar answered Sep 20 '22 16:09

Akshay Agrawal