I would like to get the top-k accuracy for my model in keras.
I have found a post here:How to calculate top5 accuracy in keras? suggesting the following:
from keras import backend as K
import tensorflow as tf
top_values, top_indices = K.get_session().run(tf.nn.top_k(_pred_test, k=5))
The output just gives me two arrays:
top_values:
array([[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
...,
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.]], dtype=float32)
top_indices:
array([[12, 0, 1, 2, 3],
[13, 0, 1, 2, 3],
[15, 0, 1, 2, 3],
...,
[12, 0, 1, 2, 3],
[17, 0, 1, 2, 3],
[18, 0, 1, 2, 3]])
How would I calculate the actual score from these values?
Accuracy calculates the percentage of predicted values (yPred) that match with actual values (yTrue). For a record, if the predicted value is equal to the actual value, it is considered accurate. We then calculate Accuracy by dividing the number of accurately predicted records by the total number of records.
Understanding TopN Accuracy:Top N accuracy measures how often your predicted class falls in the top N values of your softmax distribution. Say you have an image classification model with 4 classes — Dog, Cat, Horse and Mule . You test your model on 5 images and get the following results.
Top-k Accuracy classification score. This metric computes the number of times where the correct label is among the top k labels predicted (ranked by predicted scores).
SparseCategoricalAccuracy classThis metric creates two local variables, total and count that are used to compute the frequency with which y_pred matches y_true . This frequency is ultimately returned as sparse categorical accuracy : an idempotent operation that simply divides total by count .
Ok here is the code that works for me, in case someone else stumbles upon similar issues - the missing link for me was using ".evaluate":
import functools
top3_acc = functools.partial(keras.metrics.top_k_categorical_accuracy, k=3)
top3_acc.__name__ = 'top3_acc'
model.compile(Adam(lr=.001),#
optimizers.RMSprop(lr=2e-5),
loss='categorical_crossentropy',
metrics=['accuracy','top_k_categorical_accuracy',top3_acc])
model.evaluate(X_test, y_test)
where 'top_k_categorical_accuracy' gives me the score for k=5 (standard) and top3_acc can be adjusted by changing k=3 in the function call.
Alternatively,
from keras.metrics import top_k_categorical_accuracy
def topKacc(Y_true, Y_pred):
return top_k_categorical_accuracy(Y_true,
Y_pred,
k = int_here)
metrics = [topKacc, ...]
model.compile(...,
metrics=metrics)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With