accuracy in Keras.metrics

 accuracy in Keras.metrics (represents the model evaluation function of keras, accuracy rate)

 

keras.metrics.accuracy(y_true,y_pred)
keras.metrics.binary_accuracy(y_true,y_pred,threshold=0.5)
keras.metrics.categorycal_accuracy(y_true,y_pred)
keras.metrics.sparse_categorical_accuracy(y_true,y_pred)
keras.metrics.top_k_categorical_accuracy(y_true,y_pred,k=5)
keras.metrics.sparse_top_k_categorical_accuracy(y_true,y_pred,k=5)

1) accuracy

The accuracy is the simplest accuracy that everyone knows. For example, we have 6 samples whose real label y_true is [0, 1, 3, 3, 4, 2], but is predicted by a model as [0, 1, 3, 4, 4, 4], that is, y_pred=[ 0, 1, 3, 4, 4, 4], then the model's accuracy=4/6=66.67%.

2) binary_accuracy

The biggest difference between binary_accuracy and accuracy is that it is suitable for 2 classifications. As can be seen from the figure above, in addition to y_true and y_pred, the calculation of binary_accuracy also has a threshold parameter, which defaults to 0.5. For example, if there are 6 samples, y_true is [0, 0, 0, 1, 1, 0], y_pred is [0.2, 0.3, 0.6, 0.7, 0.8, 0.1], then its binary_accuracy=5/6=87.5%. The specific calculation method is: 1) Compare each predicted value in y_pred with the threshold, set the value greater than the threshold to 1, and set the value less than or equal to the threshold to 0, and get y_pred_new=[0, 0, 1, 1, 1, 0] ;2) Substituting y_true and y_pred_new into 2.1 to calculate the final binary_accuracy=87.5%.

3) categorical_accuracy

categorical_accuracy is also similar to accuracy. The difference is that accuracy is for the case where both y_true and y_pred are specific labels, while categorical_accuracy is for the case where y_true is the onehot label and y_pred is the vector. For example, there are 4 samples, y_true is [[0, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 0]], y_pred is [[0.1, 0.6] , 0.3], [0.2, 0.7, 0.1], [0.3, 0.6, 0.1], [0.9, 0, 0.1]], then its categorical_accuracy is 75%. The specific calculation method is: 1) Convert y_true to a non-onehot form, that is, y_true_new=[2, 1, 1, 0]; 2) Get y_pred_new=[1, 1, 1 according to the predicted score of each sample in y_pred , 0]; 3) Substituting y_true_new and y_pred_new into 2.1 to calculate the final categorical_accuracy=75%.

4) sparse_categorical_accuracy

Same as the categorical_accuracy function, except that its y_true is a non-onehot form. For example, there are 4 samples, y_true is [2, 1, 1, 0], y_pred is [[0.1, 0.6, 0.3], [0.2, 0.7, 0.1], [0.3, 0.6, 0.1], [0.9, 0] , 0.1]], then its categorical_accuracy is 75%. The specific calculation method is: 1) According to the predicted score of each sample in y_pred, y_pred_new=[1, 1, 1, 0] is obtained; 2) Substituting y_true and y_pred_new into 2.1 to calculate the final categorical_accuracy=75%.

5) top_k_categorical_accuracy

Add top_k on the basis of categorical_accuracy. Categorical_accuracy requires that the prediction score of the sample on the true category is the maximum value of the prediction scores on all categories to be considered a prediction pair, while top_k_categorical_accuracy only requires that the prediction score of the sample on the true category ranks its prediction scores on all categories The top k names of the list will do. For example, there are 4 samples, y_true is [[0, 0, 1], [0, 1, 0], [0, 1, 0], [1, 0, 0]], y_pred is [[0.3, 0.6] , 0.1], [0.5, 0.4, 0.1], [0.3, 0.6, 0.1], [0.9, 0, 0.1]], according to the previous knowledge, we can calculate its categorical_accuracy=50%, but what is its top_k_categorical_accuracy? The answer is closely related to k. If k is greater than or equal to 3, its top_k_categorical_accuracy is undoubtedly 100%, because there are only 3 categories in total. If k is less than 3, it needs to be calculated, such as k=2, then top_k_categorical_accuracy=75%. The specific calculation method is: 1) Convert y_true to a non-onehot form, that is, y_true_new=[2, 1, 1, 0]; 2) Calculate the label of top_k of y_pred, for example, when k=2, y_pred_new = [[0, 1], [0, 1], [0, 1], [0, 2]]; 3) Calculate the accuracy rate according to whether the real label of each sample is within the top_k of the predicted label. Take the above 4 samples as an example, 2 is not in [0, 1], 1 is in [0, 1], 1 is in [0, 1], 0 is in [0, 2], a total of 4 samples are predicted correctly 3, so k=2 When top_k_categorical_accuracy=75%. To explain, the default k value is 5 when calculating top_k_categorical_accuracy in Keras.

6) sparse_top_k_categorical_accuracy

It has the same function as top_k_categorical_accuracy, except that its y_true is a non-onehot form. For example, there are 4 samples, y_true is [2, 1, 1, 0], y_pred is [[0.3, 0.6, 0.1], [0.5, 0.4, 0.1], [0.3, 0.6, 0.1], [0.9, 0] , 0.1]]. The steps to calculate sparse_top_k_categorical_accuracy are as follows: 1) Calculate the label of top_k of y_pred, for example, when k=2, y_pred_new = [[0, 1], [0, 1], [0, 1], [0, 2]]; 2 ) According to whether the real label of each sample is within the top_k of the predicted label to count the accuracy rate, the above 4 samples are taken as an example, 2 is not in [0, 1], 1 is in [0, 1], 1 is in [0, 1], 0 is within [0, 2], 4 samples are predicted to be 3 in total, so when k=2, top_k_categorical_accuracy=75%.

Guess you like

Origin blog.csdn.net/chehec2010/article/details/127029451