Python 运行时错误:tf.placeholder() 与急切执行不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56561734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:37:20  来源:igfitidea点击:

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

pythonpython-3.xtensorflowtensorflow2.0

提问by AMGMNPLK

I have upgraded with tf_upgrade_v2 TF1 code to TF2. I'm a noob with both. I got the next error:

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

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

I have some tf.compat.v1.placeholder().

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

Could you give me any advice about how to proceed? Any "fast" solutions? or should I to recode this?

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

回答by AMGMNPLK

I found an easy solution here: disable Tensorflow eager execution

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

Basicaly it is:

基本上是:

tf.compat.v1.disable_eager_execution()

tf.compat.v1.disable_eager_execution()

With this, you disable the default activate eager execution and you don't need to touch the code much more.

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

回答by cs95

In TensorFlow 1.X, placeholders are created and meant to be fed with actual values when a tf.Sessionis instantiated. However, from TensorFlow2.0 onwards, Eager Executionhas been enabled by default, so the notion of a "placeholder" does not make sense as operations are computed immediately (rather than being differed with the old paradigm).

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

Also see Functions, not Sessions,

另请参阅函数,而不是 Sessions

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

回答by Ashish Bastola

tf.placeholder() is meant to be fed to the session that when run recieve the values from feed dict and perform the required operation. Generally you would create a Session() with 'with' keyword and run it.But this might not favour all situations due to which you would require immediate execution.This is called eager execution. Example:

tf.placeholder() 旨在提供给会话,该会话在运行时从 feed dict 接收值并执行所需的操作。通常,您会使用 'with' 关键字创建一个 Session() 并运行它。但这可能不适合所有需要立即执行的情况。这称为急切执行。例子:

generally this is the procedure to run a 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})

But when we run with eager execution we run it as:

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

import tensorflow as tf

tf.enable_eager_execution()

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

print(square(10)) 

Therefore we need not run it inside a session explicitely and can be more intuitive in most of the cases.This provides more of a interactive execution. For further details visit: https://www.tensorflow.org/guide/eager

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

If you are converting the code from tensorflow v1 to tensorflow v2, You must implement tf.compat.v1 and Placeholder is present at tf.compat.v1.placeholder but this can only be executed in eager mode off.

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

tf.compat.v1.disable_eager_execution()

TensorFlow released the eager execution mode, for which each node is immediately executed after definition. Statements using tf.placeholder are thus no longer valid.

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