merge different models with different inputs Keras

天涯浪子 提交于 2019-12-08 13:15:04

问题


I would like to train two different Conv models in Keras with different input dimensions.

I have:

 input_size=4
 input_sizeB=6

 model=Sequential()
 model.add(Conv2D(filters=10,input_shape= 
 (1,time_steps,input_size),kernel_size(24,3),activation='relu',data_format='channels_first',kernel_regularizer=regularizers.l2(0.001)))
 model.add(Flatten())
A= model.add(Dense(25, 
activation='tanh',kernel_regularizer=regularizers.l2(0.003)))

 model2=Sequential()
 model2.add(Conv2D(filters=10,input_shape= 
 (1,time_steps,input_sizeB),kernel_size(24,3),activation='relu',data_format='channels_first',kernel_regularizer=regularizers.l2(0.001)))
  model2.add(Flatten())
B= model2.add(Dense(25, 
activation='tanh',kernel_regularizer=regularizers.l2(0.003)))

Now I would merge the two dense layers at the end of both Conv net.

How I should do?


回答1:


Using the Sequential API, you can use the Merge layer (doc) as follows:

merged_layer = Merge([model, model2], mode='concat') # mode='sum', 'ave', etc.
merged_model = Sequential()
merged_model.add(merged_layer)

Note that this will throw a warning (depending on your version, the code should still work), as sequential Merge is getting deprecated. You could otherwise consider the Functional API, which offers some more flexibility in that regards c.f. the several pre-defined merge layers Keras provides depending on the operation you want to use (doc). Find an example below:

merged_layer = Concatenate()([model.output, model2.output])
merged_model = Model([model.input, model2.input], merged_layer)


来源:https://stackoverflow.com/questions/50214679/merge-different-models-with-different-inputs-keras

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