我已经将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))
你能给我一些关于如何进行的建议吗?有没有什么“快速”的解决方案?或者我应该重新编码这个?
发布于 2019-06-13 03:31:55
我在这里找到了一个简单的解决方案:disable Tensorflow eager execution
基本上是这样的:
tf.compat.v1.disable_eager_execution()
这样,您就禁用了默认的激活紧急执行,并且不需要更多地接触代码。
发布于 2019-06-12 20:21:08
tf.placeholder()被提供给会话,在运行时接收来自提要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,并且占位符出现在tf.compat.v1.placeholder中,但这只能在关闭急切模式下执行。
tf.compat.v1.disable_eager_execution()
TensorFlow发布了急切执行模式,每个节点在定义后都会立即执行。因此,使用tf.placeholder的语句不再有效。
发布于 2019-06-13 00:21:34
在TensorFlow 1.X中,将创建占位符,并在实例化tf.Session
时向其提供实际值。然而,从TensorFlow2.0开始,默认情况下已经启用了Eager Execution,因此“占位符”的概念没有意义,因为操作是立即计算的(而不是与旧的范例不同)。
# TensorFlow 1.X
outputs = session.run(f(placeholder), feed_dict={placeholder: input})
# TensorFlow 2.0
outputs = f(input)
https://stackoverflow.com/questions/56561734
复制相似问题