參考: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