本文主要是介绍第二章、评测指标与方法(晚点再继续补充),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上两章基本上是AI生成,下面正真的干货来临,注意看~
一、评测指标
1、准确率
- 插件命中率 :针对一批量数据,智能体独有的命中插件的概率
- 知识库命中率:针对一批量数据,智能体独有的命中知识库的概率
- 工作流命中率:针对一批量数据,智能体独有的命中工作流的概率
- 精确率:系统返回的文档中与查询相关的文档比例,精确率=TP/(TP+FP).目标值:≥ 0.80
- 召回率(Recall):是针对原样本而言的,其含义是在实际为正的样本中被预测为正样本的概率。目标值:>=0.75
- 误报率:误报率=FP/(FP+TN)
- 漏报率:漏报率=FN/(FN+TP)
- F1:精确率和召回率的调和平均值。F1分数=(2∗精确率∗召回率)/(精确率+召回率),目标值>=0.77
2、生成质量
- Blue:衡量生成文本与参考文本(通常是人工生成的)之间的相似度,常用于机器翻译评估。目标值>=0.30
- Rouge:评估生成文本与参考文本之间的重合度,包括ROUGE-1(基于单词的重合)、ROUGE-2(基于短语的重合)和ROUGE-L(基于最长公共子序列的重合)。目标值>=0.35
- Meteor:评估机器翻译质量的指标,它考虑了词义和词序。目标值:>=0.25
3、响应速度
- 首响时间:检索第一个字节响应回来的时间
- 生成速度:从检索结果到生成完成文本所需时间
- 端到端延迟:从用户输入查询到返回最终生成文本的整体时间
网上应该有很多对于样本混淆矩阵解释,这里就不再过多赘述。稍后在评测和数据集中会说明具体是怎么标记及使用的
二、评测方法
基于python环境,提前先安装
pip install jieba
pip install scikit-learn
pip install rouge
pip install nltk
1、BlueValueTools(生成质量Blue)
避免每次都要下载nltk_data,可以将nltk_data放在一个文件夹下
import osfrom xxx.utils import jieba import nltk from nltk.translate.bleu_score import SmoothingFunctionclass BlueValueTools(object):def __init__(self):nltk_data_path = os.path.abspath('./nltk_data')nltk.data.path.append(nltk_data_path)# 将句子分词并转换为n-gram格式def sentence_to_ngrams(self,sentence, n):words = jieba.lcut(sentence)return set(nltk.ngrams(words, n))# 计算BLEU指标def calculate_bleu(self,reference, candidate):smooth = SmoothingFunction().method4scores = []# for n in range(1, max_n + 1):reference_ngrams = self.sentence_to_ngrams(reference, 1)candidate_ngrams = self.sentence_to_ngrams(candidate, 1)return nltk.translate.bleu_score.sentence_bleu([reference_ngrams], candidate_ngrams, smoothing_function=smooth) # # 测试数据 # reference = "关机并断开电源:确保电脑完全关闭,并从电源插座中拔掉电源线。" # candidate = "关机并断开电源:确保电脑完全关闭,并从电源插座中拔掉电源线。" # # 计算BLEU指标 # bleu_scores = BlueValueTools().calculate_bleu(reference, candidate) # print(bleu_scores)
2、MeteorValueTools(生成质量Meteor)
import osfrom xxx.utils import jieba import nltk from nltk.translate.meteor_score import meteor_scoreclass MeteorValueTools(object):def __init__(self):nltk_data_path = os.path.abspath('./nltk_data')nltk.data.path.append(nltk_data_path)def preprocess_text(self, text):return ' '.join(jieba.cut(text)).split()def calculate_meteor(self, reference, candidate):processed_reference = self.preprocess_text(reference)processed_candidate = self.preprocess_text(candidate)scores = meteor_score([processed_reference], processed_candidate)return scoresif __name__ == '__main__':reference = "关机并断开电源:确保电脑完全关闭,并从电源插座中拔掉电源线。"candidate = "关机并断开电源。"scores = MeteorValueTools().calculate_meteor(reference, candidate)print(scores)
3、RougeValueTools(生成质量Rouge)
from rouge import Rouge from xxx.utils import jiebaclass RougeValueTools(object):def preprocess_text(self, text):# 分词并连接成字符串words = jieba.lcut(text)processed_text = ' '.join(words)return processed_textdef calculate_rouge(self, reference, candidate):processed_reference = self.preprocess_text(reference)processed_candidate = self.preprocess_text(candidate)rouge = Rouge()scores = rouge.get_scores(processed_candidate, processed_reference, avg=True)# print(scores['rouge-1']['f'])# print(scores['rouge-2']['f'])# print(scores['rouge-l']['f'])scores = {'rouge-1': scores['rouge-1']['f'],'rouge-2': scores['rouge-2']['f'],'rouge-l': scores['rouge-l']['f']}return scoresif __name__ == '__main__':reference = "关机并断开电源:确保电脑完全关闭,并从电源插座中拔掉电源线。"candidate = "关机并断开电源。"scores = RougeValueTools().calculate_rouge(reference, candidate)print(scores)
4、数据集样本计算(先分享工具类,后面数据集中详细讲解)
from sklearn.metrics import confusion_matrixclass PrecisionScoreTool:def __init__(self, actual_labels, predicted_labels):self.actual_labels = actual_labelsself.predicted_labels = predicted_labelsdef calculate_metrics(self):# 计算混淆矩阵cm = confusion_matrix(self.actual_labels, self.predicted_labels)# 从混淆矩阵提取 TP、TN、FP、FNTP = cm[1, 1]TN = cm[0, 0]FP = cm[0, 1]FN = cm[1, 0]print(TP, TN, FP, FN)recall = TP / (TP + FN) if (TP + FN) != 0 else 0precision = TP / (TP + FP) if (TP + FP) != 0 else 0F1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) != 0 else 0FPR = FP / (FP + TN) if (FP + TN) != 0 else 0FNR = FN / (FN + TP) if (FN + TP) != 0 else 0return precision, recall, FPR, FNR, F1if __name__ == '__main__':calculator = PrecisionScoreTool([1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])precision, recall, FPR, FNR, F1 = calculator.calculate_metrics()print(precision)print(recall)print(FPR)print(FNR)print(F1)
最后关于响应速度,后面章节会结合大模型调用进行说明
这篇关于第二章、评测指标与方法(晚点再继续补充)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!