语言任务的指标:困惑度与BLEU

Python 中的 LLM 入门

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

LLM任务与指标

 

语言任务的评估指标

Python 中的 LLM 入门

困惑度(Perplexity)

  • 衡量模型对下一个词预测的准确性与置信度
  • 困惑度越低=置信度越高
input_text = "Latest research findings in Antarctica show"

generated_text = "Latest research findings in Antarctica show that the ice sheet 
is melting faster than previously thought."


# Encode the prompt, generate text and decode it input_text_ids = tokenizer.encode(input_text, return_tensors="pt") output = model.generate(input_text_ids, max_length=20) generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
Python 中的 LLM 入门

困惑度输出

perplexity = evaluate.load("perplexity", module_type="metric")
results = perplexity.compute(predictions=generated_text, model_id="gpt2")

print(results)
{'perplexities': [245.63299560546875, 520.3106079101562, ....], 
 'mean_perplexity': 2867.7229790460497}
print(results["mean_perplexity"])
2867.7229790460497
  • 与基线结果对比
Python 中的 LLM 入门

BLEU

  • 基于人工参考评估翻译质量

  • 预测:LLM输出

  • 参考:人工参考
bleu = evaluate.load("bleu")


input_text = "Latest research findings in Antarctica show" references = [["Latest research findings in Antarctica show significant ice loss due to climate change.", "Latest research findings in Antarctica show that the ice sheet is melting faster than previously thought."]] generated_text = "Latest research findings in Antarctica show that the ice sheet is melting faster than previously thought."
Python 中的 LLM 入门

BLEU输出

results = bleu.compute(predictions=[generated_text], references=references)
print(results)
{'bleu': 1.0, 
 'precisions': [1.0, 1.0, 1.0, 1.0], 
 'brevity_penalty': 1.0, 
 'length_ratio': 1.2142857142857142, 
 'translation_length': 17, 
 'reference_length': 14}
  • 0–1分:越接近1=相似度越高
Python 中的 LLM 入门

让我们练习!

Python 中的 LLM 入门

Preparing Video For Download...