如何在模型推理中选择合适的决策阈值?

2024-03-28 23:22:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在进行一个说话人识别项目,我已经训练了模型,其精度为90%,但我在进行推理时遇到了一个问题,模型给出了两个概率,因为它是为两个对话者训练的,但我希望当我检测到一个不在训练集中的说话人时,告诉我他是“未知说话人”,因此,如何根据模型给出的两种概率选择决策阈值

这是有问题的代码片段:

sample_df = pd.DataFrame(list_).transpose()
sample_pred =  model.predict(sample_df)[0] # Here the model returns the name of the 
                                           # predicted speaker

sample_prob = model.predict_proba(sample_df)[0] # Here I get a list of two items, the 
                                                # probabilities for each speaker 
print(sample_prob) # Output example: [0.46 0.54]

for k,j in enumerate(sample_prob):
    if j <= 0.6 and sample_prob[k+1] <= 0.6: # How to change dynamically according to the 
                                             # result of sample_prob, this threshold ?, 
                                             # for example I put 0.6.
        sample_pred= "unknown speaker"
        break
    else:
        break

Tags: ofthesample模型dfformodelhere
1条回答
网友
1楼 · 发布于 2024-03-28 23:22:44

我认为您有一个二进制分类任务->;它是否是一个“未知说话人”。而且,如果我理解正确,您希望优化阈值。换句话说,您不希望使用0.5。由于这是一项分类任务,我将选择使验证集(而不是测试集)上的f1-score最大化的阈值,因为这意味着存在数据泄漏:

thresholds = np.arange(0, 1, 0.01)
scores = [f1_score(y_test, predictions>t) for t in thresholds]
idx = np.argmax(scores)

最佳分数由thresholds[idx]给出

我看到你输出了2个概率,但你真的只需要一个。从第一个你可以很容易地推断出第二个

相关问题 更多 >