如何制作神经网络keras的复杂输出

时间:2018-05-29 12:09:50

标签: python neural-network keras

我想结合2个显示类概率的神经网络。 有人说它是影像上的一只猫。 第二个说猫有领子。

如何在神经网络的输出上使用softmax激活函数?

请看图片了解主要想法:

Network

2 个答案:

答案 0 :(得分:1)

您可以使用functional API创建多输出网络。基本上每个输出都是单独的预测。有点像:

in = Input(shape=(w,h,c)) # image input
latent = Conv...(...)(in) # some convolutional layers to extract features
# How share the underlying features to predict
animal = Dense(2, activation='softmax')(latent)
collar = Dense(2, activation='softmax')(latent)
model = Model(in, [animal, coller])
model.compile(loss='categorical_crossentropy', optimiser='adam')

您可以拥有任意数量的单独输出。如果你只有二进制特征,你也可以有一个矢量输出,Dense(2, activation='sigmoid')和第一个条目可以预测猫与否,而第二个条目是否有一个项圈。这将是多级多标签设置。

答案 1 :(得分:1)

Juste在模型的末尾创建两个独立的密集层(使用sofmax激活),例如:

from keras.layers import Input, Dense, Conv2D
from keras.models import Model

# Input example:
inputs = Input(shape=(64, 64, 3))

# Example of model:
x = Conv2D(16, (3, 3), padding='same')(inputs)
x = Dense(512, activation='relu')(x)
x = Dense(64, activation='relu')(x)
# ... (replace with your actual layers)

# Then add two separate layers taking the previous output and generating two estimations:
cat_predictions = Dense(2, activation='softmax')(x)
collar_predictions = Dense(2, activation='softmax')(x)

model = Model(inputs=inputs, outputs=[cat_predictions, collar_predictions])