Python Keras - categorical_accuracy 和 sparse_categorical_accuracy 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44477489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keras - Difference between categorical_accuracy and sparse_categorical_accuracy
提问by reindeer
What is the difference between categorical_accuracy
and sparse_categorical_accuracy
in Keras? There is no hint in the documentation for these metrics, and by asking Dr. Google, I did not find answers for that either.
Kerascategorical_accuracy
和sparse_categorical_accuracy
在 Keras 中有什么区别?这些指标的文档中没有任何提示,通过询问 Google 博士,我也没有找到答案。
The source code can be found here:
源代码可以在这里找到:
def categorical_accuracy(y_true, y_pred):
return K.cast(K.equal(K.argmax(y_true, axis=-1),
K.argmax(y_pred, axis=-1)),
K.floatx())
def sparse_categorical_accuracy(y_true, y_pred):
return K.cast(K.equal(K.max(y_true, axis=-1),
K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
K.floatx())
采纳答案by Matti Lyra
Looking at the source
看源码
def categorical_accuracy(y_true, y_pred):
return K.cast(K.equal(K.argmax(y_true, axis=-1),
K.argmax(y_pred, axis=-1)),
K.floatx())
def sparse_categorical_accuracy(y_true, y_pred):
return K.cast(K.equal(K.max(y_true, axis=-1),
K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
K.floatx())
categorical_accuracy
checks to see if the indexof the maximal true value is equal to the indexof the maximal predicted value.
categorical_accuracy
进行检查以查看是否索引的最大真值的等于索引的最大预测值。
sparse_categorical_accuracy
checks to see if the maximal true value is equal to the indexof the maximal predicted value.
sparse_categorical_accuracy
检查最大真实值是否等于最大预测值的索引。
From Marcin's answer above the categorical_accuracy
corresponds to a one-hot
encoded vector for y_true
.
从上面 Marcin 的回答中,categorical_accuracy
对应于 的one-hot
编码向量y_true
。
回答by Marcin Mo?ejko
So in categorical_accuracy
you need to specify your target (y
) as one-hot encoded vector (e.g. in case of 3 classes, when a true class is second class, y
should be (0, 1, 0)
. In sparse_categorical_accuracy
you need should only provide an integer of the true class (in the case from previous example - it would be 1
as classes indexing is 0
-based).
因此,categorical_accuracy
您需要将目标 ( y
)指定为单热编码向量(例如,在 3 个类的情况下,当真正的类是第二类时,y
应该是(0, 1, 0)
。sparse_categorical_accuracy
您只需要提供真实类的整数(在来自上一个示例的案例 -1
就像类索引是0
基于的一样)。