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()
有了这个,您可以禁用默认的激活急切执行,并且您不需要更多地接触代码。
小智 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)
要解决此问题,您必须禁用默认的激活急切执行。因此添加以下代码行。
tf.compat.v1.disable_eager_execution() #<--- 禁用急切执行
| 归档时间: |
|
| 查看次数: |
6274 次 |
| 最近记录: |