简体   繁体   中英

How does Keras compute validation accuracy and training accuracy for multi-class classification problems?

I would like to know how Keras computes the validation and training accuracies for multi-class classification problems (ie, 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 (ie, 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.

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM