一般遇到這種問題都是應爲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()