【发布时间】:2018-08-21 01:09:10
【问题描述】:
我有一个二元分类问题,类别背景 (bg) = 0,信号 (sig) = 1,为此我正在训练 NN。出于监控目的,我正在尝试使用 TensorFlow 后端在 Keras 中实现一个自定义指标,该指标执行以下操作:
1) 计算我的 NN 输出的阈值,这将导致 X 的误报率(将 bg 分类为信号)(在本例中 X = 0.02,但它可以是任何值)。
2) 计算该阈值下的真阳性率。
给定 numpy 数组 y_true、y_pred,我会编写如下函数:
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 数组。有什么办法可以让这个工作正常吗?
【问题讨论】:
标签: python tensorflow keras