Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (47.6k points)

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.

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())

1 Answer

0 votes
by (33.1k points)

Categorical Accuracy: It evaluates the index of the maximal true value is equal to the index of the maximal predicted value. you need to specify your target (y) as one-hot encoded vector (e.g. in case of 3 classes, when a true class is a second class, y should be (0, 1, 0).

For example:

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())

Sparse Categorical Accuracy: It evaluates the maximal true value is equal to the index of the maximal predicted value. You need should only provide an integer of the true class (in the case from the previous example - it would be 1 as classes indexing is 0-based).

For example:

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())

Hope this answer helps.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...