Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module 'tensorflow' has no attribute 'get_default_graph'

I am trying to build a deep learning model but I am getting an error using tensorflow and I am failing to fix this issue.

I am trying to build a deep learning model but I m getting this error while defining the model.

from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential, load_model, Model
from tensorflow.keras.utils import plot_model
from tensorflow. keras.layers import Flatten, Dropout, Activation, Input, Dense, concatenate
from keras.layers.embeddings import Embedding
from tensorflow.keras.initializers import Constant
from tensorflow.python.keras import backend as k

# define the model
model = Sequential()
model.add(Embedding(vocabulary_size, embedding_size, input_length=MAXLEN))
model.add(Flatten())
model.add(Dense(op_units, activation='softmax'))

Error:-

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-64-4570aa61c2f1> in <module>
      1 # define the model
      2 model = Sequential()
----> 3 model.add(Embedding(vocabulary_size, embedding_size, input_length=MAXLEN))
      4 model.add(Flatten())
      5 model.add(Dense(op_units, activation='softmax'))

~\Anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~\Anaconda3\lib\site-packages\keras\layers\embeddings.py in __init__(self, input_dim, output_dim, embeddings_initializer, embeddings_regularizer, activity_regularizer, embeddings_constraint, mask_zero, input_length, **kwargs)
     88             else:
     89                 kwargs['input_shape'] = (None,)
---> 90         super(Embedding, self).__init__(**kwargs)
     91 
     92         self.input_dim = input_dim

~\Anaconda3\lib\site-packages\keras\engine\base_layer.py in __init__(self, **kwargs)
    130         if not name:
    131             prefix = self.__class__.__name__
--> 132             name = _to_snake_case(prefix) + '_' + str(K.get_uid(prefix))
    133         self.name = name
    134 

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in get_uid(prefix)
     72     """
     73     global _GRAPH_UID_DICTS
---> 74     graph = tf.get_default_graph()
     75     if graph not in _GRAPH_UID_DICTS:
     76         _GRAPH_UID_DICTS[graph] = defaultdict(int)

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

Please help me with this issue.

like image 430
Moinak Dey Avatar asked Dec 27 '25 14:12

Moinak Dey


1 Answers

You are mixing the keras and tf.keras packages in your imports:

from tensorflow.keras.layers import Flatten, Dropout, Activation, Input, Dense, concatenate
from keras.layers.embeddings import Embedding

This will NOT WORK, because these packages are not compatible with each other. You should only use tf.keras in this case:

from tensorflow.keras.layers import Embedding
like image 73
Dr. Snoopy Avatar answered Dec 31 '25 04:12

Dr. Snoopy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!