我正在尝试使用以下行合并 Keras 2.0 中的两个顺序模型:
merged_model.add(Merge([model1, model2], mode='concat'))
这仍然可以正常工作,但会发出警告:
"The `Merge` layer is deprecated and will be removed after 08/2017. Use
instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc."
但是,研究 Keras 文档并尝试添加 Add() 并没有产生有效的结果。我已经阅读了几篇有同样问题的人的帖子,但没有找到适用于我下面案例的解决方案。有什么建议么?
model = Sequential()
model1 = Sequential()
model1.add(Dense(300, input_dim=40, activation='relu', name='layer_1'))
model2 = Sequential()
model2.add(Dense(300, input_dim=40, activation='relu', name='layer_2'))
merged_model = Sequential()
merged_model.add(Merge([model1, model2], mode='concat'))
merged_model.add(Dense(1, activation='softmax', name='output_layer'))
merged_model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
checkpoint = ModelCheckpoint('weights.h5', monitor='val_acc',
save_best_only=True, verbose=2)
early_stopping = EarlyStopping(monitor="val_loss", patience=5)
merged_model.fit([x1, x2], y=y, batch_size=384, epochs=200,
verbose=1, validation_split=0.1, shuffle=True,
callbacks=[early_stopping, checkpoint])
编辑:当我尝试时(如 Kent Sommer 所建议的):
from keras.layers.merge import concatenate
merged_model.add(concatenate([model1, model2]))
这是错误信息:
Traceback (most recent call last):
File "/anaconda/lib/python3.6/site- packages/keras/engine/topology.py", line 425,
in assert_input_compatibility
K.is_keras_tensor(x)
File "/anaconda/lib/python3.6/site-
packages/keras/backend/tensorflow_backend.py", line 403, in is_keras_tensor
raise ValueError('Unexpectedly found an instance of type `' +
str(type(x)) + '`. '
ValueError: Unexpectedly found an instance of type
`<class'keras.models.Sequential'>`. Expected a symbolic tensor instance.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "quoradeeptest_simple1.py", line 78, in <module>
merged_model.add(concatenate([model1, model2]))
File "/anaconda/lib/python3.6/site-packages/keras/layers/merge.py",
line 600, in concatenate return Concatenate(axis=axis, **kwargs)(inputs)
File "/anaconda/lib/python3.6/site- packages/keras/engine/topology.py",
line 558, in __call__self.assert_input_compatibility(inputs)
File "/anaconda/lib/python3.6/site-packages/keras/engine/topology.py", line 431,
in assert_input_compatibility str(inputs) + '.All inputs to the layer '
ValueError: Layer concatenate_1 was called with an input that isn't a
symbolic tensor. Received type: <class 'keras.models.Sequential'>.
Full input: [<keras.models.Sequential object at 0x140fa7ba8>,
<keras.models.Sequential object at 0x140fabdd8>]. All inputs to the
layer should be tensors.
原文由 twhale 发布,翻译遵循 CC BY-SA 4.0 许可协议
该警告的意思是,不同的模式现在已拆分为它们自己的单独层,而不是使用具有特定模式的合并层。
所以
Merge(mode='concat')
现在是concatenate(axis=-1)
。但是,由于您要合并模型而不是图层,因此这在您的情况下不起作用。您需要做的是使用功能模型,因为基本的顺序模型类型不再支持此行为。
在您的情况下,这意味着代码应更改为以下内容: