Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable TensorFlow's eager execution?

Tags:

tensorflow

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 not compatible with eager execution, which makes sense as placeholders are not executable immediately.

So, how do I turn eager execution off?

I have never turned eager execution on in the first place, so I am not sure how it happened. Is there an opposite to tf.disable_eager_execution()?

like image 864
Lloyd Rayner Avatar asked Nov 22 '18 11:11

Lloyd Rayner


People also ask

Is eager execution enabled by default?

Eager execution is enabled by default.

What is eager execution mode?

Eager execution is a powerful execution environment that evaluates operations immediately. It does not build graphs, and the operations return actual values instead of computational graphs to run later.

How do I turn on eager execution?

In tensorflow 2.0 the eager execution is enabled by default. You don't need to enable it in your program. Now you can directly view the value of tensor without using session object. numpy_array = t.


2 Answers

Assume you are using Tensorflow 2.0 preview release which has eager execution enabled by default. There is a disable_eager_execution() in v1 API, which you can put in the front of your code like:

import tensorflow as tf  tf.compat.v1.disable_eager_execution() 

On the other hand, if you are not using 2.0 preview, please check if you accidentally enabled eager execution somewhere.

like image 141
user2117745 Avatar answered Oct 07 '22 21:10

user2117745


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.

like image 41
mibu Avatar answered Oct 07 '22 21:10

mibu