The Python Oracle

TensorFlow 2.0 dataset.__iter__() is only supported when eager execution is enabled

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Realization

--

Chapters
00:00 Tensorflow 2.0 Dataset.__iter__() Is Only Supported When Eager Execution Is Enabled
00:55 Accepted Answer Score 1
01:17 Answer 2 Score 19
01:30 Answer 3 Score 4
01:40 Thank you

--

Full question
https://stackoverflow.com/questions/5557...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #tensorflow #tensorflowdatasets #tensorflow20

#avk47



ANSWER 1

Score 19


I fixed it by enabling eager execution after importing tensorflow:

import tensorflow as tf

tf.enable_eager_execution()

Reference: Tensorflow




ANSWER 2

Score 4


In case you are using Jupyter notebook after

import tensorflow as tf

tf.enable_eager_execution()

You need to restart the kernel and it works




ACCEPTED ANSWER

Score 1


I fixed this by changing the train function to the following:

def train(model, dataset, optimizer):
    for step, (x1, x2, y) in enumerate(dataset):
        with tf.GradientTape() as tape:
            left, right = model([x1, x2])
            loss = contrastive_loss(left, right, tf.cast(y, tf.float32))
        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

The two changes are removing the @tf.function and fixing the enumeration.