问题描述
我有一个二元分类问题,类别背景 (bg) = 0,信号 (sig) = 1,我正在为此训练 NN.出于监控目的,我正在尝试使用 TensorFlow 后端在 Keras 中实现自定义指标,该指标执行以下操作:
I have a binary classification problem with categories background (bg) = 0, signal (sig) = 1, for which I am training NNs. For monitoring purposes, I am trying to implement a custom metric in Keras with TensorFlow backend that does the following:
1) 计算我的 NN 输出的阈值,这将导致 X 的误报率(将 bg 分类为信号)(在这种情况下 X = 0.02,但它可以是任何东西).
1) Calculate the threshold on my NN output which would result in a false positive rate (classifying bg as signal) of X (in this case X = 0.02, but it could be anything).
2) 计算此阈值下的真阳性率.
2) Calculate the true positive rate at this threshold.
给定 numpy 数组 y_true, y_pred,我会写一个函数:
Given numpy arrays y_true, y_pred, I would write a function like:
def eff_at_2percent_metric(y_true, y_pred):
#Find list of bg events
bg_list = np.argwhere(y_true < 0.5)
#Order by the NN output
ordered_bg_predictions = np.flip(np.sort(y_pred[bg_list]),axis=0)
#Find the threshold with 2% false positive rate
threshold = ordered_bg_predictions[0.02*round(len(ordered_bg_list))]
#Find list of signal events
sig_list = np.argwhere(y_true > 0.5)
#Order these by NN output
ordered_sig_predictions = np.sort(y_pred[sig_list])
#Find true positive rate with this threshold
sig_eff = 1 - np.searchsorted(ordered_sig_predictions,threshold)/len(ordered_sig_predictions)
return sig_eff
当然,这行不通,因为要实现自定义指标,y_true 和 y_pred 应该是 TensorFlow 张量而不是 numpy 数组.有什么办法可以让我正常工作吗?
Of course, this does not work because to implement a custom metric, y_true and y_pred are supposed to be TensorFlow tensors rather than numpy arrays. Is there any way I can make this work correctly?
推荐答案
对特异性的敏感性,我认为这是等效的(特异性是 1 减去 FPR).
There's a metric for sensitivity at specificity, which I believe is equivalent (specificity is one minus FPR).
这篇关于自定义 TensorFlow 指标:给定假阳性率下的真阳性率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!