RuntimeError: tf.placeholder() is not compatible with eager execution

PythonPython 3.xTensorflowtensorflow2.0

Python Problem Overview


I have upgraded with tf_upgrade_v2 TF1 code to TF2. I'm a noob with both. I got the next error:

RuntimeError: tf.placeholder() is not compatible with eager execution.

I have some tf.compat.v1.placeholder().

self.temperature = tf.compat.v1.placeholder_with_default(1., shape=())
self.edges_labels = tf.compat.v1.placeholder(dtype=tf.int64, shape=(None, vertexes, vertexes))
self.nodes_labels = tf.compat.v1.placeholder(dtype=tf.int64, shape=(None, vertexes))
self.embeddings = tf.compat.v1.placeholder(dtype=tf.float32, shape=(None, embedding_dim))

Could you give me any advice about how to proceed? Any "fast" solutions? or should I to recode this?

Python Solutions


Solution 1 - Python

I found an easy solution here: https://stackoverflow.com/questions/53429896/disable-tensorflow-eager-execution

Basicaly it is:

tf.compat.v1.disable_eager_execution()

With this, you disable the default activate eager execution and you don't need to touch the code much more.

Solution 2 - Python

tf.placeholder() is meant to be fed to the session that when run receive the values from feed dict and perform the required operation. Generally, you would create a Session() with 'with' keyword and run it. But this might not favour all situations due to which you would require immediate execution. This is called eager execution. Example:

generally, this is the procedure to run a Session:

import tensorflow as tf

def square(num):
    return tf.square(num) 

p = tf.placeholder(tf.float32)
q = square(num)

with tf.Session() as sess:
    print(sess.run(q, feed_dict={num: 10})

But when we run with eager execution we run it as:

import tensorflow as tf

tf.enable_eager_execution()

def square(num):
   return tf.square(num)

print(square(10)) 

Therefore we need not run it inside a session explicitly and can be more intuitive in most of the cases. This provides more of an interactive execution. For further details visit: https://www.tensorflow.org/guide/eager

If you are converting the code from tensorflow v1 to tensorflow v2, You must implement tf.compat.v1 and Placeholder is present at tf.compat.v1.placeholder but this can only be executed in eager mode off.

tf.compat.v1.disable_eager_execution()

TensorFlow released the eager execution mode, for which each node is immediately executed after definition. Statements using tf.placeholder are thus no longer valid.

Solution 3 - Python

In TensorFlow 1.X, placeholders are created and meant to be fed with actual values when a tf.Session is instantiated. However, from TensorFlow2.0 onwards, Eager Execution has been enabled by default, so the notion of a "placeholder" does not make sense as operations are computed immediately (rather than being differed with the old paradigm).

Also see Functions, not Sessions,

# TensorFlow 1.X
outputs = session.run(f(placeholder), feed_dict={placeholder: input})
# TensorFlow 2.0
outputs = f(input)

Solution 4 - Python

If you are getting this error while doing object detection using TensorFlow model then use exporter_main_v2.py instead of export_inference_graph.py for exporting the model. This is right method to do. If you just off eager_execution then it will solve this error but generate other.

Also note that there are some parameter change like hear you will specify the path to directory of checkpoint instead of path to checkpoint. refer to this document for how to do object detection with TensorFlow V2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAMGMNPLKView Question on Stackoverflow
Solution 1 - PythonAMGMNPLKView Answer on Stackoverflow
Solution 2 - PythonAshish BastolaView Answer on Stackoverflow
Solution 3 - Pythoncs95View Answer on Stackoverflow
Solution 4 - PythonVishvajeet RamanujView Answer on Stackoverflow