RuntimeError:tf.placeholder()与急切执行不兼容

AMG*_*PLK 9 python python-3.x tensorflow tensorflow2.0

我已经将tf_upgrade_v2 TF1代码升级为TF2。我俩都是菜鸟。我收到下一个错误:

RuntimeError: tf.placeholder() is not compatible with eager execution.
Run Code Online (Sandbox Code Playgroud)

我有一些tf.compat.v1.placeholder()

self.temperature = tf.compat.v1.placeholder_with_default(1., shape=())
self.edges_labels = tf.compat.v1.placeholder(dtype=tf.int64, shape=(None, vertexes, vertexes))
self.nodes_labels = tf.compat.v1.placeholder(dtype=tf.int64, shape=(None, vertexes))
self.embeddings = tf.compat.v1.placeholder(dtype=tf.float32, shape=(None, embedding_dim))
Run Code Online (Sandbox Code Playgroud)

您能给我一些有关如何进行的建议吗?任何“快速”解决方案?还是应该重新编码?

AMG*_*PLK 68

我在这里找到了一个简单的解决方案:禁用 Tensorflow 急切执行

基本上是:

tf.compat.v1.disable_eager_execution()

有了这个,您可以禁用默认的激活急切执行,并且您不需要更多地接触代码。

  • 尝试运行一些“旧”Tensorflow 代码,感觉好像有一半的库已被弃用。 (5认同)
  • 我不认为这样做是一个好主意,因为你会失去轻松交错 python 和张量流代码的能力 - 你在表达能力方面受到严重限制,还不如使用 TF 1.X。如果您想将操作编译到图形组件中,同时仍然启用急切执行,则可以使用“tf.function”修饰函数,它将代码进行 JIT 编译到图形中。 (3认同)

小智 12

tf.placeholder() 旨在提供给会话,该会话在运行时从 feed dict 接收值并执行所需的操作。通常,您会使用 'with' 关键字创建一个 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})
Run Code Online (Sandbox Code Playgroud)

但是当我们使用 Eager Execution 运行时,我们将其运行为:

import tensorflow as tf

tf.enable_eager_execution()

def square(num):
   return tf.square(num)

print(square(10)) 
Run Code Online (Sandbox Code Playgroud)

因此,我们不需要在会话中显式运行它,并且在大多数情况下可以更直观。这提供了更多的交互式执行。更多详情请访问:https : //www.tensorflow.org/guide/eager

如果您要将代码从 tensorflow v1 转换为 tensorflow v2,您必须实现 tf.compat.v1 并且占位符存在于 tf.compat.v1.placeholder 中,但这只能在 Eager 模式关闭时执行。

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

TensorFlow 发布了 Eager Execution 模式,每个节点定义后立即执行。因此使用 tf.placeholder 的语句不再有效。


cs9*_*s95 11

在 TensorFlow 1.X 中,创建了占位符,并在tf.Session实例化a 时为其提供实际值。然而,从 TensorFlow2.0 开始,Eager Execution默认启用,因此“占位符”的概念没有意义,因为操作是立即计算的(而不是与旧范式不同)。

另请参阅函数,而不是 Sessions

# TensorFlow 1.X
outputs = session.run(f(placeholder), feed_dict={placeholder: input})
# TensorFlow 2.0
outputs = f(input)
Run Code Online (Sandbox Code Playgroud)


小智 9

如果您在使用 TensorFlow 模型进行对象检测时遇到此错误,请使用exporter_main_v2.py而不是export_inference_graph.py导出模型。这是正确的方法。如果您只是关闭eager_execution,那么它将解决此错误但会生成其他错误。

另请注意,有一些参数更改,例如您将指定检查点目录的路径而不是检查点的路径。有关如何使用 TensorFlow V2 进行对象检测,请参阅文档


Lah*_*ara 6

要解决此问题,您必须禁用默认的激活急切执行。因此添加以下代码行。

tf.compat.v1.disable_eager_execution() #<--- 禁用急切执行

  • 修复错误之前 在此输入图像描述
  • 错误修复后 在此输入图像描述