我必须输入要合并到一个网络中的输入,因此应该是这样的:
input1 input2
| |
| |
hidden hidden
Merged
我尝试了以下代码:
b1_part = Sequential()
b1_part.add(Dense(units=b1_X_train.shape[1],activation='tanh', activity_regularizer=regularizers.l2(1e-2)))
b1_part.add(Dropout(0.5))
b1_part.add(Dense(units=b1_X_train.shape[1]/4,activation='tanh', activity_regularizer=regularizers.l2(1e-2)))
b2_part = Sequential()
b2_part.add(Dense(units=b2_X_train.shape[1],activation='tanh', activity_regularizer=regularizers.l2(1e-2)))
b2_part.add(Dropout(0.5))
b2_part.add(Dense(units=b2_X_train.shape[1]/4,activation='tanh', activity_regularizer=regularizers.l2(1e-2)))
result = Concatenate(axis=1)([b1_part, b2_part])
optimizer = Adagrad()
result.compile(optimizer=optimzier, loss=BinaryFocalLoss(gamma=2),
metrics=['accuracy'])
但是得到了:
ValueError: Layer concatenate_10 was called with an input that isn't a symbolic tensor. Received type: <class 'tensorflow.python.keras.engine.sequential.Sequential'>. Full input: [<tensorflow.python.keras.engine.sequential.Sequential object at 0x7fb1cbf97048>, <tensorflow.python.keras.engine.sequential.Sequential object at 0x7fb1cbeb5358>]. All inputs to the layer should be tensors.
知道为什么吗?
答案 0 :(得分:1)
首先,请记住指定顺序模型的输入形状。
然后,您可以通过以下方式组合顺序模型并定义完整模型:
b1_part = Sequential()
b1_part.add(Dense(units=100,activation='tanh', input_shape=(100,)))
b1_part.add(Dropout(0.5))
b1_part.add(Dense(units=25,activation='tanh'))
b2_part = Sequential()
b2_part.add(Dense(units=200,activation='tanh', input_shape=(200,)))
b2_part.add(Dropout(0.5))
b2_part.add(Dense(units=50,activation='tanh'))
result = Concatenate(axis=1)([b1_part.output, b2_part.output])
result = Dense(1, activation='sigmoid')(result)
model = Model([b1_part.input, b2_part.input], result)
model.compile('adam', 'binary_crossentropy', metrics='accuracy')