完全一致(Exact Match)とトークン単位の適合率・再現率・F1スコア
Agent支援エンジニアリングチェック
前提条件
第70日のベンチマークタスク設計およびPythonテキスト処理。
学習目標
SQuAD標準のテキスト正規化パイプラインを用い、完全一致(EM)とトークン単位の適合率・再現率・F1スコアを数式とコードで実装する。
手順
参照資料: この活動に割り当てられた既存の読解・練習時間内で、Hugging Face LLM Evaluation Guidebook (legacy edition) の contents/automated-benchmarks/designing-your-automatic-evaluation.md: Choosing a metric; contents/troubleshooting/troubleshooting-reproducibility.md を参照し、ローカル課題とのつながりを一つ自分の言葉で説明する。既存の演習と合格基準は維持し、外部資料が開けない場合は本コース独自の説明を使う。1. 完全一致(Exact Match)の定義: 正規化後の予測文字列と正解文字列が100%一致する場合に1.0、それ以外を0.0とする二値指標。
2. SQuAD標準正規化(Rajpurkar et al. 2016)の手順: (a) 小文字化、(b) 句読点・記号の除去、(c) 英語冠詞('a', 'an', 'the')の削除、(d) 連続空白の単一化。
3. トークン多重集合(Multiset)に基づく指標計算: 予測トークン列Pと正解トークン列Tの重複度を考慮した共通トークン数を Counter(P) & Counter(T) により算出。
4. 適合率 = 共通数 / |P|、再現率 = 共通数 / |T|、F1 = 2 * P * R / (P + R)。空文字列の境界条件(双方が空ならF1=1.0、片方のみ空ならF1=0.0)を漏れなく処理する。
演習と予測記録
SQuAD正規化を適用し、完全一致およびトークンF1を計算して {'em': float, 'f1': float} を返却するPython関数 compute_token_f1 を実装してください。
collections.Counter を用いてトークンの重複度を考慮した集合交差を求めます。
import re
from collections import Counter
def normalize_answer(s):
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
return re.sub(r'[^a-zA-Z0-9 ]', '', text)
def lower(text):
return text.lower()
return white_space_fix(remove_articles(remove_punc(lower(s))))
def compute_token_f1(prediction, ground_truth):
pred_tokens = normalize_answer(prediction).split()
gold_tokens = normalize_answer(ground_truth).split()
em = 1.0 if pred_tokens == gold_tokens else 0.0
if len(pred_tokens) == 0 or len(gold_tokens) == 0:
return {'em': em, 'f1': 1.0 if pred_tokens == gold_tokens else 0.0}
common = Counter(pred_tokens) & Counter(gold_tokens)
num_same = sum(common.values())
if num_same == 0:
return {'em': em, 'f1': 0.0}
precision = 1.0 * num_same / len(pred_tokens)
recall = 1.0 * num_same / len(gold_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return {'em': em, 'f1': f1}
多重集合トークン交差とSQuAD正規化に基づくトークンF1およびEMを正確に実装できること。
公式ソースリソースと権利証跡
Hugging Face LLM Evaluation Guidebook (legacy edition)
該当セクション・タイムスタンプ: contents/automated-benchmarks/designing-your-automatic-evaluation.md: Choosing a metric; contents/troubleshooting/troubleshooting-reproducibility.md
検証バージョンと日付: Public edition checked 2026-11-01; moving pages may change
Teaching-oriented evaluation design and reproducibility guidance complements the benchmark papers. Stable methodological chapters only; not a current tooling/version reference.
Held-out data, basic language-model metrics and experimental comparisons.
Use the original localized explanations, exercises and checks already present in this activity; no external account or purchase is required for that fallback.
比較対象候補リソースと選定評価 (2)
Retained for the multi-metric framework; the guidebook makes practical choices easier to teach.
Useful implementation cross-check; tool configuration alone does not teach metric choice or evaluator limitations.
SQuAD: 100,000+ Questions for Machine Comprehension of Text
該当セクション・タイムスタンプ: Section 6.1 Model Evaluation (Exact match and macro-averaged F1)
検証バージョンと日付: EMNLP 2016 / arXiv:1606.05250v3 (Verified: 2026-10-30)
Defines the standard NLP Exact Match (EM) and token-level Macro/Micro F1 evaluation metrics, establishing standard text normalization rules (lowercasing, punctuation stripping, article removal).
Tokenization, text normalization, precision, recall, and harmonic mean F1.
Exact normalization code, multiset token intersection logic, and F1 calculations are completely coded in lesson exercises.
比較対象候補リソースと選定評価 (2)
Adds unanswerable questions; SQuAD 1.1 formalizes the foundational token F1 and EM mathematical definitions.
Conversational multi-turn QA; SQuAD provides a widely adopted implementation of normalized token F1.
BLEU: a Method for Automatic Evaluation of Machine Translation
該当セクション・タイムスタンプ: Section 2.1.1 Modified n-gram precision on blocks of text; Section 2.3 BLEU details
検証バージョンと日付: ACL 2002 Anthology (Verified: 2026-10-30)
Foundational literature introducing modified n-gram precision and brevity penalties, providing the mathematical contrast between precision-oriented n-gram clipping and recall-oriented sequence metrics.
N-gram precision, modified n-gram clipping, brevity penalty.
N-gram clipping algorithms, geometric mean precision, and brevity penalty formulas are completely implemented in lesson code.
比較対象候補リソースと選定評価 (2)
Incorporates stemming and WordNet synonymy; BLEU is the fundamental n-gram precision baseline required for quantitative comparison.
Character-level n-gram F-score; Papineni BLEU provides the historic foundation for token-based precision clipping.