參考:https://blog.csdn.net/public669/article/details/99686151
運行:
from keras import models
from keras import layers
出現(xiàn)錯誤:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
tensorflow模塊沒有g(shù)et_default_graph屬性
錯誤原因
這是由于Keras API(https://keras.io/)有多個實現(xiàn),包括原始和參考實現(xiàn)(https://github.com/keras-team/keras)恰起,還有各種其他實現(xiàn)寸潦,包括tf.keras,它是TensorFlow的一部分刻蟹。
由于TensorFlow 2默認為急切執(zhí)行,因此Keras需要進行一些更改才能與之兼容
解決方法
方法一:
將參考實現(xiàn)與TensorFlow后端一起使用。但是邀窃,此實現(xiàn)尚未更新以支持TensorFlow 2(截至2019年6月)犬第。
方法二:
使用TensorFlow的實現(xiàn)锦积,tf.keras。這個適用于TF 2歉嗓。
例如你需要使用tf.keras丰介,必須確保使用正確的導入:
from tensorflow import keras
而不是直接使用:import keras
同樣,在要使用keras下的其他模塊時:
from tensorflow.keras import layers
而不是使用 from keras import layers