keras中的二元分类指标
binary_accuracy
在预测一个样本为阳性或阴性时,使用的阈值是多少?该阈值是否为0.5?如何进行调整?我想将阈值设为0.80,如果预测值为0.79,则被视为负样本;否则,如果预测值为0.81,则被视为正样本。binary_accuracy
在预测一个样本为阳性或阴性时,使用的阈值是多少?该阈值是否为0.5?如何进行调整?我想将阈值设为0.80,如果预测值为0.79,则被视为负样本;否则,如果预测值为0.81,则被视为正样本。binary_accuracy
没有阈值参数,但您可以轻松地自行定义一个。
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))
metrics=[threshold_binary_accuracy]
。threshold=0.5
,他们应该得到与仅使用默认的“accuracy”度量相同的值,对吗?但是当我这样做时,我得到了非常不同的值... - Monica Heddneckhttps://github.com/fchollet/keras/blob/master/keras/metrics.py
def binary_accuracy(y_true, y_pred):
return K.mean(K.equal(y_true, K.round(y_pred)))