Keras度量中的二元精度,将一个样本预测为正例和负例的阈值是多少?

7
keras中的二元分类指标binary_accuracy在预测一个样本为阳性或阴性时,使用的阈值是多少?该阈值是否为0.5?如何进行调整?我想将阈值设为0.80,如果预测值为0.79,则被视为负样本;否则,如果预测值为0.81,则被视为正样本。

在这里提问之前,请确保您已经确定了最佳的提问方式(以及改进了英语)。因此,请查看如何创建一个最小、完整和可验证的示例并适当地更新它,好吗? - diogo
3个回答

6

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 Heddneck
def binary_accuracy(y_true, y_pred, threshold=0.5): threshold = math_ops.cast(threshold, y_pred.dtype) y_pred = math_ops.cast(y_pred > threshold, y_pred.dtype) return K.mean(math_ops.equal(y_true, y_pred), axis=-1)在Tensorflow中似乎很常见,但在Keras中不是。这个函数与Keras中的普通二元准确度完全相同。遇到了和你一样的问题,建议的方法肯定有问题。 - Axl
@MonicaHeddneck,这个问题有什么进展吗? - ace

6

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接