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

前端 未结 4 1562
暖寄归人
暖寄归人 2021-02-02 07:14

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 exec         


        
4条回答
  •  无人共我
    2021-02-02 07:55

    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.

提交回复
热议问题