如何“合并" Keras 2.0中的顺序模型? [英] How to "Merge" Sequential models in Keras 2.0?

查看:82
本文介绍了如何“合并" Keras 2.0中的顺序模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下行在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.

解决方案

该警告的意思是,现在已经将不同的模式拆分为各自的单独层,而不是将Merge层用于特定模式. >

所以Merge(mode='concat')现在是concatenate(axis=-1).

但是,由于您要合并模型而不是图层,因此这不适用于您的情况.您需要做的是使用功能模型,因为基本的顺序模型类型不再支持此行为.

在您的情况下,这意味着应将代码更改为以下内容:

from keras.layers.merge import concatenate
from keras.models import Model, Sequential
from keras.layers import Dense, Input

model1_in = Input(shape=(27, 27, 1))
model1_out = Dense(300, input_dim=40, activation='relu', name='layer_1')(model1_in)
model1 = Model(model1_in, model1_out)

model2_in = Input(shape=(27, 27, 1))
model2_out = Dense(300, input_dim=40, activation='relu', name='layer_2')(model2_in)
model2 = Model(model2_in, model2_out)


concatenated = concatenate([model1_out, model2_out])
out = Dense(1, activation='softmax', name='output_layer')(concatenated)

merged_model = Model([model1_in, model2_in], out)
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])

I am trying to merge two Sequential models In Keras 2.0, using the following line:

merged_model.add(Merge([model1, model2], mode='concat'))

This still works fine, but gives a warning:

"The `Merge` layer is deprecated and will be removed after 08/2017. Use
instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc." 

However, studying the Keras documentation and trying add, Add(), has not resulted in something that works. I have read several posts from people with the same problem, but found no solution that works in my case below. Any suggestions?

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

EDIT: When I tried (as suggested below by Kent Sommer):

from keras.layers.merge import concatenate
merged_model.add(concatenate([model1, model2]))

This was the error message:

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.

解决方案

What that warning is saying is that instead of using the Merge layer with a specific mode, the different modes have now been split into their own individual layers.

So Merge(mode='concat') is now concatenate(axis=-1).

However, since you want to merge models not layers, this will not work in your case. What you will need to do is use the functional model since this behavior is no longer supported with the basic Sequential model type.

In your case that means the code should be changed to the following:

from keras.layers.merge import concatenate
from keras.models import Model, Sequential
from keras.layers import Dense, Input

model1_in = Input(shape=(27, 27, 1))
model1_out = Dense(300, input_dim=40, activation='relu', name='layer_1')(model1_in)
model1 = Model(model1_in, model1_out)

model2_in = Input(shape=(27, 27, 1))
model2_out = Dense(300, input_dim=40, activation='relu', name='layer_2')(model2_in)
model2 = Model(model2_in, model2_out)


concatenated = concatenate([model1_out, model2_out])
out = Dense(1, activation='softmax', name='output_layer')(concatenated)

merged_model = Model([model1_in, model2_in], out)
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])

这篇关于如何“合并" Keras 2.0中的顺序模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