一般遇到这种问题都是应为TensorFlow版本的原因
TensorFlow2.x与1.x版本导入用法格式稍微有不同
现在版本都是一般都是默认TensorFlow2.x
解决方法
1.将x = tf.placeholder()替换为x = tf.compat.v1.placeholder()
x = tf.compat.v1.placeholder(tf.float32, [None, size, size, 3]) y_ = tf.compat.v1.placeholder(tf.float32, [None, 2])
2.import tensorflow as tf换成import tensorflow.compat.v1 as tf
结果按方法1,2都会出现以下问题
raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.
查看后发现,如果启动了紧急执行,会出错。
最终查到了一个解决方案
在import tensorflow as tf 后加上
tf.compat.v1.disable_eager_execution()
关闭紧急执行。
第二种方法举例 可以正常运行
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()