如何在keras中连接两个层?

问题描述 投票:64回答:2

我有一个有两层神经网络的例子。第一层有两个参数,有一个输出。第二个应该采用一个参数作为第一层和另一个参数的结果。它应该是这样的:

x1  x2  x3
 \  /   /
  y1   /
   \  /
    y2

所以,我创建了一个有两层的模型,并试图合并它们,但它返回一个错误:The first layer in a Sequential model must get an "input_shape" or "batch_input_shape" argument.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'])
python machine-learning keras neural-network
2个回答
86
投票

您收到错误是因为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'])

但是,我建立具有此类输入结构的模型的首选方法是使用functional 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输入第三。


5
投票

你可以试试model.summary()(注意concatenate_XX(Concatenate)图层大小)

# merge samples, two input must be same shape
inp1 = Input(shape=(10,32))
inp2 = Input(shape=(10,32))
cc1 = concatenate([inp1, inp2],axis=0) # Merge data must same row column
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()

# merge row must same column size
inp1 = Input(shape=(20,10))
inp2 = Input(shape=(32,10))
cc1 = concatenate([inp1, inp2],axis=1)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()

# merge column must same row size
inp1 = Input(shape=(10,20))
inp2 = Input(shape=(10,32))
cc1 = concatenate([inp1, inp2],axis=1)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()

你可以在这里查看笔记本的详细信息:https://nbviewer.jupyter.org/github/anhhh11/DeepLearning/blob/master/Concanate_two_layer_keras.ipynb

© www.soinside.com 2019 - 2024. All rights reserved.