python - Keras softmax 输出和准确率

标签 python tensorflow keras neural-network

这是 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/

相关文章:

python - 我可以让 pylint 警告不需要的抑制线吗?

python - redis管道execute()方法什么时候被阻塞?

python - 来自形状为 [32,1] 的张量的 Keras 有状态 LSTM 错误 : Specified a list with shape [4, 1]

variables - `tf.random_normal` 和 `tf.random_normal_initializer` 有什么区别?

python - 如何标准缩放 3D 矩阵?

python - 即使在 matplotlib 中的 plt.cla() 之后,如何强制 xlim 永久不变

python - 从 DateTime 系列中的多个 CSV 文件设置 MultiIndex DataFrame

tensorflow - 值错误 : List argument 'values' to 'ConcatV2' Op with length 0 shorter than minimum length 2

python - Keras 无法使用回调来存储检查点

python - keras 和 pydot 中的 plot_model 问题