Keras如何编写并行模型,用于多类预测

时间:2019-11-06 12:51:22

标签: python keras

我有以下模型,其中keep_features = 900左右,y是类的一键编码。我正在寻找下面的架构(keras可以做到这一点,表示法的想法是什么样的,特别是平行部分和隐喻)

model = Sequential()
model.add(Dense(keep_features, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(3, activation='softmax'))
model.compile(loss=losses.categorical_crossentropy,optimizer='adam',metrics=['mae', 'acc'])

enter image description here

1 个答案:

答案 0 :(得分:1)

通过“多输入和多输出模型”一章here,您可以为所需的模型制作类似的东西:

K = tf.keras
input1 = K.layers.Input(keep_features_shape)

denseA1 = K.layers.Dense(256, activation='relu')(input1)
denseB1 = K.layers.Dense(256, activation='relu')(input1)
denseC1 = K.layers.Dense(256, activation='relu')(input1)

batchA1 = K.layers.BatchNormalization()(denseA1)
batchB1 = K.layers.BatchNormalization()(denseB1)
batchC1 = K.layers.BatchNormalization()(denseC1)

denseA2 = K.layers.Dense(64, activation='relu')(batchA1)
denseB2 = K.layers.Dense(64, activation='relu')(batchB1)
denseC2 = K.layers.Dense(64, activation='relu')(batchC1)

batchA2 = K.layers.BatchNormalization()(denseA2)
batchB2 = K.layers.BatchNormalization()(denseB2)
batchC2 = K.layers.BatchNormalization()(denseC2)

denseA3 = K.layers.Dense(32, activation='softmax')(batchA2) # individual layer
denseB3 = K.layers.Dense(16, activation='softmax')(batchB2) # individual layer
denseC3 = K.layers.Dense(8, activation='softmax')(batchC2) # individual layer

concat1 = K.layers.Concatenate(axis=-1)([denseA3, denseB3, denseC3])

model = K.Model(inputs=[input1], outputs=[concat1])

model.compile(loss = K.losses.categorical_crossentropy, optimizer='adam', metrics=['mae', 'acc'])

这导致: enter image description here enter image description here