RuntimeError: tf.placeholder() is not compatible with eager execution.解决方法
程序员文章站
2022-06-15 13:53:29
...
在写吴恩达老师的第四课第一周作业时,由于我的tensorflo是2.3版本,自己改了之后出现了以下错误提示
RuntimeError: tf.placeholder() is not compatible with eager execution.
错误代码:
def create_placeholders(n_H0, n_W0, n_C0, n_y):
"""
为session创建占位符
参数:
n_H0 - 实数,输入图像的高度
n_W0 - 实数,输入图像的宽度
n_C0 - 实数,输入的通道数
n_y - 实数,分类数
输出:
X - 输入数据的占位符,维度为[None, n_H0, n_W0, n_C0],类型为"float"
Y - 输入数据的标签的占位符,维度为[None, n_y],维度为"float"
"""
X = tf.compat.v1.placeholder(tf.float32, [None, n_H0, n_W0, n_C0])
Y = tf.compat.v1.placeholder(tf.float32, [None, n_y])
return X, Y
X , Y = create_placeholders(64,64,3,6)
print ("X = " + str(X))
print ("Y = " + str(Y))
加上了:
tf.compat.v1.disable_eager_execution()
正确代码:
def create_placeholders(n_H0, n_W0, n_C0, n_y):
"""
为session创建占位符
参数:
n_H0 - 实数,输入图像的高度
n_W0 - 实数,输入图像的宽度
n_C0 - 实数,输入的通道数
n_y - 实数,分类数
输出:
X - 输入数据的占位符,维度为[None, n_H0, n_W0, n_C0],类型为"float"
Y - 输入数据的标签的占位符,维度为[None, n_y],维度为"float"
"""
tf.compat.v1.disable_eager_execution()
X = tf.compat.v1.placeholder(tf.float32, [None, n_H0, n_W0, n_C0])
Y = tf.compat.v1.placeholder(tf.float32, [None, n_y])
return X, Y
X , Y = create_placeholders(64,64,3,6)
print ("X = " + str(X))
print ("Y = " + str(Y))
就能正常输出了!
结果:
X = Tensor("Placeholder:0", shape=(None, 64, 64, 3), dtype=float32)
Y = Tensor("Placeholder_1:0", shape=(None, 6), dtype=float32)
推荐阅读
-
RuntimeError: tf.placeholder() is not compatible with eager execution.
-
RuntimeError: tf.placeholder() is not compatible with eager execution.和没有placeholder函数解决方法
-
RuntimeError: tf.placeholder() is not compatible with eager execution.解决方法
-
RuntimeError: tf.placeholder() is not compatible with eager execution.(亲测有效)