71 / 90

第 71 天:定量評測指標與統計信心區間分析

數學推導完全匹配(EM)、Token F1、ROUGE-L 指標,計算 held-out 集對數似然機率,並運用非參數自助法估計小樣本信心區間。

自學方法與 Python 工程基礎 480 分鐘有效研習(8 小時)
01 120 分鐘

完全匹配(EM)與 Token 級精確率、召回率及 F1 值

Agent 輔助工程檢查

前置要求

第 70 天基準任務構建與 Python 文本處理基礎。

學習目標

數學推導完全匹配(EM)與基於 SQuAD 文本規範化的多重集合 Token 級 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, EM):二值化判定指標,當且僅當模型預測文本在規範化後與真實標籤字串完全一致時輸出 1.0,否則輸出 0.0。
2. 學習標準 SQuAD 規範化流程(Rajpurkar et al. 2016):(a) 全文轉小寫、(b) 剔除所有標點符號、(c) 剔除英語常見冠詞('a'、'an'、'the')、(d) 合併多餘連續空格。
3. 推導基於多重集合(Multiset)的 Token 級指標:設 P 和 T 分別為預測與參考 Token 列表。共通 Token 計數 = sum(min(count_P(w), count_T(w)))。
4. 計算 Precision = 共通數 / |P|;Recall = 共通數 / |T|;F1 = 2 * P * R / (P + R)。嚴謹處理邊界:若兩者均為空字串則 F1=1.0;若僅有一方為空則 F1=0.0。

自主實作與記錄

編寫 Python 函數 compute_token_f1(prediction, ground_truth),嚴格執行 SQuAD 文本規範化,計算並返回包含 {'em': float, 'f1': float} 的字典結果。

獨立掌握標準

獨立推導並編碼實現多重集合 Token F1 與 EM 指標,100% 覆蓋各類空值與異常邊界條件。

權威來源資源與權利證明

linking CC BY-NC-SA 4.0 noncommercial share-alike

Hugging Face LLM Evaluation Guidebook (legacy edition)

作者 / 機構: Hugging Face evaluation-guidebook contributors | 原始出處連結 ↗

對應章節 / 時間戳: 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)
HELM ↗ compared_alternative [Open Access / CC BY 4.0]

Retained for the multi-metric framework; the guidebook makes practical choices easier to teach.

LM Evaluation Harness ↗ compared_alternative [MIT]

Useful implementation cross-check; tool configuration alone does not teach metric choice or evaluator limitations.

linking Open Access / CC BY-SA 4.0 share-alike

SQuAD: 100,000+ Questions for Machine Comprehension of Text

作者 / 機構: Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, Percy Liang | 原始出處連結 ↗

對應章節 / 時間戳: 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.

linking CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material) noncommercial share-alike

BLEU: a Method for Automatic Evaluation of Machine Translation

作者 / 機構: Kishore Papineni, Salim Roukos, Todd Ward, Wei-Jing Zhu | 原始出處連結 ↗

對應章節 / 時間戳: 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)
Banerjee & Lavie. METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments ↗ compared_alternative [CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material)]

Incorporates stemming and WordNet synonymy; BLEU is the fundamental n-gram precision baseline required for quantitative comparison.

Popović. chrF: character n-gram F-score for automatic MT evaluation ↗ compared_alternative [CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material)]

Character-level n-gram F-score; Papineni BLEU provides the historic foundation for token-based precision clipping.

02 120 分鐘

序列重合度指標:ROUGE-L 與最長公共子序列(LCS)

獨立掌握檢查(無 AI 輔助)

前置要求

第 71 天活動 1 及動態規劃(DP)基礎。

學習目標

透過動態規劃實現最長公共子序列(LCS)演算法,並推導 ROUGE-L 查準率、查全率與 F 度量。

操作指引

1. 學習 ROUGE 指標家族(Lin 2004):ROUGE-N 衡量連續 n-gram 召回率,而 ROUGE-L 採用最長公共子序列(LCS),無需詞對連續即可評估跨度語序結構。
2. 動態規劃求解 LCS:設參考序列 X(長度 m)與候選序列 Y(長度 n)。構建 DP 表 dp[i][j] = dp[i-1][j-1] + 1(X[i-1] == Y[j-1] 時)或 max(dp[i-1][j], dp[i][j-1])(不相等時)。時間複雜度 O(m * n)。
3. 計算 ROUGE-L:查全率 R_lcs = LCS(X, Y) / m;查準率 P_lcs = LCS(X, Y) / n;F 度量 F_lcs = ((1 + beta^2) * R * P) / (R + beta^2 * P)。常規任務取 beta=1.0。
4. 辨析核心缺陷:ROUGE-L 對否定語義反轉極度不敏感(例如插入單個否定詞 'not' 僅使 LCS 長度減少 1,而句意完全相反)。

自主實作與記錄

不依賴外部三方庫,使用純 Python 與原生二維列表構建動態規劃矩陣,編寫 compute_rouge_l(candidate, reference, beta=1.0) 函數並輸出精確評測分值。

獨立掌握標準

手寫動態規劃求解 LCS 並推導 ROUGE-L,能夠清晰剖析其在語義否定下的盲區。

