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

标签 python keras

我有以下模型,其中 keep_features=900 左右,y 是类的 one-hot 编码。我正在寻找下面的架构(这对于 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

最佳答案

随章节“多输入多输出模型”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

关于python - Keras 如何编写并行模型,用于多类预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58730615/

相关文章:

python - 近似小于或等于的可微分张量运算?

python - keras.models load_model 中的 TypeError ('Keyword argument not understood:' , 'groups' )

tensorflow - 在拟合和预测模型之前,如何将 logits 传递给 sigmoid_cross_entropy_with_logits?

python - 删除了 argparse.py

Python:对列表进行排序

python - 使用 for 循环创建元组。

tensorflow - 不了解类 UNET 架构中的数据流,并且对 Conv2DTranspose 层的输出有问题

python - 当图像以 0 到 9 的小数进行评级时,Earth Mover Loss 的输入类型应该是什么(Keras、Tensorflow)

python - 带有指标 DataFrame 的累积和 pandas DataFrame

python - 如何在django中将数据保存到外键中