升级TensorFlow2.1后,出现“RuntimeError:tf.placeholder()与急切执行不兼容”的错误。

4

我是新手,最近在学习使用TensorFlow和Keras来构建卷积神经网络(CNN)。我试图运行CNN模型来训练mnist数据集,但是在将TensorFlow从2.0版本升级到2.1版本后,出现了以下错误信息:

 raise RuntimeError("tf.placeholder() is not compatible with "

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

我尝试了这段代码

tf.compat.v1.disable_eager_execution()

接下来,

##build input layer

with tf.compat.v1.name_scope('Input_Layer'):
x=tf.compat.v1.placeholder("float",shape=[None, 784],name="x")
x_image = tf.compat.v1.reshape(x, [-1,28,28,1])

下面是CNN模型,所以我能够成功地运行该模型,但我仍然想理解为什么......(在我升级到2.1之前,我可以运行该模型,但现在我需要那段代码...)有人可以帮助我吗?谢谢。


急切执行的目的是摆脱预先构建的图形,因此也就没有了占位符。 - Susmit Agrawal
1个回答

4

在你的代码中使用以下代码行:tf.compat.v1.disable_eager_execution()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接