權威來源資源與權利證明

linking CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material) noncommercial share-alike

ROUGE: A Package for Automatic Evaluation of Summaries

作者 / 機構: Chin-Yew Lin | 原始出處連結 ↗

對應章節 / 時間戳: Section 3 ROUGE-L: Longest Common Subsequence

驗證版本與日期: ACL 2004 Workshop Anthology (Verified: 2026-10-30)

入選理由:

Primary literature establishing ROUGE-N and ROUGE-L (Longest Common Subsequence), deriving sequence-level recall, precision, and F-measure without requiring consecutive n-gram matches.

來源前置條件:

Longest Common Subsequence (LCS), dynamic programming, recall-oriented evaluation.

安全回退方案(離線彈性):

Dynamic programming LCS algorithms and ROUGE-L scoring equations are completely self-contained in lesson code.

對比候選資源與選型分析 (2)

Embedding similarity based metric; ROUGE-L provides exact deterministic surface-form sequence overlap without model weights.

Optimal transport metric; ROUGE-L is the required universal baseline for summary and instruction evaluation.

linking Apache License 2.0 / Open Access

A Call for Clarity in Reporting BLEU Scores

作者 / 機構: Matt Post | 原始出處連結 ↗

對應章節 / 時間戳: Section 2 Problem Description

驗證版本與日期: WMT 2018 / arXiv:1804.08771v1 (Verified: 2026-10-30)

入選理由:

Critical paper on metric reproducibility, proving how inconsistent tokenization schemes and smoothing parameters alter reported metrics by multiple percentage points, demanding standardized evaluation strings.

來源前置條件:

Tokenization standardization, evaluation parameters, string normalization pipelines.

安全回退方案(離線彈性):

Standardized token normalization pipelines, deterministic tokenizer flags, and signature strings are implemented in curriculum exercises.

對比候選資源與選型分析 (2)
Google Research rouge-score Python library ↗ compared_alternative [Apache 2.0]

Specific ROUGE implementation; Post et al. provides the overarching reproducibility doctrine for NLP metric reporting.

Hugging Face evaluate library ↗ compared_alternative [Apache 2.0]

Unified evaluation framework; Post establishes the critical methodology for eliminating hidden tokenization divergence.

03 120 分鐘

Held-Out 驗證集對數似然評測與長度懲罰困惑度

Agent 輔助工程檢查

前置要求

第 71 天活動 1 至 2 及自迴歸條件機率建模。

學習目標

計算候選續寫序列的自迴歸對數似然,應用長度指數歸一化懲罰,並推導困惑度(Perplexity)。

操作指引

