gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:45:38 24 4
gpt4 key购买 nike

我正在尝试构建一个深度学习模型,但我在使用 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'

请帮我解决这个问题。

最佳答案

您在导入中混合了 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

关于tensorflow - 模块 'tensorflow' 没有属性 'get_default_graph',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57390631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com