3

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?

Kevin
  • 3,077
  • 6
  • 31
  • 77

1 Answers1

2

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.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • This is great, What if I wanted to add some more layers to this model? I tried this: `model.add(Dense(22, activation='softmax', name='final_dense'))` and it gave me `AttributeError: 'Model' object has no attribute 'add'` – Kevin Jun 23 '17 at 22:15
  • Create the layer and give it the input to get the output: `denseOut = Dense(22,activation='softmax',name='final_dense')(concatOut)` --- Your model will then be like `model = Model([CNN_Model.input, RNN_Model.input], denseOut)` – Daniel Möller Jun 24 '17 at 01:40