1. 自迴歸生成序列對數似然公式:log P(Y | X) = sum_{t=1}^T log P(y_t | X, y_{2. 困惑度(Perplexity, PPL)公式:PPL = exp(- (1 / T) * sum log P(y_t | X, y_{3. 長度偏差痛點:直接累加 log P(Y | X) 會懲罰長序列(每個額外 Token 都是負數累加);而簡單的算術平均 (1/T) 則會導致模型傾向於極短、平庸的通用詞彙。
4. 長度指數懲罰歸一化:Score(Y | X) = log P(Y | X) / ((5 + T)^alpha / (5 + 1)^alpha),其中 alpha in [0.6, 1.0] 控制長度偏好平滑度。
5. 消除先驗 Token 偏置:計算無條件生成對数似然 log P(Y),使用邊際分數 Score_norm = log P(Y|X) - log P(Y) 消除高頻詞偽影。

自主實作與記錄

編寫 Python 函數 rank_with_length_penalty(candidates, alpha=0.7),其中 candidates 把 4 個候選選項的標籤映射到 (total_log_prob, token_length)。使用累積對數似然 [-1.2, -0.8, -0.5, -2.1] 與 Token 長度 [1, 2, 3, 5],按 alpha=0.7 的長度懲罰歸一化分值從高到低排序。

獨立掌握標準

熟練計算自迴歸對數似然、長度懲罰因子與驗證集困惑度,準確完成多選排序任務。

權威來源資源與權利證明

linking Open Access / OpenAI Public Publication

Language Models are Unsupervised Multitask Learners

作者 / 機構: Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever | 原始出處連結 ↗

對應章節 / 時間戳: Section 3.1 Language Modeling

驗證版本與日期: OpenAI Technical Report 2019 (Verified: 2026-10-30)

入選理由:

Foundational demonstration of zero-shot task transfer via log-likelihood evaluation over target candidate completions, formalizing length normalization and held-out perplexity scoring.

來源前置條件:

Autoregressive conditional likelihood, cross-entropy loss, perplexity exponentiation.

安全回退方案(離線彈性):

Conditional log-likelihood equations, length normalization factors, and held-out cross-entropy calculations are fully derived in lesson material.

對比候選資源與選型分析 (2)

Focuses on empirical power-law loss curves; Radford et al. establishes the zero-shot log-likelihood task evaluation protocol.

Optimal compute scaling; Radford provides the fundamental zero-shot conditional probability formulation.

linking arXiv non-exclusive license / Open Access

The Curious Case of Neural Text Degeneration

作者 / 機構: Ari Holtzman, Jan Buys, Li Du, Maxwell Forbes, Yejin Choi | 原始出處連結 ↗

對應章節 / 時間戳: Section 4.2 Perplexity

驗證版本與日期: ICLR 2020 / arXiv:1904.09751v2 (Verified: 2026-10-30)

入選理由:

Primary source introducing Nucleus (Top-p) sampling and analyzing the failure of greedy decoding (loops, repetition).

來源前置條件:

Softmax probabilities, cumulative probability distributions from Days 25-27 and 41-42.

安全回退方案(離線彈性):

Top-p truncation algorithms and worked probability cutoffs are completely explained in the lesson.

對比候選資源與選型分析 (2)

Introduces top-k sampling; Holtzman et al. dynamic nucleus threshold is compared directly against top-k.

Hugging Face Generation Strategies Guide ↗ compared_alternative [Apache 2.0]

Practical library guide; Holtzman provides the foundational mathematical reasoning.

04 120 分鐘

綜合評估:小樣本評測切片上的非參數自助法(Bootstrap)信心區間

獨立掌握檢查(無 AI 輔助)

前置要求

第 71 天活動 1 至活動 3。

學習目標

實現 Bradley Efron 非參數自助法(Bootstrap)重採樣,構建 95% 信心區間並執行配對假設檢驗。

操作指引

1. 警惕小切片評測統計噪音:在 N=50 或 100 樣本的小切片上,兩個模型間 3% 或 4% 的準確率差異通常完全落在隨機採樣誤差範圍內。
2. 非參數 Bootstrap 原理(Efron 1979):從規模為 N 的原始評測集合中,執行有放回抽樣(sampling with replacement),構建 B=10,000 次重採樣樣本集 S*_b。
3. 在每次重採樣副本上重新計算目標指標 theta*_b(如準確率或 Macro F1)。
4. 構建經驗分位數 95% 信心區間:提取重採樣分佈的 2.5% 與 97.5% 分位點 [CI_lower, CI_upper]。
5. 實現配對顯著性檢驗(Koehn 2004):同步計算模型 B 與 A 的差值 delta*_b = metric(B*_b) - metric(A*_b)。經驗 p 值即為 delta*_b <= 0 的重採樣頻次佔比。

自主實作與記錄

編寫 Python 函數 bootstrap_confidence_interval(scores, num_resamples=10000, alpha=0.05),對輸入的樣本打分陣列執行 10,000 次自助法重採樣,返回均值與 95% 信心區間。

獨立掌握標準

掌握非參數自助法重採樣與信心區間構建方法,能夠在小樣本評測中科學辨識虛假優勢。

權威來源資源與權利證明

linking Institute of Mathematical Statistics copyright / Academic linking only

Bootstrap Methods: Another Look at the Jackknife

作者 / 機構: Bradley Efron | 原始出處連結 ↗

對應章節 / 時間戳: N/A

驗證版本與日期: The Annals of Statistics 1979 (Verified: 2026-10-30)

入選理由:

Original statistical work introducing non-parametric bootstrap resampling, enabling rigorous confidence interval estimation without Gaussian distribution assumptions.

來源前置條件:

Empirical distributions, sampling with replacement, confidence intervals, standard error.

安全回退方案(離線彈性):

Bootstrap resampling algorithms with replacement, empirical percentile confidence interval bounds, and standard error formulas are coded in Python.

對比候選資源與選型分析 (2)

Requires strong normality assumptions; Efron's non-parametric bootstrap applies directly to non-Gaussian NLP accuracy metrics.

Tests sharp null hypotheses under exchangeability; bootstrap directly yields confidence intervals for point estimates.

linking CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material) noncommercial share-alike

Statistical Significance Tests for Machine Translation Evaluation

作者 / 機構: Philipp Koehn | 原始出處連結 ↗

對應章節 / 時間戳: Section 5 Bootstrap Resampling and Section 6 Paired Bootstrap Resampling

驗證版本與日期: EMNLP 2004 Anthology (Verified: 2026-10-30)

入選理由:

Pioneering application of paired bootstrap resampling in NLP evaluation, demonstrating how to compute statistical significance and prevent false claims of model superiority on small test slices.

來源前置條件:

Paired bootstrap resampling, hypothesis testing, p-values, test set variance.

安全回退方案(離線彈性):

Paired bootstrap hypothesis testing, delta confidence intervals, and empirical p-value computation are completely implemented in lesson code.

對比候選資源與選型分析 (2)
Dror et al. The Hitchhiker's Guide to Testing Statistical Significance in NLP ↗ compared_alternative [CC BY 4.0 (ACL Anthology, 2016 or later)]

Modern comprehensive survey of significance testing; Koehn 2004 is the established, accessible paired bootstrap paper in NLP.

Berg-Kirkpatrick et al. An Empirical Investigation of Statistical Significance in NLP ↗ compared_alternative [CC BY-NC-SA 3.0 (ACL Anthology, pre-2016 material)]

Evaluates optimizer vs test-set randomness; Koehn provides the direct, actionable paired test-set bootstrap.