之所以会出现错误result
,Sequential()
是因为定义为只是模型的容器,而尚未定义输入。
给定您要构建的集合,result
以接受第三个输入x3
。
first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))
second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))
third = Sequential()
# of course you must provide the input to result with will be your x3
third.add(Dense(1, input_shape=(1,), activation='sigmoid'))
# lets say you add a few more layers to first and second.
# concatenate them
merged = Concatenate([first, second])
# then concatenate the two outputs
result = Concatenate([merged, third])
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
result.compile(optimizer=ada_grad, loss='binary_crossentropy',
metrics=['accuracy'])
但是,构建具有这种输入结构类型的模型的首选方法是使用功能性api。
这是您的入门要求的实现:
from keras.models import Model
from keras.layers import Concatenate, Dense, LSTM, Input, concatenate
from keras.optimizers import Adagrad
first_input = Input(shape=(2, ))
first_dense = Dense(1, )(first_input)
second_input = Input(shape=(2, ))
second_dense = Dense(1, )(second_input)
merge_one = concatenate([first_dense, second_dense])
third_input = Input(shape=(1, ))
merge_two = concatenate([merge_one, third_input])
model = Model(inputs=[first_input, second_input, third_input], outputs=merge_two)
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
model.compile(optimizer=ada_grad, loss='binary_crossentropy',
metrics=['accuracy'])
要在评论中回答问题:
1)结果和合并如何连接?假设您是说它们如何连接。
串联的工作方式如下:
a b c
a b c g h i a b c g h i
d e f j k l d e f j k l
即行只是连接在一起。
2)现在,x1
输入到第一,x2
输入到第二,x3
输入到第三。
result
和merged
(或merged2
)层在答案的第一部分如何相互连接?