Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: how to get top-k accuracy

Tags:

python

keras

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?

like image 843
AaronDT Avatar asked Aug 21 '18 14:08

AaronDT


People also ask

How is accuracy calculated in keras?

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.

What is top n accuracy?

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.

What is TOPK accuracy?

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

What is SparseCategoricalAccuracy?

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 .


2 Answers

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.

like image 93
AaronDT Avatar answered Oct 10 '22 05:10

AaronDT


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)
like image 31
Shreesh Tripathi Avatar answered Oct 10 '22 07:10

Shreesh Tripathi