文字生成的評估指標

Deep Learning for Text with PyTorch

Shubham Jain

Instructor

評估文字生成

  • 文字生成任務產生類似人類的文字
  • 一般的準確率與 F1 等指標不適用於此類任務
  • 需要能評估生成文字品質的指標
  • BLEU 與 ROUGE

用於文字生成的 Dalle 聊天機器人

Deep Learning for Text with PyTorch

BLEU(Bilingual Evaluation Understudy)

  • 比對生成文字與參考文字
  • 檢查 n-gram 的出現情況
  • 在句子「The cat is on the mat」中
    • 1-gram(uni-gram):[the, cat, is, on, the, mat]
    • 2-gram(bi-gram):["the cat", "cat is", "is on", "on the", "the mat"]
    • 依此類推至 n-gram
  • 完全符合:分數為 1.0
    • 0 表示完全不相符
Deep Learning for Text with PyTorch

用 PyTorch 計算 BLEU 分數

from torchmetrics.text import BLEUScore

generated_text = ['the cat is on the mat'] real_text = [['there is a cat on the mat', 'a cat is on the mat']]
bleu = BLEUScore() bleu_metric = bleu(generated_text, real_text) print("BLEU Score: ", bleu_metric.item())
BLEU Score: tensor(0.7598)
Deep Learning for Text with PyTorch

ROUGE(Recall-Oriented Understudy for Gisting Evaluation)

  • 以兩種方式比較生成文字與參考文字
  • ROUGE-N:考量兩者間重疊的 n-gram(N=1 表示 unigram,2 表示 bigram,等等)
  • ROUGE-L:找出兩者的最長共同子序列(LCS)
  • ROUGE 指標:
    • F-measure:精確率與召回率的調和平均
    • Precision:生成文字中 n-gram 與參考文字匹配的比例
    • Recall:參考文字中 n-gram 與生成文字匹配的比例
  • 前綴 'rouge1'、'rouge2'、'rougeL' 分別對應 1-gram、2-gram、或 LCS
Deep Learning for Text with PyTorch

用 PyTorch 計算 ROUGE 分數

from torchmetrics.text import  ROUGEScore

generated_text='Hello, how are you doing?' real_text= "Hello, how are you?"
rouge = ROUGEScore()
rouge_score = rouge([generated_text], [[real_text]]) print("ROUGE Score:", rouge_score)
Deep Learning for Text with PyTorch

ROUGE 分數:輸出

ROUGE Score: {'rouge1_fmeasure': tensor(0.8889), 
              'rouge1_precision': tensor(0.8000), 
              'rouge1_recall': tensor(1.),

'rouge2_fmeasure': tensor(0.8571), 'rouge2_precision': tensor(0.7500), 'rouge2_recall': tensor(1.),
'rougeL_fmeasure': tensor(0.8889), 'rougeL_precision': tensor(0.8000), 'rougeL_recall': tensor(1.),
'rougeLsum_fmeasure': tensor(0.8889), 'rougeLsum_precision': tensor(0.8000), 'rougeLsum_recall': tensor(1.)}
Deep Learning for Text with PyTorch

考量與限制

  • 評估的是詞彙出現,非語意理解
  • 對生成文字長度敏感
  • 參考文字的品質會影響分數
Deep Learning for Text with PyTorch

一起來練習吧!

Deep Learning for Text with PyTorch

Preparing Video For Download...