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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:02:33  来源:igfitidea点击:

Keras - Difference between categorical_accuracy and sparse_categorical_accuracy

pythonkerasdeep-learningneural-networkclassification

提问by reindeer

What is the difference between categorical_accuracyand sparse_categorical_accuracyin 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_accuracysparse_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_accuracychecks to see if the indexof the maximal true value is equal to the indexof the maximal predicted value.

categorical_accuracy进行检查以查看是否索引的最大真值的等于索引的最大预测值。

sparse_categorical_accuracychecks 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_accuracycorresponds to a one-hotencoded vector for y_true.

从上面 Marcin 的回答中,categorical_accuracy对应于 的one-hot编码向量y_true

回答by Marcin Mo?ejko

So in categorical_accuracyyou 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, yshould be (0, 1, 0). In sparse_categorical_accuracyyou need should only provide an integer of the true class (in the case from previous example - it would be 1as classes indexing is 0-based).

因此,categorical_accuracy您需要将目标 ( y)指定为单热编码向量(例如,在 3 个类的情况下,当真正的类是第二类时,y应该是(0, 1, 0)sparse_categorical_accuracy您只需要提供真实类的整数(在来自上一个示例的案例 -1就像类索引是0基于的一样)。