tensorflow2.0版本AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

參考:https://blog.csdn.net/public669/article/details/99686151
運(yùn)行:

from keras import models
from keras import layers

出現(xiàn)錯(cuò)誤:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
tensorflow模塊沒(méi)有g(shù)et_default_graph屬性

錯(cuò)誤原因
這是由于Keras API(https://keras.io/)有多個(gè)實(shí)現(xiàn),包括原始和參考實(shí)現(xiàn)(https://github.com/keras-team/keras),還有各種其他實(shí)現(xiàn),包括tf.keras,它是TensorFlow的一部分。
由于TensorFlow 2默認(rèn)為急切執(zhí)行,因此Keras需要進(jìn)行一些更改才能與之兼容

解決方法
方法一:
將參考實(shí)現(xiàn)與TensorFlow后端一起使用。但是,此實(shí)現(xiàn)尚未更新以支持TensorFlow 2(截至2019年6月)。

方法二:
使用TensorFlow的實(shí)現(xiàn),tf.keras。這個(gè)適用于TF 2。
例如你需要使用tf.keras,必須確保使用正確的導(dǎo)入:
from tensorflow import keras
而不是直接使用:import keras

同樣,在要使用keras下的其他模塊時(shí):
from tensorflow.keras import layers
而不是使用 from keras import layers

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容