'Precision@K as a custom metric in Keras

I include Precision@K as a custom metric in Keras. According to the documentation

import keras.backend as K

def mean_pred(y_true, y_pred):
    return K.mean(y_pred)

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy', mean_pred])

I just need to calculate a function using Keras backend and pass it at the compilation step.

With Numpy only precision@k could be calculated as the following example:

def precisionatk(y_true,y_pred,k)
    precision_average = []
    idx =  (-y_pred).argsort(axis=-1)[:,:k]
    for i in range(idx.shape[0]):
        precision_sample = 0
        for j in idx[i,:]:
            if y_true[i,j] == 1:
                precision_sample += 1
        precision_sample = precision_sample / k
        precision_average.append(precision_sample)
    return np.mean(precision_average)


y_true = np.array([[0,0,1,0],[1,0,1,0]])
y_pred = np.array([[0.1,0.4,0.8,0.2],[0.3,0.2,0.5,0.1]])

print(precisionatk(y_true,y_pred,2))

0.75

So, how to I translate this to Keras backend?

EDIT: I'm working with a multilabel problem and the y_true is always an array with ones or zeros, and y_prediction each class probability.



Solution 1:[1]

You can use Keras-native implementation of Precision@k: tf.keras.metrics.TopKCategoricalAccuracy

https://www.tensorflow.org/api_docs/python/tf/keras/metrics/TopKCategoricalAccuracy

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamidreza Hosseinkhani