如何禁用TensorFlow的热切执行?

Llo*_*ner 19 tensorflow

我正在尝试学习TensorFlow。目前,我正在与占位符一起工作。当我尝试创建占位符时,出现错误:RuntimeError: tf.placeholder() is not compatible with eager execution,因为占位符不能立即执行,所以这很有意义。

那么,如何关闭渴望执行的程序?

首先,我从未渴望执行过,所以我不确定它是如何发生的。与之相反tf.disable_eager_execution()吗?

小智 30

假设您正在使用Tensorflow 2.0预览版本,该版本默认情况下已启用急切执行。disable_eager_execution()v1中有一个API,您可以将其放在代码的前面,例如:

import tensorflow as tf

tf.compat.v1.disable_eager_execution()
Run Code Online (Sandbox Code Playgroud)

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


Syn*_*sis 16

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

import tensorflow as tf

tf.config.run_functions_eagerly(False)
Run Code Online (Sandbox Code Playgroud)

  • 这在2.6.0版本中似乎没有任何影响 (7认同)
  • 不,它根本不影响! (3认同)

Jud*_*AYE 11

您可以像这样禁用 TensorFlow v2 行为:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Run Code Online (Sandbox Code Playgroud)


mib*_*ibu 7

我假设您正在使用TensorFlow 2.0。在TF2中,默认情况下开启了紧急模式。但是,disable_eager_execution()在TensorFlow 2.0.0-alpha0中有一个,但它隐藏得很深,无法从顶级模块名称空间(即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)
Run Code Online (Sandbox Code Playgroud)

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

print(disable_eager_execution.__doc__) 
Run Code Online (Sandbox Code Playgroud)

>>>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.