How do I disable TensorFlow's eager execution?

后端 未结 4 1899
無奈伤痛
無奈伤痛 2020-12-02 16:47

I am trying to learn TensorFlow. Currently, I am working with placeholders. When I tried to create the placeholder, I got an error: RuntimeError: tf.placeholder() is n

4条回答
  •  鱼传尺愫
    2020-12-02 17:03

    I assume the you are using TensorFlow 2.0. In TF2, eager mode is turned on by default. However, there is a disable_eager_execution() in TensorFlow 2.0.0-alpha0 but it is hidden quite deep and cannot be directly accessed from top-level module namespace (i.e tf namespace).

    You can call the function like so:

    import tensorflow as tf
    from tensorflow.python.framework.ops import disable_eager_execution
    
    disable_eager_execution()
    
    a = tf.constant(1)
    b = tf.constant(2)
    c = a + b
    print(c)
    

    >>>Tensor("add:0", shape=(), dtype=int32)

    print(disable_eager_execution.__doc__) 
    

    >>>Disables eager execution. This function can only be called before any Graphs, Ops, or Tensors have been created. It can be used at the beginning of the program for complex migration projects from TensorFlow 1.x to 2.x.

提交回复
热议问题