The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

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

--

Chapters
00:00 Question
01:13 Accepted answer (Score 1)
01:39 Answer 2 (Score 18)
01:57 Answer 3 (Score 4)
02:15 Thank you

--

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

Answer 1 links:
[Tensorflow]: https://www.tensorflow.org/guide/eager

--

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.