how does Keras compute validation accuracy and tra

2019-04-15 16:39发布

问题:

I would like to know how Keras computes the validation and training accuracies for multi-class classification problems (i.e., the function used).I set my model compile as follows:

model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

But I am trying to understand how is the validation accuracy and training accuracy is computed (i.e., explicit formulae).

I know the validation and training loss are determined by the 'categorical_crossentropy, but I am not sure about the accuracies.

Note: this is NOT a duplicate of this post. My question is looking for an explanation of the python function used by Keras to compute accuracy not the theoretical details given in the mentioned post.

回答1:

You can find the metrics file and their implementation in the Keras github repo. In this case following metric applies:

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

This calculates the accuracy of a single (y_true, y_pred) pair by checking if the predicted class is the same as the true class. It does this so comparing the index of the highest scoring class in y_pred vector and the index of the actual class in the y_true vector. It returns 0 or 1.

It uses this function to calculate the overall accuracy of the data set, by using the conventional accuracy formula, which is defined as

(amount of correct guesses)/(total amount of guesses)