How do I disable TensorFlow's eager execution?

kokeuurv  于 2022-11-16  发布在  其他
关注(0)|答案(4)|浏览(149)

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() ?

wkyowqbh

wkyowqbh1#

假设您使用的是Tensorflow 2.0预览版,默认情况下启用了即时执行。v1 API中有一个disable_eager_execution(),您可以将其放在代码前面,如下所示:

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

另一方面,如果您没有使用2.0预览,请检查您是否在某处意外启用了急切执行。

pu82cl6c

pu82cl6c2#

我假设您使用的是TensorFlow 2.0。在TF 2中,eager模式默认打开。但是,TensorFlow 2.0.0中有一个disable_eager_execution()-alpha 0,但它隐藏得很深,无法从顶层模块名称空间(即tf名称空间)直接访问。
您可以像这样呼叫函式:

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.

wlwcrazw

wlwcrazw3#

在TensorFlow 2.3+中,您可以使用以下方法随时禁用急切模式:

import tensorflow as tf

tf.config.run_functions_eagerly(False)
7kqas0il

7kqas0il4#

您可以禁用TensorFlow v2行为,如下所示:

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

相关问题