神经网络keras的复杂输出

2024-10-03 15:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想结合两个神经网络,这是显示类的概率。 有人说这是照片上的猫。 第二个说猫有项圈。你知道吗

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

请看图片了解主要内容:

Network


Tags: 函数内容图片神经网络概率照片softmax项圈
2条回答

只需在模型末尾创建两个单独的密集层(使用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])

可以使用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')和第一个条目可以预测cat是否存在,而第二个条目可以预测cat是否有项圈。这将是多类多标签设置。你知道吗

相关问题 更多 >