如何合并具有相同输入的 keras 序列模型? [英] How to merge keras sequential models with same input?

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

问题描述

我正在尝试在 keras 中创建我的第一个集成模型.我的数据集中有 3 个输入值和一个输出值.

I am trying to create my first ensemble models in keras. I have 3 input values and a single output value in my dataset.

from keras.optimizers import SGD,Adam
from keras.layers import Dense,Merge
from keras.models import Sequential

model1 = Sequential()
model1.add(Dense(3, input_dim=3, activation='relu'))
model1.add(Dense(2, activation='relu'))
model1.add(Dense(2, activation='tanh'))
model1.compile(loss='mse', optimizer='Adam', metrics=['accuracy'])

model2 = Sequential()
model2.add(Dense(3, input_dim=3, activation='linear'))
model2.add(Dense(4, activation='tanh'))
model2.add(Dense(3, activation='tanh'))
model2.compile(loss='mse', optimizer='SGD', metrics=['accuracy'])

model3 = Sequential()
model3.add(Merge([model1, model2], mode = 'concat'))
model3.add(Dense(1, activation='sigmoid'))
model3.compile(loss='binary_crossentropy', optimizer='Adam', metrics=['accuracy'])

model3.input_shape

集成模型(model3)编译没有任何错误,但在拟合模型时,我必须两次传递相同的输入model3.fit([X,X],y).我认为这是一个不必要的步骤,我希望我的集成模型有一个通用的输入节点,而不是两次传递输入.我该怎么做?

The ensemble model(model3) compiles without any error but while fitting the model I have to pass the same input two times model3.fit([X,X],y). Which I think is an unnecessary step and instead of passing input twice I want to have a common input nodes for my ensemble model. How can I do it?

推荐答案

Keras 函数式 API 似乎更适合您的用例,因为它在计算图中提供了更大的灵活性.例如:

Keras functional API seems to be a better fit for your use case, as it allows more flexibility in the computation graph. e.g.:

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

# a single input layer
inputs = Input(shape=(3,))

# model 1
x1 = Dense(3, activation='relu')(inputs)
x1 = Dense(2, activation='relu')(x1)
x1 = Dense(2, activation='tanh')(x1)

# model 2 
x2 = Dense(3, activation='linear')(inputs)
x2 = Dense(4, activation='tanh')(x2)
x2 = Dense(3, activation='tanh')(x2)

# merging models
x3 = concatenate([x1, x2])

# output layer
predictions = Dense(1, activation='sigmoid')(x3)

# generate a model from the layers above
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# Always a good idea to verify it looks as you expect it to 
# model.summary()

data = [[1,2,3], [1,1,3], [7,8,9], [5,8,10]]
labels = [0,0,1,1]

# The resulting model can be fit with a single input:
model.fit(data, labels, epochs=50)

注意事项:

  • Keras 版本之间的 API 可能略有不同(版本 2 之前和之后)
  • 上面的示例为每个模型指定了不同的优化器和损失函数.但是,由于 fit() 仅被调用一次(在模型 3 上),因此相同的设置 - 模型 3 的设置 - 将应用于整个模型.为了在训练子模型时有不同的设置,它们必须分别适合() -请参阅@Daniel 的评论.

根据评论更新笔记

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

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