语言模型的评估常用指标(BLEU、ROUGE、PPL)

语言模型的评估常用指标

1.Accuracy(准确率):模型预测正确的样本数量占总样本量的比重

2.Precision(精确率):在被识别为正类别的样本中,为正类别的比例

3.Recall(召回率):在所有正类别样本中,被正确识别为正类别的比例

4.BLEU分数

评估一种语言翻译成另一种语言的文本质量的指标。它将“质量”的好坏定义为与人类翻译结果的一致性程度,取值范围是[0,1],越接近1,表明翻译质量越好。

  1. 根据‘n-gram’可以划分成多种评价指标,其中‘n-gram’指的是连续的单词个数为n,实践中,通常是取N=1~4,然后进行加权平均
  2. 计算过程:计算模型预测的句子和真实结果的N-grams模型,然后统计其匹配的个数,计算匹配度
  3. 举例
    假设机器翻译的译文candidate和一个参考翻译reference如下:
candidate:It is a nice day today
reference:today is a nice day
- 使用1-gram进行匹配
candidate:{It, is, a, nice, day, today}
reference:{today, is, a, nice, day}
结果:匹配度为5/6
- 使用2-gram进行匹配
candidate:{It is, is a, a nice, nice day, day today}
reference:{today is, is a, a nice, nice day}
结果:匹配度为3/5
- 使用3-gram进行匹配
candidate:{It is a, is a nice, a nice day, nice day today}
reference:{today is a, is a nice, a nice day}
结果:匹配度为2/4
4. 极端例子 对于:
candidate:the the the the
reference:The cat is standing on the ground
如果按照1-gram的方法进行匹配,则匹配度为1,显然是不合理的

python示例代码

from nltk.translate.bleu_score import sentence_bleu
candidate_texts = ["This", "is", "some", "generated", "text"] # 生成的文本
reference_texts = [["This", "is", "a", "reference", "text"]] # 参考文本列表
bleu_1_gram = sentence_bleu(reference_texts, candidate_texts, weights=(1,0,0,0)) # weights表示使用N-gram进行匹配的加权求和,当前的weights表示使用1-gram进行匹配的结果

5.ROUGE:在机器翻译、自动摘要、问答生成等领域常见的评估指标。通过将模型生成的摘要或者回答与参考答案进行比较计算

ROUGE相较于BLEU指标非常类似,不同的是ROUGE基于召回率,BLEU更看重准确率
在这里插入图片描述
python示例代码

import Rouge
generated_text = "This is some generated text"
reference_texts = ["This is a reference text", "This is another generated reference text"] # 参考文本列表
rouge = Rouge()
scores = rouge.get_scores(generated_text, reference_texts[0])
print("ROUGE-1 precision:", score[0]["rouge-1"]["p"]) # 0.666
print("ROUGE-1 recall:", score[0]["rouge-1"]["r"]) # 0.8
print("ROUGE-1 f1:", score[0]["rouge-1"]["f"]) # 0.727272

6.PPL:用来度量一个概率分布或概率模型预测样本的好坏程度,PPL越小,标明模型越好

  1. 参考链接:语言模型评价指标Perplexity
  2. python示例代码
import math
# 定义语料库
sentences = [
['I', 'have', 'a', 'pen'],
['He', 'has', 'a', 'book'],
['She', 'has', 'a', 'cat']
]
# 定义语言模型
unigram = {'I': 1/12, 'have': 1/12, 'a': 3/12, 'pen': 1/12,
          'He': 1/12,'has': 2/12,'book': 1/12,'She': 1/12, 'cat': 1/12}
perplexity = 0
for sentence in sentences:
    sentence_prob = 1
    for word in sentence:
        sentence_prob *= unigram[word]
    temp = -math.log(sentence_prob, 2)/len(sentence)
    perplexity+=2**temp
perplexity = perplexity/len(sentences)
print('困惑度为:', perplexity)
# 困惑度为: 8.15
  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值