合并具有不同输入的不同模型Keras [英] merge different models with different inputs Keras

查看:275
本文介绍了合并具有不同输入的不同模型Keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Keras中使用不同的输入维度训练两个不同的Conv模型.

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

我有:

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

现在,我将在两个Conv网络的末尾合并两个密集层.

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

我应该怎么做?

推荐答案

使用Sequential API,您可以使用Merge层(

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)

请注意,这将引发警告(取决于您的版本,代码仍应工作),因为不推荐使用连续的Merge.否则,您可以考虑使用Functional API,在c.f方面,它提供了更多的灵活性. Keras根据您要使用的操作提供了几个预定义的 merge 层( doc ).在下面找到一个示例:

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)

这篇关于合并具有不同输入的不同模型Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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