如何在keras中连接两层?
我有一个具有两层的神经网络的示例。第一层有两个参数,并有一个输出。第二个参数应接受一个参数作为第一层的结果,并附加一个参数。它看起来应该像这样:
x1 x2 x3
\ / /
y1 /
\ /
y2
因此,我创建了一个具有两层的模型,并尝试将它们合并,但是它返回一个错误:The first layer in a Sequential model
must get an "input_shape" or "batch_input_shape"
argument.
在line上result.add(merged)
。
模型:
first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))
second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))
result = Sequential()
merged = Concatenate([first, second])
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
result.add(merged)
result.compile(optimizer=ada_grad, loss=_loss_tensor, metrics=['accuracy'])
-
之所以会出现错误
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 which 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'])
要在评论中回答问题:
- 结果和合并如何连接?假设您的意思是它们如何连接。
串联的工作方式如下:
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
即行只是连接在一起。
- 现在,
x1
输入到第一,x2
输入到第二,x3
输入第三。