Pratyush
Pratyush

Reputation: 480

Keras calculate accuracy as +/- 1 from predicted

I am using Keras (+TensorFlow) to build a deep neural network model. In the model, I need to define my own accuracy function.

Let's say, the model predicts the time taken to do a work (in minutes, between 0 and 20). I want the model to print out accuracy if the predicted output is within +/- 2. If the predicted output is x minutes, while the expected output is x+1, I want to consider this is a correct prediction, if the expected output is x+3, I want to consider this is a wrong prediction.

This is slightly different from top_k_categorical_accuracy

Upvotes: 0

Views: 631

Answers (1)

indraforyou
indraforyou

Reputation: 9099

You can easily implement the logic using Keras backend apis .. which will also ensure your metric working both on tensorflow and theano.

Here with test:

import numpy as np
import keras
from keras import backend as K

shift = 2
def custom_metric(y_true,y_pred):
    diff = K.abs(K.argmax(y_true, axis=-1) - K.argmax(y_pred, axis=-1))
    return K.mean(K.lesser_equal(diff, shift))


t1 = np.asarray([   [0,0,0,0,0,0,1,0,0,],
                    [0,0,0,0,0,0,1,0,0,],
                    [0,0,0,0,0,0,1,0,0,],
                    [0,0,0,0,0,0,1,0,0,],
                    [0,0,0,0,0,0,1,0,0,],
                    [0,0,0,0,0,0,1,0,0,],
                ])
p1 = np.asarray([   [0,0,0,0,0,1,0,0,0,],
                    [0,0,0,0,1,0,0,0,0,],
                    [0,0,0,0,0,0,0,1,0,],
                    [0,0,0,0,0,0,0,0,1,],
                    [1,0,0,0,0,0,0,0,0,],
                    [0,0,0,0,0,0,1,0,0,],
                ])


print K.eval(keras.metrics.categorical_accuracy(K.variable(t1),K.variable(p1)))
print K.eval(custom_metric(K.variable(t1),K.variable(p1)))

now in your compile statement use it: metrics=custom_metric

Upvotes: 2

Related Questions