Trying to understand the difference between categorical_accuracy and sparse_categorical_accuracy in metrics.py

2,135 views
Skip to first unread message

travel...@gmail.com

unread,
May 22, 2016, 6:45:44 PM5/22/16
to Keras-users
When I look at some of the metrics in metrics.py, two of the metrics I see are:


def categorical_accuracy(y_true, y_pred):
   
return K.mean(K.equal(K.argmax(y_true, axis=-1),
                  K
.argmax(y_pred, axis=-1)))




def sparse_categorical_accuracy(y_true, y_pred):
   
return K.mean(K.equal(K.max(y_true, axis=-1),
                          K
.cast(K.argmax(y_pred, axis=-1), K.floatx())))


Am I correct in believing that both of these metrics are designed for a 1-hot encoding, but that the sparse_categorical_accuracy casts outputs to type float, whereas the categorical_accuracy does not?




boll...@gmx.de

unread,
Jun 1, 2016, 2:29:12 PM6/1/16
to Keras-users, travel...@gmail.com
I believe that one is for use with the "categorical_crossentropy" loss, the other for use with the "sparse_categorical_crossentropy" loss. The difference should just be in the internal representation of the training data; with categorical crossentropy, you have 1-hot vectors, while with the sparse version you just use numeric indices.

- Marcel

Reply all
Reply to author
Forward
0 new messages