【问题标题】:RuntimeError: tf.placeholder() is not compatible with eager executionRuntimeError: tf.placeholder() 与急切执行不兼容
【发布时间】:2021-08-20 01:50:27
【问题描述】:

我已使用 tf_upgrade_v2 TF1 代码升级到 TF2。我是两者的菜鸟。我得到了下一个错误:

RuntimeError: tf.placeholder() is not compatible with eager execution.

我有一些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))

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

【问题讨论】:

    标签: python python-3.x tensorflow tensorflow2.0


    【解决方案1】:

    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})
    

    但是当我们以急切执行的方式运行时,我们将其运行为:

    import tensorflow as tf
    
    tf.enable_eager_execution()
    
    def square(num):
       return tf.square(num)
    
    print(square(10)) 
    

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

    如果您要将代码从 tensorflow v1 转换为 tensorflow v2,则必须实现 tf.compat.v1 并且 Placeholder 存在于 tf.compat.v1.placeholder 中,但这只能在急切模式下执行。

    tf.compat.v1.disable_eager_execution()
    

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

    【讨论】:

    • 谢谢阿什。是的,我理解 eager 和 session 下的逻辑,但我说的是使用 ft_upgrade_v2 工具将 TF1 代码升级到 TF2。它不处理占位符吗?
    • 感谢您澄清您的问题!似乎占位符已从 tensorflow2.0 中删除,您需要创建 None 类型的张量。另外,如果您使用 tf.compat.v1.*,那么您需要由于不兼容而关闭 Eager Execution!
    • tf.compat.v1.disable_eager_execution(),谢谢~
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我在这里找到了一个简单的解决方案:disable Tensorflow eager execution

      基本上是这样的:

      tf.compat.v1.disable_eager_execution()

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

      【讨论】:

      • 我不认为这样做是一个好主意,因为你失去了轻松交错 python 和 tensorflow 代码的能力——你在表现力方面受到严重限制,不妨使用 TF 1。 X。如果你想在仍然启用 Eager Execution 的同时将操作编译成图形组件,你可以使用 tf.function 装饰函数,它将代码 JIT 编译到图形中。
      • 我们在哪里使用这个方法?而不是每个 tf.compat.v1.placeholder() ?
      • 开始使用TF或脚本。
      • 尝试运行一些“旧”的 Tensorflow 代码,感觉有一半的库被弃用了。
      • @AMGMNPLK tf.compat.v1.disable_eager_execution(),谢谢~
      【解决方案4】:

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

      另见Functions, not Sessions

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

      【讨论】:

        猜你喜欢
        • 2019-02-27
        • 2020-11-20
        • 2018-09-22
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多