Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary_accuracy in keras Metrices , what's the threshold value to predicted one sample as positive and negative cases

What's the threshold value of binary_accuracy in keras Metrices is used to predicted one sample as positive and negative cases? is that threshold value 0.5? how to adjust it? I want to set the threshold value 0.80, if the predicted value is 0.79, then it is considered a negative sample,otherwise,if the predicted value is 0.81, then it is considered a positive sample.

like image 622
jing Avatar asked Jan 14 '17 14:01

jing


1 Answers

binary_accuracy don't have threshold param but you can easily define one yourself.

import keras
from keras import backend as K

def threshold_binary_accuracy(y_true, y_pred):
    threshold = 0.80
    if K.backend() == 'tensorflow':
        return K.mean(K.equal(y_true, K.tf.cast(K.lesser(y_pred,threshold), y_true.dtype)))
    else:
        return K.mean(K.equal(y_true, K.lesser(y_pred,threshold)))

a_pred = K.variable([.1, .2, .6, .79, .8, .9])
a_true = K.variable([0., 0., 0.,  0., 1., 1.])

print K.eval(keras.metrics.binary_accuracy(a_true, a_pred))
print K.eval(threshold_binary_accuracy(a_true, a_pred))

Now you can use it as metrics=[threshold_binary_accuracy]

like image 130
indraforyou Avatar answered Sep 20 '22 22:09

indraforyou