gpt4 book ai didi

python - 属性错误 : 'Tensor' object has no attribute '_keras_shape' when creating a model in keras

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:43 25 4
gpt4 key购买 nike

当我在 keras 中定义模型时,错误如下所示AttributeError: 'Tensor' 对象没有属性 '_keras_shape'

产生错误的代码是

vocab_size = 10000
MAX_SEQUENCE_LENGTH = 256
sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedding=keras.layers.Embedding(vocab_size, 16, input_length = MAX_SEQUENCE_LENGTH)(sequence_input)
x=keras.layers.GlobalAveragePooling1D()(embedding)
x=keras.layers.Dense(16, activation=tf.nn.relu)(x)

preds = keras.layers.Dense(1, activation=tf.nn.sigmoid)(x)

model = Model(inputs=sequence_input, outputs=preds)
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='binary_crossentropy',
metrics=['accuracy'])

完整的错误信息如下所示

AttributeError                            Traceback (most recent call last)
<ipython-input-5-1c6ea41c38e1> in <module>()
1 from keras.models import Model
----> 2 model = Model(inputs=sequence_input, outputs=preds)
3 model.compile(optimizer=tf.train.AdamOptimizer(),
4 loss='binary_crossentropy',
5 metrics=['accuracy'])

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

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
89 'inputs' in kwargs and 'outputs' in kwargs):
90 # Graph network
---> 91 self._init_graph_network(*args, **kwargs)
92 else:
93 # Subclassed network

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
249 input_masks=[None for _ in self.inputs],
250 output_masks=[None for _ in self.outputs],
--> 251 input_shapes=[x._keras_shape for x in self.inputs],
252 output_shapes=[x._keras_shape for x in self.outputs])
253

~/Datacube/datacube_env/lib/python3.5/site-packages/keras/engine/network.py in <listcomp>(.0)
249 input_masks=[None for _ in self.inputs],
250 output_masks=[None for _ in self.outputs],
--> 251 input_shapes=[x._keras_shape for x in self.inputs],
252 output_shapes=[x._keras_shape for x in self.outputs])
253

AttributeError: 'Tensor' object has no attribute '_keras_shape'

谁能告诉我怎么解决

最佳答案

我注意到,如果您混淆了常规的 keras 导入和 tensorflow 导入,就会发生这种情况。避免像这样混合导入:

# Mixed imports, one is regular keras, other is TF's keras
import keras
from tensorflow.keras.model import Model

sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
...
model = Model(inputs=sequence_input, outputs=preds)

例如,从 TF 导入所有内容:

from tensorflow import keras
from tensorflow.keras.model import Model

sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
...
model = Model(inputs=sequence_input, outputs=preds)

或者只使用 Keras:

import keras
from keras.model import Model

sequence_input = keras.layers.Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
...
model = Model(inputs=sequence_input, outputs=preds)

不能确定这是你问题的根本原因,但这可能对某些人有帮助

关于python - 属性错误 : 'Tensor' object has no attribute '_keras_shape' when creating a model in keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51515929/

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