我正在进行一个说话人识别项目,我已经训练好了模型,它的准确率是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发布于 2021-10-05 10:45:26
我认为你有一个二进制分类任务->它是一个“未知的说话人”还是不是。而且,如果我理解正确的话,您想要优化阈值。换句话说,您不希望使用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个概率,但你真的只需要一个。从第一个你可以很容易地推断出第二个。
https://stackoverflow.com/questions/69437638
复制相似问题