Keras - Merging layers - Keras 2.0

偶尔善良 提交于 2019-12-12 15:38:21

问题


I am trying to merge two networks. I can accomplish this by doing the following:

merged = Merge([CNN_Model, RNN_Model], mode='concat')

But I get a warning:

merged = Merge([CNN_Model, RNN_Model], mode='concat')
__main__:1: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.

So I tried this:

merged = Concatenate([CNN_Model, RNN_Model])
model = Sequential()
model.add(merged)

and got this error:

ValueError: The first layer in a Sequential model must get an `input_shape` or `batch_input_shape` argument.

Can anyone give me the syntax as how I would get this to work?


回答1:


Don't use sequential models for models with branches.

Use the Functional API:

from keras.models import Model  

You're right in using the Concatenate layer, but you must pass "tensors" to it. And first you create it, then you call it with input tensors (that's why there are two parentheses):

concatOut = Concatenate()([CNN_Model.output,RNN_Model.output])

For creating a model out of that, you need to define the path from inputs to outputs:

model = Model([CNN_Model.input, RNN_Model.input], concatOut)

This answer assumes your existing models have only one input and output each.



来源:https://stackoverflow.com/questions/44710080/keras-merging-layers-keras-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!