Search code examples
pythontensorflow

`tf.data.Dataset` only supports Python-style iteration in eager mode or within tf.function


RuntimeError: `tf.data.Dataset` only supports Python-style iteration in eager mode or within tf.function.

please help with this error as I am not sure what to do

here is the offending line:

for input_example_batch, target_example_batch in dataset.take(1):

Solution

  • As per the doc, dataset.take(n) returns another dataset, not a single batch. To actually get a batch out of that single-batch dataset, you can do

    for input_example_batch, target_example_batch in next(iter(dataset.take(1))):