简体   繁体   English

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

[英]Keras how to write parallel model, for multiclass prediction

I have the following model, where keep_features=900 or so,y is one-hot encoding of classes.我有以下 model,其中 keep_features=900 左右,y 是类的 one-hot 编码。 I am looking for the architecture below though(is that possible with keras, and what would the notation idea look like,specially the parallel part and the concatination)我正在寻找下面的架构(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'])

在此处输入图像描述

With the chapter "Multi-input and multi-output models" here you can make something like this for your desired model:通过此处的“多输入和多输出模型”一章,您可以为所需的 model 制作类似的东西:

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'])

This results in:这导致: 在此处输入图像描述 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM