这是 Keras 模型的最后一层。
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
我知道softmax层的输出是一个数组,概率加起来为1,比如[0.1, 0.4, 0.5]
.我有一个关于使用准确性作为指标的问题。
例如,当真正的类是
[0, 0, 1]
预测概率为 [0.1, 0.4, 0.5]
,即使 0.5
是最大的概率,这个预测的准确率应该是0
, 因为 0.5 != 1
.那是对的吗?更一般地,当输出层激活为
softmax
,我们通常会得到浮点概率预测,并且我们很少有机会得到整数概率预测,如 [0, 0, 1]
.所以我们不能使用 accuracy
作为使用 softmax
时的指标作为激活。那是对的吗?
最佳答案
e.g., when the true class is [0, 0, 1] and predicted probability is [0.1, 0.4, 0.5], even if 0.5 is the largest probability, the accuracy of this prediction should be 0, because 0.5 != 1. Is that correct?
不,您将具有最大值的索引视为模型的预测。因此,在您的示例中,此样本预测将计入提高准确性。这通常称为 Top-1 精度。在图像分类中,也经常使用Top-5精度(softmax层中的前5个最大值被视为NN的猜测值,并考虑精度)。
More generally, when the output layer activation is softmax, we will normally get floating probability predictions, and in very very little chance will we get integer probability predictions like [0, 0, 1]. So we can't use accuracy as a metric when using softmax as activation. Is that correct?
从技术上讲,你永远不会得到 softmax 层输出的整数值,因为类型是浮点数。但是,是的,获得 [0.0, 0.0, 1.0] 的可能性很小。你的这个假设是不正确的,因为前提不成立。然而,当使用 Softmax 作为神经网络的分类层时,准确率是一个有效的指标。
关于python - Keras softmax 输出和准确率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63093045/