python – How to fix AttributeError: module tensorflow has no attribute get_default_graph?

python – How to fix AttributeError: module tensorflow has no attribute get_default_graph?

Please try:

from tensorflow.keras.models import Sequential

instead of

from keras.models import Sequential

For tf 2.1.0 I used tf.compat.v1.get_default_graph() – e.g:

import tensorflow as tf
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
tf.compat.v1.keras.backend.set_session(sess)

python – How to fix AttributeError: module tensorflow has no attribute get_default_graph?

for latest tensorflow 2 replace the above code with below code with some changes

for details check keras documentation:
https://www.tensorflow.org/guide/keras/overview

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, load_model

model = tf.keras.Sequential()
model.add(layers.Dense(32, input_dim=784))
model.add(layers.Activation(relu))
model.add(layers.LSTM(17))
model.add(layers.Dense(1, activation=sigmoid))
model.compile(loss=binary_crossentropy, optimizer=tf.keras.optimizers.Adam(0.01), metrics=[accuracy])

Leave a Reply

Your email address will not be published.