模块'tensorflow'没有属性'get_default_graph'

时间:2019-08-07 09:00:12

标签: tensorflow keras tf.keras

我正在尝试构建深度学习模型,但是使用tensorflow时出现错误,并且无法解决此问题。

我正在尝试构建深度学习模型,但是在定义模型时遇到此错误。

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'))

错误:-

---------------------------------------------------------------------------
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'

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:1)

您在导入中混用了kerastf.keras软件包:

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

这将无效,这两个程序包彼此不兼容。在这种情况下,您只能使用tf.keras

from tensorflow.keras.layers import Embedding