如何解决Keras中的sigmoid层使用什么阈值?
这是我的模型
IMG_WIDTH,IMG_HEIGHT = 300,300
preTrainedModel = ResNet50(
include_top=False,weights="imagenet",input_shape=(IMG_WIDTH,IMG_HEIGHT,3)
)
for layer in preTrainedModel.layers(:-7): #(-m) means up to the last m layers
layer.trainable = False # False means this layer is not Trainable--i.e. leave weights as is.
#Add new layers to solve this specific problem
flattenedLayer = layers.Flatten()(preTrainedModel.output) #***CHECK THIS OUT****
flattenedLayer = GlobalAveragePooling2D()(preTrainedModel.output)
firstDense = layers.Dense(1024,'relu')(flattenedLayer)
dropOut = layers.Dropout(0.2)(firstDense) #Dropout is applied to the previous layer
classifierLayer = layers.Dense(1,'sigmoid')(dropOut) #sigmoid outputs a number between 0 and 1. What is the threshold for belonging to one class rather than another?
finalModel = tf.keras.Model(preTrainedModel.input,classifierLayer)
print("Done setting up model!")
训练进行得很顺利。基于真阳性、真阴性、曲线下面积、准确性等指标,测试集的结果很好。 问题是我不知道最后一层使用什么阈值来决定放入 A 类而不是 B 类。我想知道这一点,因为当我运行 finalModel.predict(test_generator) 时,我得到的是概率,然后我必须映射到 0 或 1。我将使用结果与真实标签进行比较。类似的东西:
prediction_probabilities = finalModel.predict(testing_generator) #gives probabilities
###HOW DO DECIDE ON THRESHOLD
predictions = prediction_probabilities > 0.189
然后我可以用 sklearn.metrics.confusion_matrix 得到混淆矩阵,比如
actual_classes = testing_generator.classes
CM = confusion_matrix(actual_classes,predictions)
如何确定合适的阈值(替代上面显示的 0.189)?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。