Lecture 13 · 置信度、校准与不确定性 · Confidence, Calibration & Uncertainty

Trustworthy Machine Learning · Lecture 13。个人学习讲义与配套实验,按主题整理并持续修订。

课程源码 ZIP ↓

个人整理的学习笔记,非官方讲义;课程来源与整理说明见课程介绍。配套示例未逐一运行验证。

本篇目录

对应 Maps to:Stanford CS329T Fall 2023 Lecture 10(2023-10-26,Eric Mitchell 的客座讲座 “Towards More Truthful LLMs”:LLM 的事实性问题、Transformer 回顾、“Is there even hope for factuality?”——用 CCS 探针从隐藏状态读出真假、Kadavath et al. 的校准结果、RLHF 之后的校准与 “Just Ask for Calibration”、semantic entropy;以及用自动事实性排序 + DPO 做 factuality tuning);同一周 syllabus 列出的主题:Chelsea Finn 组关于校准的工作、Yarin Gal 组关于不确定性的工作、self-consistency、prompt consistency。还对应 Fall 2023 Lecture 1 的 “Confidence” 维度(“How hot is it today?” → 让模型给出 3 个猜测和概率;“I am not sure but the answer may be…”)和 Lecture 6 项目设计里的 Confidence 阅读清单(Semantic Uncertainty;Just Ask for Calibration;Teaching Models to Express Their Uncertainty in Words;Reducing Conversational Agents’ Overconfidence through Linguistic Calibration);Fall 2024 Lecture 3(“Calibrated” 作为可信维度)与 Oct 15 “Grounding and Factuality 1”(P(True);Fine-tuning Language Models for Factuality;参考文献 Lin, Trivedi & Sun 2023 “Generating with Confidence”)。Selective prediction 的风险–覆盖率曲线和 conformal prediction(§8–§9)不在公开的 slides 里,来自公开论文。 配套代码 Code:code/lecture13/(只用 Python 标准库;5 个脚本 + 2 个工具模块,每个脚本 1–4 秒) 预计阅读时间:3 小时(含运行代码)


0. 本讲导航 · How to read this lecture

0.1 Learning goals

After this lecture you should be able to:

  1. 说清楚一个 LLM 的”置信度”是关于哪个事件的(这串字、这个答案、这个论断、“我能答对”),并解释为什么整个字符串的概率不是”答对的概率”。
  2. Measure calibration as a measurement: know why ECE is biased upward at small n, why the binning scheme matters, what a noise floor is, and why proper scoring rules (Brier, log loss) reward honesty while ECE alone can be gamed.
  3. Explain, with appropriate caution, why pre-trained LMs are often well calibrated on suitably formatted questions and why RLHF tends to make token probabilities overconfident — and what that does and does not imply about what the model “knows”.
  4. 会用 temperature scaling、Platt scaling、histogram binning 做事后重新校准,知道它们各需要什么(logits、标签、多少数据),以及为什么分布一变就失效。
  5. 比较黑盒不确定性方法——verbalized confidence、top-k 猜测、采样一致性、semantic entropy、P(True)——并说出它们共同的盲区:一致的错误。
  6. 把置信度变成行动:风险–覆盖率曲线、AURC、选阈值;linguistic calibration(用户听到的是什么);split conformal 的覆盖保证、它为什么是边际的而不是条件的、它在开放式生成上的限制,以及用 conformal risk control 做弃权。

0.2 先修与本讲范围 Prerequisites & scope

默认你已经会 Assumed本讲省略或只点到 Omitted / previewed
第 1 讲 §5.5:calibration vs discrimination、reliability table、ECE;§3.5:RLHF 让校准变差Bayesian deep learning 的细节(MC dropout、deep ensembles 的推导)
第 1 讲 §7.3、第 8 讲 §7:“能证明的就回答、其余弃权”隐藏状态探针(CCS、“Internal State”)的训练细节——第 12 讲
next-token prediction、temperature sampling、RLHF / DPO 是什么(CS329H)conformal prediction 的一般理论(full conformal、加权 conformal 的证明)
AUROC、bootstrap 置信区间(第 1 讲 §8)长文本(long-form)的 claim 级校准,只在 §7、§10 简述

本讲和第 12 讲(并行编写)分工:第 12 讲讲幻觉检测——用采样一致性、NLI、检索判断一个回答有没有依据;本讲讲量化置信度并据此行动——置信度有没有含义、怎样测、怎样修、怎样变成”答 / 不答 / 怎么说”。

0.3 本讲地图 Map

§1  什么时候该说"我不知道"?"How hot is it today?"、Mitchell 的问题
§2  置信度是关于哪个事件的?token / 字符串 / 答案 / 论断              ← 代码 01 演示 1
§3  测量校准:ECE 是一个估计量;proper scoring rules                  ← 代码 01 演示 2、3
§4  预训练为什么校准、RLHF 为什么破坏它(谨慎地)
§5  事后重新校准:temperature、Platt、histogram;黑盒;分布偏移       ← 代码 02
§6  黑盒不确定性:verbalized、top-k、一致性、semantic entropy、P(True) ← 代码 03 演示 1–4
§7  Linguistic calibration:用户听到的是什么                         ← 代码 04 演示 3
§8  Selective prediction 与弃权:风险–覆盖率、AURC、选阈值            ← 代码 04 演示 1、2
§9  Conformal prediction:覆盖保证、开放式生成、条件覆盖、风险控制    ← 代码 05
§10 从测量到改进:factuality tuning(Tian, Mitchell et al.)          ← 代码 03 演示 5
§11–15 小结、误区、练习、延伸阅读、下一讲预告

0.4 Notation

符号含义
xx;a^(x)\hat a(x)问题;模型的(贪心)回答
a⋆(x)a^\star(x)真答案(上帝视角,只用于评估)
Y=1{a^≡a⋆}Y = 1\{\hat a \equiv a^\star\}回答是否正确(≡ 表示”意思相同”,不是字面相同)
c(x)∈[0,1]c(x) \in [0, 1]某种置信度(越大越相信 Y=1Y = 1);不确定性 u(x)u(x) 取负号后当置信度用
p⋆(x)=P(Y=1∣x)p^\star(x) = P(Y = 1 \mid x)回答真正答对的概率
π(a∣x)\pi(a \mid x);ϕ(s∣a)\phi(s \mid a)模型对”答案 aa“的概率;对”说法 ss“的概率
KK;TT采样次数;采样温度
α\alpha;C(x)C(x);λ\lambda允许的错误率;conformal 预测集合;弃权阈值

本讲不报告群体差距,没有 group 0 / group 1 的符号约定问题。

0.5 关于数字和模拟器 · About the numbers and the simulator

讲义里所有实验数字都来自配套代码的实际输出(固定随机种子;Python 3.9 和 3.13 输出逐字节相同)。没有调用任何真实的 LLM:代码用一个标准库写的模拟世界(uq_world.py)——虚构实体、每题 5 个候选答案、每个答案几种说法、按流行度分三层(head / torso / tail),以及一个”训练截止后 40% 的事实变了”的 domain B。模拟 LLM 的每一种行为都是一个写明出处的参数。

这一点要读得很仔细,因为它决定了哪些数字可以拿来推断真实世界:

写进模拟器的假设(数字的高低由参数决定)模拟器能检验的机制(不依赖具体参数)
基础模型在答案层面恰好校准(真答案按它的信念抽取;Kadavath et al. 2022、GPT-4 报告)字符串概率 ≠ 答案概率;措辞会污染字面熵
RLHF 把答案概率锐化 2.5 倍(Kadavath et al. 2022 §3.3 的温度 2.5)ECE 估计量的偏差和噪声地板
口头置信度偏高、取 5% 的整数倍(Xiong et al. 2024);top-k 偏差更小(Tian et al. 2023)单调重新校准不改变 AUROC / 风险–覆盖率;分布一变就失效
P(True) 的噪声大小;domain B 的 stale rate一致性方法看不见一致的错误;conformal 的覆盖是边际的、依赖可交换性

所以,“口头置信度比 logprob 校准得好多少”这类问题,模拟器回答不了——它只是把论文的结论写成了参数。它能做的,是在一个知道真相的环境里,把每一种测量方法拿出来检查。


1. 什么时候该说”我不知道”? · When should a model say “I don’t know”?

1.1 “How hot is it today?”

Fall 2023 第 1 讲用一个小例子引出 “Confidence” 这个可信维度。问一个聊天模型 “How hot is it today?”,它回答:

The temperature outside is 95 degrees Fahrenheit. This is a very hot temperature, and it is important to stay hydrated…

流畅、具体、还附带建议——但模型既不知道你在哪里,也不知道今天是哪天。slide 上的问题是:怎样让模型说”我不知道”,或者表达不确定? 课上给出的一个做法来自 Tian et al.(2023):

Provide your 3 best guesses and the probability that each is correct (0.0 to 1.0)… G1: 75 degrees P1: 0.5 G2: 80 degrees P2: 0.4 G3: 90 degrees P3: 0.1

同一页 slide 列出了两种交付形式:一个校准的数值概率,以及用措辞表达的校准(“I am not sure but the answer may be: …”)。这两种形式分别是本讲 §3–§6 和 §7 的主题。

1.2 Convincing but not always correct

Eric Mitchell 的讲座从几个公开的失败开始:Google Bard 的发布演示里关于 James Webb 望远镜的一句错误陈述(2023 年 2 月);“新 Bing”总结 Gap 的季度财报时,有些数字是对的,有些在原文里根本不存在(5.9%、$0.42 EPS 是编出来的),对未来展望的描述也是编的。他的评语是:“It’s tempting to use them anyway!” —— 这正是问题所在:用户无法从语气里分辨哪句可靠。第 1 讲 §4.1 区分过 trust 和 trustworthiness:目标不是让用户更信任模型,而是让信任跟着模型真实的可靠性走(appropriate reliance)。要做到这一点,模型输出的置信度必须有含义。

1.3 What is confidence for?

置信度本身不是目的,它要驱动一个决定:

决定需要置信度的哪种性质本讲
回答还是弃权 / 转人工 / 去检索排序(discrimination):错的回答要排在对的后面§8
阈值设在哪里、告诉用户”有多可靠”校准(calibration):说 80% 的时候真的有 80%§3–§5、§7
一个有保证的集合或弃权规则只需要可交换性,不需要校准§9
构造训练信号(哪个回答更符合事实)同一个 prompt 内部的排序§10

第 1 讲 §5.5 已经说明:AUC 衡量排序,ECE 衡量数字的含义,两者都要看。本讲反复出现的一个模式是:很多操作(重新校准、温度缩放)只改含义不改排序;另一些(换信号、加样本)改排序。

1.4 Mitchell’s question: is there even hope?

Mitchell asked a more basic question: does an LLM model truth at all — whether a statement is true, rather than just how common it is in the data? He offered two lines of evidence:

  1. Decoding truth from hidden states. CCS (Contrast-Consistent Search; Burns, Ye, Klein & Steinhardt, ICLR 2023) uses no labels at all. It exploits the structure of truth — a statement and its negation have exactly one true member, so p(True∣x)+p(False∣x)=1p(\text{True} \mid x) + p(\text{False} \mid x) = 1, and a confident probe should not sit at 0.5 for both — to fit a probe on hidden states. The probes were as accurate as, or more accurate than, zero-shot prompting: the representation may encode truth better than what the model says.
  2. Calibrated uncertainty. Kadavath et al. (2022): a model whose confidence is calibrated must in some sense be modeling what is true, and larger models are better calibrated.

Both say “there is hope”. This lecture is about the second line: how to read out, check, repair and use a model’s confidence. Probes and “internal state” methods belong to Lecture 12.


2. 置信度是关于哪个事件的? · What event is the probability about?

2.1 Four events, four numbers

对分类器,f(x)=P^(y=1∣x)f(x) = \hat P(y = 1 \mid x) 说的是哪个事件一目了然。对 LLM,至少有四个不同的事件,每个都能读出一个”概率”:

层级事件怎样得到问题
token下一个 token 是 tt一次前向传播的 softmax一个答案通常有很多 token
sequence模型恰好输出这串字 ss各 token 概率之积 ∏tp(st∣s<t)\prod_t p(s_t \mid s_{<t})同一个意思的其他说法分走了概率
answer(语义)回答的意思是 aa把 aa 的所有说法的概率加起来;选择题里就是选项字母的概率开放式回答的说法无穷多,只能采样近似
claim / “I know”某个原子事实为真;“我能答对这题”P(True)、P(IK)、按 claim 分解需要额外的提问或训练

"P(Y=1)P(Y = 1)"——回答答对了——对应的是答案层面的事件。最容易拿到的字符串概率,说的却是另一件事。

2.2 Same answers, three probabilities

代码 01 演示 1:同一批 4000 道题、同一个贪心回答,读出三个白盒概率。base 是预训练模型;chat 是”RLHF 之后”的模型(模拟器里把答案 logits 乘以 2.5;锐化不改变 argmax,所以两个模型的回答、准确率完全相同):

signalmean confaccECEBrierNLLAUROC
base:P(exact string)0.3470.6380.2900.2950.8060.690
base:P(string)1/L^{1/L}(长度归一化)0.5690.6380.0890.2340.6620.637
base:P(answer)(各说法求和)0.6390.6380.0190.1790.5280.785
chat:P(exact string)0.4530.6380.1990.2650.7340.630
chat:P(string)1/L^{1/L}0.6540.6380.1090.2370.6790.608
chat:P(answer)0.8330.6380.1950.2260.7930.782

Three lessons:

  • The string probability answers a different question. It is badly underconfident (0.347 vs 0.638) because other phrasings of the same answer take probability mass. Splitting by answer type makes the mechanism visible: all four types have accuracy 0.63–0.65, but years (2 phrasings) get a mean string probability of 0.483 and person names (7 phrasings, including “surname only”) only 0.256. A low string probability may just mean “there are many ways to say this”. That is also why its AUROC (0.690) is worse than the answer-level probability’s (0.785).
  • Length normalization treats a symptom. Taking the geometric mean per token (Malinin & Gales 2021 discuss such normalizations) lifts the number to 0.569 and the ECE to 0.089, but the AUROC drops to 0.637: it corrects for length, not for the wrong event.
  • RLHF changes the meaning, not the ranking. chat’s answer-level AUROC (0.782) is essentially base’s (0.785), but its mean confidence rises from 0.639 to 0.833. The reliability table shows where:
binbase P(answer):count / conf / accchat P(answer):count / conf / acc
[0.2, 0.4)720 / 0.343 / 0.31490 / 0.351 / 0.189
[0.4, 0.6)1216 / 0.490 / 0.496603 / 0.512 / 0.345
[0.6, 0.8)812 / 0.697 / 0.713711 / 0.698 / 0.449
[0.8, 1.0]1252 / 0.917 / 0.9132596 / 0.961 / 0.773

chat 把 2596 个回答放进了最高的箱,平均说 0.961,实际只有 0.773 对。这和第 1 讲 §5.5 里 Model A 部署后”AUC 不变、ECE 变坏”是同一种现象。

还有一个容易被忽略的细节:chat 的字符串概率 ECE(0.199)和它的答案概率 ECE(0.195)差不多——锐化让数字偏高,措辞分散让数字偏低,两者部分抵消。一个看起来还行的 ECE,可能是两个错误互相抵消的结果。 先问清楚概率是关于哪个事件的,再看它准不准。

2.3 Aleatoric vs epistemic, and why “How hot is it” is special

Yarin Gal 组的工作(例如 Gal & Ghahramani 2016 的 MC dropout)把不确定性分成两类:aleatoric(数据本身的随机性,再多数据也消不掉)和 epistemic(模型知识不足,原则上可以靠更多数据减少)。对一个有参数后验 p(θ∣D)p(\theta \mid D) 的模型,总的预测熵可以分解为

H[ Eθ p(y∣x,θ) ]⏟total  =  Eθ H[p(y∣x,θ)]⏟aleatoric  +  I(y;θ∣x)⏟epistemic\underbrace{H\big[\,\mathbb E_\theta\, p(y \mid x, \theta)\,\big]}_{\text{total}} \;=\; \underbrace{\mathbb E_\theta\, H\big[p(y \mid x, \theta)\big]}_{\text{aleatoric}} \;+\; \underbrace{I(y; \theta \mid x)}_{\text{epistemic}}

LLM 的情形更混乱。“How hot is it today?” 的不确定性主要来自问题不完整(缺少地点和时间)——再好的模型也不能回答;“Veloria 的首都是什么?“的不确定性来自模型不知道。对 LLM 我们通常拿不到参数后验,只能对输出采样,于是两种不确定性混在一起。实际的含义:一个歧义的问题会让 semantic entropy 变高,即使模型”什么都知道”。对于”这个回答对不对”这件事,这样的高熵是合理的;但如果你想用不确定性决定”该去检索还是该追问用户”,就需要区分这两种来源(追问用户解决歧义,检索解决无知)。


3. 测量校准 · Measuring calibration, carefully

3.1 The target: top-label calibration

对 LLM 最常用的定义是关于被选中的回答的(top-label / confidence calibration):

P(Y=1 ∣ c(x)=q)=qfor all q.P\big(Y = 1 \,\big|\, c(x) = q\big) = q \qquad \text{for all } q.

第 1 讲 §5.5 的 reliability table 和 ECE 就是在估计左边:把 cc 分箱,比较每个箱的平均置信度和准确率,ECE=∑bnbn ∣cˉb−accb∣\text{ECE} = \sum_b \frac{n_b}{n}\,|\bar c_b - \text{acc}_b|。

3.2 ECE is an estimator, and it is biased

ECE 是一个从有限样本估计出来的数。代码 01 演示 2 拿一个完美校准的分数(真实校准误差 = 0)和 chat 的分数(真实误差 0.194,用 20 万道题和已知的 p⋆p^\star 算出),在不同的样本量和分箱下各测 200 次取平均:

nbase w-5w-15w-50m-15chat w-5w-15w-50m-15完美校准 w-15 的 95% 分位
1000.0640.1100.1930.1260.2030.2160.2550.2090.154
3000.0380.0640.1140.0720.1960.1990.2160.1960.089
10000.0210.0350.0640.0400.1940.1940.1990.1930.051
30000.0120.0200.0370.0240.1940.1940.1950.1940.028

(w = 等宽箱 equal-width,m = 等量箱 equal-mass,数字是箱数。)

Upward bias from noise. A perfectly calibrated score measures ECE 0.193 at n = 100 with 50 bins, and still 0.020 at n = 3000 with 15 bins. Each bin’s accuracy is a noisy estimate, and the absolute value turns noise of either sign into a positive error. For a bin with nbn_b samples and true accuracy pbp_b, E ∣accb−pb∣≈2/π pb(1−pb)/nb\mathbb E\,|\text{acc}_b - p_b| \approx \sqrt{2/\pi}\,\sqrt{p_b(1-p_b)/n_b}, so

E[ECE]  ≈  2π  1n∑bnb pb(1−pb)  ∝  B/n  for B equal bins.\mathbb E[\text{ECE}] \;\approx\; \sqrt{\tfrac{2}{\pi}}\;\frac{1}{n}\sum_b \sqrt{n_b\,p_b(1 - p_b)} \;\propto\; \sqrt{B/n}\ \text{ for } B \text{ equal bins}.

More bins or fewer samples → a higher floor. The last column is the noise floor: the ECE that a perfectly calibrated model exceeds 5% of the time. At n = 300 it is 0.089 — measuring 0.08 on 300 questions says almost nothing.

Equal-mass bins are not a free fix. I expected adaptive (equal-mass) bins to reduce the bias, as is often recommended (Nixon et al. 2019). For a perfectly calibrated score they did the opposite: 0.126 vs 0.110 at n = 100. The formula explains why: for a fixed total, ∑bnb\sum_b \sqrt{n_b} is largest when all bins hold the same count (Cauchy–Schwarz). Equal-width bins, by contrast, leave some bins nearly empty, and those barely contribute. Equal-mass binning has real advantages — no meaningless near-empty bins, and Roelofs et al. (2022) pair it with a monotonicity-based choice of BB to reduce bias — but it is a trade-off, and it needs a noise floor like any other choice.

Downward bias from coarse bins. Within a bin, over- and under-confidence can cancel (Kumar, Liang & Ma 2019). For chat, which is overconfident everywhere, I saw no underestimation even with 5 bins (0.194). It appears when the error changes sign inside a bin: an S-shaped distortion q=σ(2.5 logit p)q = \sigma(2.5\,\text{logit}\,p) with a true error of 0.113 measures 0.104 with 3 bins. Both directions of bias exist; the upward one dominates at the sample sizes typical of LLM evaluation sets (a few hundred questions).

实用的建议:报告 ECE 时写明 n、箱数和分箱方式;同时报告同样设置下完美校准分数的 ECE(噪声地板)或一个 bootstrap 区间;比较两个模型时用同样的 n 和分箱。这是第 1 讲 §8.1”指标也有误差棒”在校准上的版本。

3.3 Proper scoring rules

A scoring rule S(q,y)S(q, y) scores a reported probability qq against the outcome yy. It is proper if reporting the true probability is optimal: when Y∼Bernoulli(p)Y \sim \text{Bernoulli}(p), EY[S(q,Y)]\mathbb E_Y[S(q, Y)] is minimized at q=pq = p (Gneiting & Raftery 2007). Two classics:

  • Brier score (q−y)2(q - y)^2 (Brier 1950): expected value p(1−q)2+(1−p)q2p(1-q)^2 + (1-p)q^2, derivative 2(q−p)2(q - p), minimized at q=pq = p.
  • Log score −ylog⁡q−(1−y)log⁡(1−q)-y\log q - (1-y)\log(1-q) (= NLL): derivative −p/q+(1−p)/(1−q)-p/q + (1-p)/(1-q), also zero at q=pq = p.

ECE is not a scoring rule of this kind: you can drive it to zero by always reporting the base rate. The Murphy (1973) decomposition shows what Brier rewards:

Brier  ≈  ∑bnbn(cˉb−accb)2⏟REL(校准误差)  −  ∑bnbn(accb−yˉ)2⏟RES(区分能力)  +  yˉ(1−yˉ)⏟UNC(题目难度)\text{Brier} \;\approx\; \underbrace{\textstyle\sum_b \frac{n_b}{n}(\bar c_b - \text{acc}_b)^2}_{\text{REL(校准误差)}} \;-\; \underbrace{\textstyle\sum_b \frac{n_b}{n}(\text{acc}_b - \bar y)^2}_{\text{RES(区分能力)}} \;+\; \underbrace{\bar y(1 - \bar y)}_{\text{UNC(题目难度)}}

代码 01 演示 3 把一个完美校准的分数 pp 故意扭曲(n = 20000):

reportmean qECEBrierNLLAUROCRELRES
honest q=pq = p0.6450.0060.18020.5290.7800.00010.0496
overconfident σ(2.5 logit p)\sigma(2.5\,\text{logit}\,p)0.6750.1150.19710.6380.7800.01560.0478
mildly over σ(1.25 logit p)\sigma(1.25\,\text{logit}\,p)0.6570.0290.18130.5340.7800.00110.0500
underconfident σ(0.5 logit p)\sigma(0.5\,\text{logit}\,p)0.5980.0790.18870.5570.7800.00850.0494
always the base rate0.6400.0000.23030.6530.5000.00000.0000
always 0.95(“I’m sure”)0.9500.3090.32611.1100.5000.09580.0000
  • 说真话的 Brier 和 NLL 都最小;AUROC 对前四行完全相同(单调变换不改变排序)。
  • “永远报基础准确率”的 ECE 是完美的 0.000,但 RES = 0、Brier 比说真话差得多:校准好但没用。所以 ECE 必须和 AUROC 或一个 proper score 一起报告。
  • 最优点附近很平:轻微的过度自信只让 Brier 差 0.0011。只看一个总分很难发现小的校准问题,这时 REL 项和 reliability table 更敏感。
  • NLL 对”自信地答错”罚得最重:“always 0.95” 的 NLL 是 1.110。

4. 预训练与 RLHF:校准从哪里来、又去了哪里 · Pre-training, RLHF and calibration

4.1 Why pre-trained LMs are (often) calibrated

Pre-training minimizes next-token cross-entropy — exactly the log score of §3.3, a proper scoring rule. Optimize it well on the training distribution and the token probabilities approach the true conditional probabilities: honesty is the optimal policy. This explains an empirical fact: Kadavath et al. (2022) found large pre-trained models well calibrated on multiple-choice and true/false questions in the right format (options listed in the prompt, answered with a letter, few-shot examples), with calibration improving with model size — the plot on Mitchell’s slide.

The argument has three qualifications. (1) It guarantees calibration on the training distribution, for the next-token event. (2) Turning that into “is the answer correct” calibration requires a format in which an answer is exactly one token (a multiple-choice letter) — the event problem of §2. (3) When the distribution moves (a new domain, facts that changed after the cutoff), the guarantee is gone (§5.4).

4.2 Evidence that post-training hurts

Three pieces of evidence, all from the readings:

  • GPT-4 technical report (OpenAI 2023). On a subset of MMLU, the pre-trained model’s calibration plot is close to the diagonal (ECE ≈ 0.007); after post-training with PPO it is noticeably worse (ECE ≈ 0.074). 第 1 讲 §3.5 已经提过这张图。
  • Kadavath et al. (2022, §3.3). A quick experiment with RLHF policies trained as in Bai et al. (2022): they “naively appear very miscalibrated”, and raising the temperature to 2.5 for every evaluation mostly resolves it. The authors add a caveat: more intensive RL training might distort calibration in ways a temperature cannot fix.
  • Tian et al. (2023, “Just Ask for Calibration”). Their starting point, summarized on Mitchell’s slide: “By default, RLHF’d LLMs are worse-calibrated than pre-RLHF.” They then ask how to get calibrated confidences out of RLHF models such as ChatGPT, GPT-4 and Claude anyway (§6.1).

4.3 Why, cautiously

There is no single proven mechanism, but several plausible ones point the same way:

  1. The objective is no longer a proper scoring rule. RLHF maximizes expected reward minus a KL penalty. Reward is a function of the response, not a score of the probabilities, so nothing rewards the policy for keeping its distribution honest. Maximizing expected reward is mode-seeking: if one answer gets the most reward, the optimum moves mass toward it, and only the KL term (with strength β) pulls back. Kadavath et al. describe this as RL fine-tuning tending to “collapse” predictions toward the behaviors that get the most reward. A temperature of 2.5 undoing it is consistent with a mostly uniform sharpening.
  2. Preferences may favor confident-sounding answers. Zhou, Hwang, Ren & Sap (ACL 2024) report that annotators in preference datasets used for alignment tend to be biased against texts that express uncertainty, and that deployed LMs rarely express uncertainty; when asked for a confidence, their confident answers were wrong on average 47% of the time. A reward model trained on such data pushes away from hedging.
  3. Mitchell’s argument about labels. RLHF encourages whatever makes labelers happy, and judging “is this factually accurate” is much harder than judging “do I like this response”. So human labels only weakly encourage truth, and more weakly still calibrated expressions of truth.

What this does not mean: that the RLHF model “knows less”. Its ranking can be intact (our chat’s AUROC is unchanged by construction), knowledge can still be decodable from hidden states (CCS), and Tian et al. found that asking the RLHF model to state a confidence works better than reading its token probabilities. “The probabilities got sharper” and “the model lost its knowledge” are different claims.

在我们的模拟器里,§4.2 的第二条直接成了一个参数(锐化 2.5)。所以代码 02 找回 T̂ ≈ 2.5 是设计,不是发现:它演示的是”如果失真是一个温度,温度缩放能修好它”。


5. 事后重新校准 · Post-hoc recalibration

5.1 Temperature scaling

Guo et al.(2017)的做法:在一个有标签的校准集上,找一个标量 T>0T > 0,使 softmax(z/T)\text{softmax}(z/T) 的 NLL 最小:

T^=arg⁡min⁡T 1n∑i=1n−log⁡softmax(zi/T)ai⋆.\hat T = \arg\min_T\ \frac{1}{n}\sum_{i=1}^n -\log \text{softmax}\big(z_i / T\big)_{a^\star_i}.

为什么只用一个参数?(1) 它不改变 argmax,所以不改变准确率;(2) 一个参数几乎不会过拟合,几百条标签就够;(3) 现代网络的失真常常就是”整体太尖”。在 log T 上做一维搜索即可(代码用黄金分割)。代码 02 演示 1(chat,校准集 1000 条):

T0.51.01.52.02.53.04.0
校准集 NLL2.2561.2851.0290.9460.9280.9380.989

拟合得到 T^=2.507\hat T = 2.507,测试集(4000 条)上:

signal → recalibrationmeanECEBrierAUROC
P(answer), raw0.8400.2060.2320.786
P(answer), temperature T^=2.51\hat T = 2.510.6470.0160.1790.786
P(answer) → Platt (a = 0.42, b = −0.60)(只有 top-1 概率)0.6530.0210.1790.785
verbalized, raw0.8130.1800.2350.731
verbalized → Platt (a = 0.85, b = −0.78)0.6510.0370.1980.731
verbalized → histogram binning(8 箱)0.6480.0190.1970.729
agreement of 10 samples, T = 1, raw0.8430.2190.2450.731
agreement T = 1 → Platt0.6510.0160.1920.731
agreement of 10 samples, T = 2.5, raw(不用标签)0.6480.0690.1970.747

(测试准确率 0.635。)

5.2 Platt scaling and histogram binning

当你只有一个标量分数(top-1 概率、口头置信度、一致率)时:

  • Platt scaling(Platt 1999):p=σ(a⋅logit(c)+b)p = \sigma(a\cdot\text{logit}(c) + b),两个参数,就是在 logit(c)\text{logit}(c) 上做一维逻辑回归。a<1a < 1 表示原来过度自信。在 top-1 概率上它得到 a=0.42≈1/2.5a = 0.42 \approx 1/2.5——又一次”找回”了模拟器的锐化系数。
  • Histogram binning(Zadrozny & Elkan 2001):按置信度分 BB 个等量箱,新置信度 = 箱内准确率。非参数,不假设形状。(Isotonic regression,Zadrozny & Elkan 2002,是它的单调版本。)

一个没有计划到的结果:Platt 修口头置信度只修到 0.037,histogram binning 修到 0.019。看口头置信度的可靠性表(测试集 A)就明白了:

stated(箱内平均)0.4330.5330.6300.7270.8270.937
count5015340577910371566
accuracy0.2600.4120.4000.4660.5830.848

说 0.63 和说 0.73 的准确率几乎一样(0.400 vs 0.466),到了 0.937 那一档才跳到 0.848:这不是 logit 上的一条直线,两个参数的模型装不下(形状偏差)。代价换到另一边:代码 02 演示 2 改变校准集大小(30 次重复):

n_caltemperature(白盒)Platt(口头)histogram-10(口头)
300.044 ± 0.0250.089 ± 0.0390.162 ± 0.059
1000.034 ± 0.0160.057 ± 0.0200.088 ± 0.028
3000.026 ± 0.0050.039 ± 0.0110.051 ± 0.017
10000.022 ± 0.0030.035 ± 0.0070.038 ± 0.011

(未校准时分别是 0.190 和 0.167;测试集本身的噪声地板约 0.019。)这是一个标准的 bias–variance 权衡:温度缩放 1 个参数,30 条标签就有用;Platt 2 个参数,收敛快但停在 0.035(偏差);histogram binning 30 条时几乎没用(每箱约 3 个样本),1000 条时追上 Platt 而且还在下降。标签少时选参数少的方法,标签多时选灵活的方法。

5.3 When post-hoc recalibration is impossible (or limited)

你能拿到什么能做什么不能做什么
全部候选的 logits(开源模型、选择题)温度缩放、矩阵 / 向量缩放—
只有 top-1(或 top-k)logprob(部分 API)对这个标量做 Platt / histogram多类温度缩放
只有文本(多数聊天 API)对口头置信度或采样一致率做 Platt / histogram;调采样温度任何关于 token 概率的操作
没有自己领域的标签几乎什么都不能可靠地做所有重新校准都需要标签

两点值得强调:

  • Recalibration fixes meaning, not discrimination. Every method above is (nearly) monotone, so the AUROC stays where it was: 0.731 for verbalized before and after Platt, versus 0.786 for the white-box probability. If your signal ranks poorly, recalibration cannot help; you need a better signal (§6).
  • The sampling temperature is a black-box knob. Sampling chat at T=2.5T = 2.5 makes the agreement rate almost calibrated without labels (ECE 0.069) and even raises its AUROC to 0.747, because fewer questions have “all 10 samples identical”. But this works only because the simulator’s distortion is a temperature of 2.5. With a real model you don’t know the right temperature, so you are back to choosing it with labeled data.

5.4 A recalibrator is a statement about one distribution

代码 02 演示 3:在 domain A 上拟合,用在 domain B(40% 的事实在训练截止后变了):

ECE on AECE on BAUROC AAUROC B
P(answer), raw0.1900.4210.7830.631
P(answer), T^\hat T fitted on A (2.53)0.0130.2210.7860.633
P(answer), T^\hat T refit on 100 B labels (8.74)0.3050.0780.7830.632
verbalized → Platt on A (a = 0.68)0.0390.2060.7290.599
verbalized → Platt on 100 B labels (a = −0.05)0.2960.0650.2710.401
verbalized → Platt on 300 B labels (a = 0.19)0.2760.0640.7290.599

(准确率:A 0.651,B 0.422。)

  • A 上近乎完美的温度(ECE 0.013)在 B 上给出 0.221。温度缩放后置信度 ≥ 0.8 的 1248 个 B 回答,平均说 0.916,准确率只有 0.560,其中 40% 是过时的事实。
  • 在 B 上重新拟合能把 B 的 ECE 降下来,但 A 的 ECE 变成 0.3:一个校准器只对一个分布成立。
  • 更微妙的是 Platt 在 B 上学到的斜率:几乎是 0(300 条标签时 0.19;另抽 20 份 100 条的样本,中位数 0.18)。在 B 上,“模型说得多笃定”几乎不说明什么,于是校准器输出近似一个常数——它的 ECE 很低,但已经接近 §3.3 里”永远报基础准确率”的”校准但没用”。表里那份 100 条的样本甚至拟合出负的斜率,把排序完全反过来(A 上 AUROC 0.271)。
  • AUROC 在 B 上掉到 0.60–0.63,任何单调的重新校准都修不好:自信地答错的过时事实和自信地答对的题,在模型内部看起来一模一样。这需要新的证据(检索,第 12 讲),不是新的校准。

这和 Ovadia et al.(2019)在图像模型上的系统性发现一致:分布偏移下,校准往往最先坏掉。对选择性问答,Kamath, Jia & Liang(2020)发现只用 softmax 概率决定弃权在领域偏移下效果很差,训练一个用多种特征、并见过一些领域外数据的 calibrator 更好。部署之后,要持续用少量新标签监控校准。


6. 黑盒不确定性 · Black-box uncertainty

当你只能通过 API 拿到文本,置信度只能从说了什么和说了几次里读出来。这一节比较几类方法,代码 03 演示 2 是主表(domain A,2000 道题,K = 10 个样本,T = 1,完美 NLI;随机分数的 AUROC 是 0.5 ± 0.026):

signalbasechat
白盒 white-box(需要 logprobs)
P(exact string)0.6900.636
mean log p(−predictive entropy)0.6640.672
mean log p / L(length-normalized)0.6370.661
−SE(prob-weighted,Kuhn et al. 原始版本)0.7470.718
P(True)0.7480.751
黑盒 black-box(只看文本)
−lexical entropy0.6620.658
−#semantic sets(NumSet)0.6930.710
−SE(discrete)0.7210.718
pairwise agreement(Deg 式)0.7260.717
agreement with greedy(self-consistency)0.7270.711
lexical agreement with greedy0.6370.615
verbalized—0.723
verbalized top-k—0.740
(参照:P(answer),白盒的真实信念)0.7790.779

6.1 Verbalized confidence

The most direct black-box method: ask the model to say its confidence.

  • Lin, Hilton & Evans (2022), “Teaching Models to Express Their Uncertainty in Words”. They fine-tuned GPT-3 to append a “verbalized probability” to its answers on arithmetic problems (CalibratedMath) — “90% confidence”, or words such as “high confidence” — and found that such stated probabilities can be calibrated and stay moderately calibrated under some distribution shift.
  • Tian et al. (2023), “Just Ask for Calibration”. For RLHF models (ChatGPT, GPT-4, Claude) on TriviaQA, SciQ and TruthfulQA, confidences emitted as output tokens are typically better calibrated than the models’ conditional probabilities, often reducing ECE by a relative 50%. Generating several candidates before assigning probabilities (“Provide your k best guesses and the probability that each is correct”) helps — the “How hot is it” prompt of §1.1.
  • Xiong et al. (ICLR 2024) evaluated elicitation methods systematically and found verbalized confidences generally overconfident, concentrated in 80–100%, and mostly multiples of 5 — the way people say “90%” or “95%”.

在模拟器里,这些发现被写成参数(口头置信度 = 基础信念经过偏高的仿射变换 + 噪声,取 5% 的整数倍;top-k 的偏差更小)。所以主表里口头 0.723、top-k 0.740 的差别——paired bootstrap 区间 [−0.001, +0.036],不显著——不是对真实模型的证据,只是在演示怎样公平地比较:同一批题、成对的 bootstrap、和噪声地板比。口头置信度还有两个工程上的问题:数值是离散的(这里只有 15 个不同的值,§8.2 会看到这限制了选择性预测),而且需要用你自己的标签重新校准(§5)。

6.2 Sampling-based: agreement and self-consistency

第二类方法:同一个问题采样 K 次,看答案有多一致。

  • Self-consistency(Wang et al., ICLR 2023)本来是提高准确率的方法:对 chain-of-thought 采样多次、按最终答案投票。投票的得票率天然就是一个置信度。
  • SelfCheckGPT(Manakul et al. 2023)把”和其他样本是否一致”用于检测幻觉——第 12 讲的主题。
  • Lin, Trivedi & Sun(2023, “Generating with Confidence”,TMLR 2024) 是 Fall 2024 的参考文献。它区分了两个概念:uncertainty 是给定输入时可能输出的分散程度(和具体哪个回答无关),confidence 是对某一个特定回答的把握。它在样本之间建一个相似度图(相似度用 Jaccard 或 NLI 的 entailment / contradiction 概率),提出几种度量:语义集合数(NumSet)、度矩阵导出的平均距离(Deg)、图拉普拉斯的特征值之和(EigV)、到中心的距离(Ecc),并用 accuracy–rejection 曲线下面积(AUARC,§8)评估。它的发现之一:相似度函数的选择比度量本身更重要,NLI entailment 最好;簇的个数不如连续的度量。

我们的主表和这一点一致:几个”语义层面”的黑盒信号——离散 SE 0.721 / 0.718、两两一致 0.726 / 0.717、和贪心一致 0.727 / 0.711——彼此差别很小,它们都在估计同一件事:样本有多集中在一个意思上。簇的个数最粗(0.693 / 0.710)。

6.3 Semantic entropy

Mitchell 用一个例子讲 Kuhn, Gal & Farquhar(ICLR 2023)的想法:对 “What is the capital of France?”,三个回答 “Paris”(0.5)、“It’s Paris”(0.4)、“London”(0.1)。

  • 当作三个不同的结果,predictive entropy =−∑sp(s)log⁡p(s)=0.943= -\sum_s p(s)\log p(s) = 0.943;
  • 把同义的合并(Paris 0.9,London 0.1),semantic entropy =0.325= 0.325。

模型其实相当确定答案是 Paris;多出来的 0.6 nats 来自措辞的不确定,而不是答案的不确定。算法三步(代码 03 演示 1 重算了 slide 上的例子):

  1. 对问题采样 M 个回答;
  2. 用一个 NLI 模型(论文用 DeBERTa-large MNLI)按双向蕴含聚类:ss 和 s′s' 同义 ⇔ s⇒s′s \Rightarrow s' 且 s′⇒ss' \Rightarrow s。依次处理每个样本,和每个已有簇的第一个成员比较;
  3. 在簇(意思)上算熵:SE(x)=−∑cp(c∣x)log⁡p(c∣x)\text{SE}(x) = -\sum_c p(c \mid x)\log p(c \mid x),其中 p(c∣x)=∑s∈cp(s∣x)p(c \mid x) = \sum_{s \in c} p(s \mid x)。

(slide 上的第二个例子:7 个样本分成 Paris 0.65、London 0.23、Rome 0.12 三组,SE = 0.872。注意 slide 上的公式 ∑gp(g)ln⁡p(g)\sum_g p(g)\ln p(g) 少了一个负号。)Farquhar, Kossen, Kuhn & Gal(Nature 2024)把它用于检测 “confabulations”(任意的、每次不一样的错误),并给出了只用样本频率的离散版本:不需要 token 概率,黑盒可用。

为什么双向? 单向蕴含会把”更具体”和”更笼统”的说法混为一谈(“巴黎”⇒“法国的一个城市”,反之不成立)。双向的代价是:有些”同一个答案”会被拆开。模拟器里,“只说姓”的回答(“Moren.”)不能推出完整的名字(可能是另一个姓 Moren 的人),所以它和 “Ilsa Moren” 落进不同的簇。练习 3 让你手算这个代价。

主表里的结论,是这一讲里不依赖于模拟器参数的少数比较之一:

  • 离散 SE 比字面熵(按标准化后的字符串聚类)高约 0.06:base +0.059 [+0.043, +0.079],chat +0.060 [+0.041, +0.084]。
  • 白盒的 predictive entropy(0.664 / 0.672)也输给黑盒的离散 SE:它同样把措辞算了进去;长度归一化救不回来。按语义簇加概率(prob-weighted SE)在 base 上最好(0.747)——白盒信息 + 正确的事件。
  • 一个没有预想到的细节:字面熵比语义熵多出的”措辞噪声”(chat 上平均 0.684 nats)在答对的题上更大(0.713 vs 0.630)。模型越确定答案,10 个样本越集中在一个答案上,这个答案的各种说法就越完整地显现出来。所以措辞噪声不只是稀释信号,它和信号反着走。

6.4 White-box relatives: P(True), P(IK), probes

  • P(True)(Kadavath et al. 2022):把模型自己的回答拿给它看,问 “Is the proposed answer True or False?”,读 “True” 的概率。他们发现这在很多任务上效果不错,而且如果先让模型看到自己的多个样本再判断,效果更好。注意它仍然是一个 token 概率,所以 RLHF 的锐化同样作用在它上面。
  • P(IK)(“I know”):训练模型在回答之前预测自己能否答对。它表现不错、部分地跨任务泛化,但在新任务上的校准较弱;当上下文里给出相关材料或提示时,P(IK) 会合理地上升。
  • 隐藏状态探针:CCS(§1.4)、“The Internal State of an LLM Knows When It’s Lying”(Azaria & Mitchell 2023)、ITI(Li et al. 2023)——第 12 讲。

6.5 What it costs

代码 03 演示 3(chat,1500 道题):

K−lexical H−SE(NLI err 0)err 0.1err 0.25agree(err 0)agree(err 0.25)
20.5400.6000.5830.5560.6300.583
30.5940.6450.6090.5780.6640.587
50.6250.6940.6430.6130.6830.588
100.6300.6980.6740.6550.6930.632
200.6570.7310.7150.7000.7250.654
sampling T0.51.01.52.54.0
−SE(discrete)0.6480.7180.7370.7360.685
10 个样本全部相同的比例0.5980.4330.3090.1510.030
  • Samples. SE climbs from 0.600 (K = 2) to 0.731 (K = 20) and is still rising. The cost is K generations plus O(K × #clusters) NLI calls per question. (On the same 1500 questions, K = 10 at T = 1 gives 0.698 in the first table and 0.718 in the second; the two runs differ only in the sampling seed. A 10-sample estimate’s AUROC wobbles by about ±0.02 from sampling alone, which is why the main table reports paired CIs.)
  • The clusterer is the bottleneck. With each NLI direction wrong 25% of the time, SE at K = 10 drops to 0.655, close to lexical entropy (0.630). A bad NLI model eats most of the “semantic” advantage.
  • Temperature matters. At T = 0.5, 60% of questions have ten identical samples; the ties cap the AUROC at 0.648. T = 1.5–2.5 is best here, T = 4 is too noisy. That the optimum sits near 2.5 is again the simulator’s sharpening; for a real model the sampling temperature is a hyperparameter to tune on validation data.

6.6 What consistency cannot see

代码 03 演示 4:同样的方法搬到 domain B。

signalAUROC AAUROC B
agreement with greedy0.7120.605
−SE(discrete)0.7200.611
verbalized0.7320.605
P(True)0.7630.627
(白盒 P(answer))0.640

答错的题里,“10 个样本里至少 9 个和贪心答案一致”的比例:domain A 0.336(696 个错误),domain B 事实没变的题 0.325(440 个),事实已经变了的题 0.590(697 个)。

Every sampling-consistency method rests on one premise: hallucinations are random, different on each sample (Farquhar et al. explicitly restrict their target to “confabulations”). Systematic errors — stale knowledge, misconceptions common in the training data — come out the same every time and look exactly like correct answers. All signals drop together to 0.61–0.64 AUROC, including the white-box true belief: this is not a flaw of any one method; the model simply has no internal information that separates these errors from correct answers. Uncertainty tells you what the model knows, not what the world is like. Such errors need external evidence — retrieval, citations, grounding checks (Lecture 12).


7. Linguistic calibration:用户听到的是什么 · What users hear

数字之外,置信度也可以用措辞表达:“I’m not sure, but…”、“I think…”。Fall 2023 第 1 讲把它列为第二种交付形式:“Rewrite the response with a linguistic measure of calibration”。

  • Mielke, Szlam, Dinan & Boureau(TACL 2022) 研究了开放域聊天机器人:它们口头表达的怀疑或自信,和回答是否正确几乎没有关系(linguistically miscalibrated);但回答正确的概率可以被一个单独的模型较好地预测。他们把这个预测作为控制信号,训练一个可控生成模型,用相应的语气说话,得到的对话模型在措辞上校准得好得多。
  • Zhou et al.(ACL 2024)(§4.3):部署的 LM 很少主动表达不确定;在用户实验里,用户对模型输出的依赖程度很高,不论回答有没有被标上确定性。
  • Band, Li, Ma & Hashimoto(ICML 2024) 把这个问题推广到长文本:一段长回答是”语言上校准的”,如果读者读完之后能对相关问题做出校准的预测;他们用基于决策的目标训练模型。

代码 04 演示 3 用一个模拟用户(我们的假设):对直接陈述 “X.” 以 0.95 的概率相信,“I think it’s X.” 0.75,“I’m not sure, but maybe X.” 0.35,“I don’t know.” 不相信。按某个置信度选措辞(≥ 0.9 直接说,≥ 0.6 “I think”,≥ 0.3 “not sure”,否则 “I don’t know”)。chat,domain A,3000 道题:

policyplain:share / accI thinknot sureIDKling. ECEtrusted wrong / 100 Qsmissed right / 100 Qs
always assertive1.000 / 0.63——0.0000.31634.83.2
hedge by raw verbalized0.360 / 0.830.572 / 0.540.067 / 0.350.0010.16126.910.8
hedge by Platt(verbalized)0.056 / 0.980.584 / 0.710.350 / 0.450.0090.05919.321.3
hedge by true p⋆p^\star(oracle)0.184 / 0.950.333 / 0.750.451 / 0.450.0320.04715.821.0

(ling. ECE = 各措辞的实际准确率和用户以为的可靠度之差,按份额加权。)

  • **“永远笃定”**是 RLHF 聊天模型常见的风格:用户以为”直接说”意味着 95% 可靠,实际只有 63%,每 100 题有 34.8 个错误答案被当真。
  • 按原始口头置信度选措辞好一些,但措辞本身也过度自信了:“I think” 只有 54% 对。
  • 按校准后的概率选措辞,ling. ECE 降到 0.059,被当真的错误降到 19.3,接近 oracle(15.8)。这就是 Mielke et al. 的结构:先有一个校准的正确率预测器,再让措辞服从它。
  • 代价:用户错过的正确答案从 3.2 增加到约 21——被”我不确定”修饰的正确答案也会被打折扣。oracle 也一样(21.0):模型确实不确定。Linguistic calibration 的目标不是让用户少错过,而是让信任跟着可靠性走。用户怎样理解 “I think” 因人而异,0.95 / 0.75 / 0.35 只是我们的假设;真实的数字要做用户研究来测。

8. Selective prediction 与弃权 · Selective prediction & abstention

8.1 Setup

A selective predictor is a pair (f,g)(f, g): ff answers, and g(x)∈{answer,abstain}g(x) \in \{\text{answer}, \text{abstain}\} decides whether to. With a confidence cc and a threshold λ\lambda, g(x)=1{c(x)≥λ}g(x) = 1\{c(x) \ge \lambda\}. Two numbers describe it:

coverage(λ)=P(c(x)≥λ),selective risk(λ)=P(Y=0∣c(x)≥λ).\text{coverage}(\lambda) = P\big(c(x) \ge \lambda\big), \qquad \text{selective risk}(\lambda) = P\big(Y = 0 \mid c(x) \ge \lambda\big).

Sweeping λ\lambda traces the risk–coverage curve (Chow 1970 for the reject option; Geifman & El-Yaniv 2017 for deep networks). Its area, AURC, summarizes a confidence signal; E-AURC subtracts the AURC of the optimal ordering (all correct answers first), which depends only on accuracy (Geifman, Uziel & El-Yaniv 2019). Lin et al.’s AUARC (§6.2) is the same idea with accuracy instead of risk.

This is the third time the course meets “decide what you can, abstain on the rest”: 第 1 讲 §7.3 对 ∣z∣≤ϵ∥θ∥1|z| \le \epsilon\lVert\theta\rVert_1 的申请人弃权,换来可证明的鲁棒性;第 8 讲 §7 的 randomized smoothing 在 pA‾<1/2\underline{p_A} < 1/2 时弃权。这里弃权的理由不是”可能被攻击”,而是”可能答错”。在 agent 里,它会变成”什么时候停下来问用户”(第 14 讲)。

8.2 Ranking decides how good, calibration decides where

代码 04 演示 1(chat,domain A,3000 道题,准确率 0.664;最优 AURC = 0.0642):

signalrisk@100%@80%@60%@40%@20%AURCE-AURC#values
P(answer)(白盒)0.3360.2740.2010.1160.0580.16350.09933000
P(True)0.3360.2820.2210.1330.0600.17430.11013000
verbalized0.3360.2920.2360.1670.0760.19170.127515
verbalized → Platt0.3360.2920.2360.1670.0760.19170.127515
verbalized top-k0.3360.2880.2130.1450.0750.17980.115617
agreement(K = 10)0.3360.2740.2080.1760.1760.21670.152511
−semantic entropy(K = 10)0.3360.2740.2080.1760.1760.21640.152328
−lexical entropy(K = 10)0.3360.2950.2670.2530.2340.26540.201241
random0.3360.3360.3360.3360.3360.33600.27181
oracle0.3360.1700.0000.0000.0000.06420.00002
  • Monotone recalibration changes nothing here: verbalized before and after Platt are identical in every column. Selective prediction only needs the ranking; calibration matters for choosing λ (§8.3).
  • Ties are a hidden cost of discrete scores. Ten-sample agreement has 11 possible values, and more than 40% of questions have all ten samples agreeing (43% in 03 demo 3). They are tied for first place, so the risk at 40% and at 20% coverage is the same 0.176 — you cannot answer “only the most certain 20%” because nothing distinguishes them. The code breaks ties in expectation so that no discrete score benefits from an arbitrary order. Continuous scores (P(answer), P(True)) reach risk 0.058–0.060 at 20% coverage. For a low-risk, low-coverage operating point you need more samples or a continuous score.
  • Lexical entropy is the worst signal (AURC 0.2654): §6.3’s phrasing noise turns directly into more wrong answers.

8.3 Picking the threshold

目标:“回答的部分错误率 ≤ 0.15”。代码 04 演示 2:

rulecoverage Arisk Acoverage Brisk B
verbalized ≥ 0.90(直接相信口头数字)0.3860.1560.4000.478
Platt(verbalized) ≥ 0.85(每个回答的风险 ≤ 0.15)0.0570.0350.0530.387
plug-in:在 1000 条校准集上挑的阈值 0.950.2280.0970.2190.464
  • 直接相信”90%“在 A 上碰巧接近目标;在 B 上风险 0.478,三倍于目标。
  • “校准后的概率 ≥ 0.85”控制的是每一个回答的风险,平均风险远低于目标(0.035),代价是只回答 5.7%。控制”每个回答”和控制”回答的平均”是两个不同的目标。
  • Plug-in(在校准集上挑满足约束的、覆盖率最大的阈值)没有保证。反复抽校准集,测试风险超过 0.15 的频率:
n_calmean riskP(risk > 0.15)95% quantilemean coverage
1000.1190.4770.1970.291
3000.1160.4470.1560.280
10000.1180.4000.1560.285

我原以为校准集越大,超标越少。实际一直在 40%–48%:口头置信度只有十几个可选阈值,”≥ 0.90”这一档的真实风险(约 0.156)正好紧挨着 0.15,校准集上估计得偏低一点就会选它。超标幅度不大(95% 分位 0.156–0.197),但”挑一个刚好满足约束的阈值”正是第 1 讲 §8.2 的赢家诅咒。§9.6 用一个小小的修正项换来有限样本的保证。


9. Conformal prediction

9.1 The idea and the guarantee

前面所有方法都要求置信度”有含义”,而这只在校准集的分布上近似成立。Conformal prediction(Vovk, Gammerman & Shafer 2005;入门见 Angelopoulos & Bates 的 “A Gentle Introduction”)换了一个问题:不要求校准,只要求可交换。

Split conformal. Pick any nonconformity score s(x,a)s(x, a) (large = ”aa looks wrong for xx”), e.g. s=1−p^(a∣x)s = 1 - \hat p(a \mid x). On nn labeled calibration examples compute si=s(xi,ai⋆)s_i = s(x_i, a^\star_i). Let q^\hat q be the kk-th smallest, k=⌈(n+1)(1−α)⌉k = \lceil (n+1)(1-\alpha) \rceil. Output

C(x)={a:s(x,a)≤q^}.C(x) = \{a : s(x, a) \le \hat q\}.

Theorem. If the calibration examples and the test example are exchangeable, then P(a⋆∈C(x))≥1−αP\big(a^\star \in C(x)\big) \ge 1 - \alpha (and ≤1−α+1n+1\le 1 - \alpha + \frac{1}{n+1} when scores have no ties).

Why. By exchangeability, the test score sn+1s_{n+1} is equally likely to occupy any of the n+1n + 1 ranks among s1,…,sn+1s_1, \dots, s_{n+1}. So P(sn+1≤the k-th smallest of s1..sn)≥k/(n+1)≥1−αP(s_{n+1} \le \text{the } k\text{-th smallest of } s_1..s_n) \ge k/(n+1) \ge 1 - \alpha. Nothing was assumed about the model, the score, or the distribution — only that the n+1n+1 examples are exchangeable.

Two things the guarantee does not say:

  1. It averages over the calibration draw. For a fixed calibration set, coverage is a random variable distributed as Beta(k, n+1−k)\text{Beta}(k,\ n + 1 - k).
  2. It averages over test points. It is marginal, not conditional on xx or on any subgroup (§9.4).

9.2 It works even with overconfident scores

代码 05 演示 1:5 个候选答案(选择题格式,Kumar et al. 2023 在 MCQA 上做过同样的事),α = 0.1,测试集 5000 道题,500 次随机抽校准集:

scores fromn_calcoveragesd(emp)sd(Beta 理论)5% quantileset sizesingleton
chat(过度自信)1000.8970.0300.0300.8412.330.404
chat5000.8980.0130.0140.8762.300.405
chat20000.8970.0060.0080.8882.290.406
base(校准)1000.8990.0310.0300.8432.350.341
base5000.8980.0140.0140.8752.310.344
base20000.8960.0070.0080.8832.290.346

对照:不做 conformal,直接相信模型的概率、按概率从大到小加入候选直到累计 ≥ 0.9——chat 只覆盖 0.814(集合 1.65),base 覆盖 0.941(集合 2.84,离散的候选总会越过 0.9 一点)。

  • Coverage is ≈ 0.9 in every row, including the overconfident chat scores. A bad score costs you larger sets, not coverage. Trusting chat’s own probabilities instead under-covers (0.814).
  • The spread matches the Beta prediction: with n = 100, 5% of calibration sets give coverage around 0.84. A guarantee “in expectation” is not a guarantee for your calibration set; larger n tightens it. (All rows sit about 0.003 below 0.9 because the 5000-question test set is fixed; its own sampling error is about ±0.004.)

9.3 Open-ended generation

选择题有固定的候选列表;开放式回答没有。最直接的做法是用采样得到的答案(语义簇)当候选,分数 = 1 − 该簇的频率。但真答案没被采到时,它的分数是 +∞,任何有限的阈值都覆盖不了它。代码 05 演示 2(n_cal = 1000):

KTP(truth sampled)α = 0.3:q̂ / cov / sizeα = 0.2α = 0.1
51.00.7590.60 / 0.667 / 1.15∞(整个答案空间)∞
201.00.8590.70 / 0.686 / 1.180.90 / 0.801 / 1.67∞
52.50.8320.80 / 0.832 / 2.240.80 / 0.832 / 2.24∞
202.50.9570.70 / 0.692 / 1.230.80 / 0.797 / 1.740.90 / 0.908 / 2.65

能达到的覆盖率上限是”真答案至少被采到一次”的概率。当 α 小于漏采率时,conformal 诚实地返回 ∞:在这个采样预算下,唯一有保证的集合是”所有可能的回答”。要 α = 0.1,这里需要 K = 20 且 T = 2.5。分数只有 K + 1 个可能值,所以覆盖率会跳过目标(K = 5、T = 2.5、α = 0.3 时 0.832)。

两篇论文给出了更好的答案(细节以论文为准):

  • Quach et al.(ICLR 2024, “Conformal Language Modeling”):不断采样加入候选集合,用一个经过校准的停止规则决定何时停止、一个经过校准的拒绝规则去掉低质量的候选;保证集合里至少有一个可接受的回答的概率很高,同时集合尽量小。它还能在回答内部挑出各自独立正确的句子或短语。
  • Mohri & Hashimoto(ICML 2024, “Language Models with Conformal Factuality Guarantees”):把”一个回答是否正确”看成一个不确定性量化问题,不确定性集合是回答的蕴含集合;conformal 对应一个 back-off 算法:逐步让回答变得不那么具体(删掉没把握的子论断),直到正确性保证成立。在 FActScore、NaturalQuestions 和 MATH 上,它能给出 80–90% 的正确性保证,同时保留大部分原始内容。

9.4 Marginal, not conditional

代码 05 演示 3:校准集和测试集都来自 domain B 的混合分布(可交换性成立),α = 0.1,n_cal = 1000,200 次重复:

slicen(测试)marginal:cov / sizeMondrian by tier:cov / size
head17660.777 / 2.840.900 / 4.05
torso24280.948 / 4.440.917 / 4.15
tail18060.984 / 4.840.887 / 4.06
fact unchanged36260.993 / 4.110.988 / 4.11
fact changed23740.779 / 4.060.772 / 4.06
all60000.909 / 4.090.903 / 4.09
  • Overall coverage is 0.909: the guarantee holds. But head (popular) entities get only 0.777 while tail entities get 0.984. In domain B, changed facts hurt most where the model is most confident — popular entities — and the model returns a small, wrong set. A marginal guarantee allows a subgroup to be systematically under-covered: Lecture 1 §5.2’s “averages hide subgroups”, once more.
  • Mondrian conformal (a separate quantile per slice) brings all three tiers back to 0.89–0.92, at the price of larger head sets (2.84 → 4.05). It requires knowing the slice at test time (entity popularity can be estimated from corpus frequency).
  • The real problem slice is “fact changed” (0.779 / 0.772). It is invisible at test time, so no method can condition on it. You can only guarantee coverage conditional on observable features; coverage conditional on every xx is impossible in finite samples without distributional assumptions (Vovk 2012; Foygel Barber, Candès, Ramdas & Tibshirani 2021).

(一个计划外的结果:我原想在 domain A 上展示冷门实体欠覆盖,但 A 上各层都接近 0.9——head 0.907、torso 0.892、tail 0.908——因为 1−p^1 - \hat p 这个分数会自适应,冷门的题自动得到更大的集合。见代码 README。)

9.5 Exchangeability broken

代码 05 演示 4:在 A 上校准(q^=0.9885\hat q = 0.9885),在 B 上用:

test oncoverageset size
A0.9072.37
B(all)0.6942.41
B(fact unchanged)0.9062.42
B(fact changed)0.3802.40

保证是关于”和校准集来自同一分布的测试样本”的,不是关于模型的。在 B 上重新校准需要 q^=0.9999\hat q = 0.9999(几乎把所有候选放进来,§9.4 里平均 4.09 个)。处理漂移的变体(例如 Gibbs & Candès 2021 的 adaptive conformal inference)在线调整阈值,但需要持续拿到标签。

9.6 Conformal abstention: risk control

Instead of sets, control a risk. Conformal risk control (Angelopoulos, Bates, Fisch, Lei & Schuster, ICLR 2024) handles any loss Li(λ)∈[0,B]L_i(\lambda) \in [0, B] that is monotone in λ\lambda. For abstention, take

Li(λ)=1{c(xi)≥λ and Yi=0}(answered and wrong),L_i(\lambda) = 1\{c(x_i) \ge \lambda \ \text{and}\ Y_i = 0\} \quad \text{(answered and wrong)},

which can only decrease as λ\lambda grows. Choose the most aggressive λ\lambda with nn+1R^n(λ)+Bn+1≤α\frac{n}{n+1}\hat R_n(\lambda) + \frac{B}{n+1} \le \alpha; then E[Ln+1(λ^)]≤α\mathbb E[L_{n+1}(\hat\lambda)] \le \alpha. Note the denominator is all questions: this controls how often a user receives a wrong answer, not the selective risk of §8 (which is not monotone in λ and needs Learn-then-Test, Angelopoulos et al. 2021).

代码 05 演示 5(口头置信度,黑盒;准确率 A 0.651、B 0.421):

ruleanswer rate AP(wrong answer) Aanswer rate BP(wrong answer) B
verbalized ≥ 0.90(固定规则)0.3870.0590.3850.190
CRC α = 0.10,500 条 A:v ≥ 0.900.3870.0590.3850.190
CRC α = 0.05,500 条 A:v ≥ 0.950.2320.0200.2230.105
αn_calmean P(wrong answer)P(> α)mean answer rate
0.10100 / 500 / 20000.079 / 0.074 / 0.0620.350 / 0.270 / 0.0570.437 / 0.427 / 0.395
0.05100 / 500 / 20000.034 / 0.029 / 0.0200.333 / 0.217 / 0.0100.283 / 0.265 / 0.233
  • α = 0.10 时 CRC 恰好选中固定规则的 0.90(我在看到这个结果之后加了 α = 0.05 这一行)。α = 0.05 时两者分开:固定规则 0.059 超标,CRC 选 0.95、给出 0.020。固定规则偶尔”碰巧”满足目标,但没有任何东西保证它。
  • 保证是期望意义上的:平均风险都在 α 以下,但 n = 100 时单次超标的频率有约三分之一。想要”以高概率不超标”,需要 Learn-then-Test 这类更保守的版本。离散的口头置信度让阈值只能跳着选,实际风险常常远低于 α:保证成立,但浪费了回答率。
  • 搬到 B:α = 0.05 的阈值给出 0.105,两倍于承诺。和 §5.4、§9.5 一样,任何基于校准集的承诺都不跨分布。

10. 从测量到改进:训练模型更符合事实 · From measuring to improving

10.1 Mitchell’s argument

The second half of Mitchell’s lecture asked: if LLMs seem to know something about truth (§1.4), how do we make them say only the true parts? His picture: pre-training (~a trillion words) learns what is true and false; RLHF (~a billion words) learns to say only the true stuff. But standard RLHF does not deliver this. It encourages whatever makes labelers happy, and deciding “is this response factually accurate” is much harder than deciding “do I like this response” — so human labels only weakly encourage truth. And most results so far are on short QA; long responses must first be decomposed into atomic claims, each judged separately.

10.2 Fine-tuning for factuality (Tian, Mitchell et al. 2023)

Tian, Mitchell, Yao, Manning & Finn(“Fine-tuning Language Models for Factuality”,ICLR 2024)的方法:

  1. 对每个 prompt 采样多个长回答;
  2. 自动给每个回答的事实性打分,两种方式:
    • reference-based:FActScore(Min et al. 2023)——抽取原子论断,和参考资料(如 Wikipedia)比对;
    • reference-free:用模型自己的置信度——把每个论断改写成一个问题,让模型重新回答多次,看答案有多一致(本讲 §6.2 的采样一致性),再在论断上平均;
  3. 按分数构造偏好对,用 DPO 训练(Mitchell 选 DPO 的理由:“fast, stable, and effective”)。

在 7B 模型上(传记生成和医学问答),reference-based 版本相比 Llama-2-chat 把事实错误率降低了 58%(传记)和 40%(医学问答);reference-free 版本也有改进,但小一些;factuality tuning 还能和 DoLa(Chuang et al. 2024)这类解码时方法叠加。slide 上的结论:“Only factuality tuning (using the reference-based factuality ranking) strictly improves over supervised fine-tuning.”

10.3 How good are the rankings? (simulation)

代码 03 演示 5 只检查第一步(打分和排序),没有做 DPO:每个 prompt 是 6 个原子事实的”传记”,采样 4 个回答(chat,T = 1),250 个 prompt。

domainscorerpair accbest-of-4 precision
Areference-based(FactScore 式,判断有 10% 错误)0.7900.667
Areference-free(每个论断重采样 10 次的一致率)0.5880.626
Averbalized confidence(平均)0.5020.614
Arandom0.4730.615
Aoracle(真实精度)1.0000.706
Breference-based0.7750.447
Breference-free0.5700.408
Bverbalized0.5760.418
Brandom / oracle0.488 / 1.0000.408 / 0.483

(734 / 682 个真实精度不同的回答对;pair acc 的抽样标准差约 0.018–0.019。平均回答的精度:A 0.615,B 0.404。)

  • Reference-based 打分的偏好对 79% 正确;reference-free 只有 59%。同一个 prompt 的几个回答,只在模型本来就犹豫的论断上有差别,而在那里候选答案的概率往往差不多(比如 0.35 vs 0.25)——自我一致性能给出的信号本来就弱。这和论文里”reference-free 也有效但较弱”的方向一致。
  • 我原以为 reference-free 打分在 B 上会崩溃,实际只是略降(0.588 → 0.570)。原因:B 里 55.3% 的错误论断在 4 个回答里是同一个错误答案(事实已变的论断里 65.5%;A 里是 40.2%)。这些错误在任何偏好对里都不出现差别——不会被排错,也永远不会被修正。从模型自己的样本构造偏好数据,只能教它在”已经犹豫的地方”选更好的一边;它一致相信的错误,需要外部参考,或者让它学会在这类问题上弃权。

10.4 Measure again after you optimize

把一个置信度信号当作训练目标,它就会受到 Goodhart’s law 的压力(第 1 讲 §8.3):用”一致性”做奖励,最省力的提升方式之一是让模型变得更一致,而不一定是更正确——一致的错误恰恰是一致性看不见的(§6.6)。所以 factuality tuning 之后要用独立的参考重新测量事实性,并重新检查置信度的校准:训练改变了模型,第 5 节的校准器也就过期了。


11. 小结 · Summary

问题工具主要陷阱
置信度说的是哪个事件?答案层面的概率;选择题格式;按语义求和字符串概率被措辞分走;长度归一化只治症状
校准得好不好?reliability table、ECE(+ 噪声地板)、Brier、NLLECE 在小 n 上偏高;“永远报基础准确率”ECE = 0
怎样修?temperature(白盒)、Platt、histogram binning修不了排序;分布一变就失效;小样本会拟合出平的甚至反向的映射
黑盒怎么办?verbalized / top-k、采样一致性、semantic entropy、P(True)措辞噪声;NLI 质量;样本数和温度;一致的错误
怎样行动?风险–覆盖率、AURC;措辞的选择平局限制工作点;plug-in 阈值没有保证
有没有保证?split conformal、Mondrian、conformal risk control只是边际的、期望意义的;依赖可交换性;开放式生成受采样限制
怎样改进?factuality 偏好 + DPO自我一致性修不了一致的错误;优化后要重新测量

Six takeaways:

  1. Ask what event a probability is about before asking whether it is calibrated. For LLMs, the cheapest number (the string probability) answers the wrong question.
  2. ECE is an estimator with a noise floor. Report n, the binning, and the floor; pair ECE with a proper score or AUROC, because a useless constant predictor is perfectly calibrated.
  3. RLHF tends to sharpen token probabilities without destroying the ranking. A temperature or Platt map fixes the meaning on one distribution — and only there.
  4. Meaning, not wording. Uncertainty should be measured over meanings (semantic entropy, agreement under an NLI equivalence); lexical measures mix in phrasing noise that even runs against the signal.
  5. Uncertainty measures what the model knows, not what is true. Every consistency- or confidence-based method is blind to consistent errors (stale facts, shared misconceptions); those need external evidence.
  6. Guarantees are about distributions, not models. Conformal coverage and risk control hold marginally, in expectation, under exchangeability. They are the right tool for making promises — as long as you state exactly what is promised, to whom, and on which data.

12. 常见误区 · Common pitfalls

  • “The model gave this answer probability 0.3, so it’s probably wrong.” That may be the probability of the string. Other phrasings of the same answer may carry the rest (§2.2).
  • “Our ECE on 300 questions is 0.06, so the model is well calibrated (or: miscalibrated).” At n = 300 with 15 bins, a perfectly calibrated model averages 0.064 and exceeds 0.089 one time in twenty. Compare with the noise floor (§3.2).
  • “We used adaptive bins, so our ECE estimate is unbiased.” Equal-mass bins trade one bias for another; for well-calibrated scores they can have more upward bias (§3.2).
  • “After temperature scaling the model is calibrated.” On the calibration distribution. On domain B the same T left an ECE of 0.221 — worse than the uncalibrated model had been on A (0.190) (§5.4).
  • “Recalibration improved our confidence signal.” It improved its meaning. AUROC, AURC and risk at a given coverage are unchanged by monotone maps (§5.3, §8.2).
  • “The model gave the same answer 10 times out of 10, so it’s right.” Consistency catches random confabulations, not consistent errors (§6.6).
  • “Semantic entropy is just entropy.” Its quality is bounded by the equivalence judge; with a noisy NLI it falls to lexical-entropy level (§6.5).
  • “Conformal prediction guarantees 90% coverage for every user.” It guarantees 90% on average over exchangeable data. Subgroups can sit at 78% (§9.4), and shift breaks it (§9.5).
  • “We’ll just tell the model to say ‘I’m not sure’ when it is unsure.” The phrase must be tied to a calibrated estimate, and users must interpret it the way you assume (§7).

13. 练习 · Exercises

练习 1(ECE 的噪声地板). 一个完美校准的分数,用 BB 个等量箱、总样本 nn。(a) 设每个箱的真实准确率为 pbp_b,用正态近似 E∣Xˉ−p∣≈2/πp(1−p)/m\mathbb E|\bar X - p| \approx \sqrt{2/\pi}\sqrt{p(1-p)/m}(mm 个 Bernoulli 的平均)推导 E[ECE]\mathbb E[\text{ECE}] 的近似式。(b) 代码里的 base 置信度满足 E[p(1−p)]≈0.18\mathbb E[p(1-p)] \approx 0.18。估计 B=15B = 15、n=100n = 100 和 n=3000n = 3000 时的 ECE,和 §3.2 表里 m-15 那一列比较。(c) 用 Cauchy–Schwarz 说明:在总数和箱数固定时,为什么各箱样本数相等时这个偏差最大。

参考答案

(a) E[ECE]≈∑bnbn2πpb(1−pb)nb=2π1n∑bnb pb(1−pb)\mathbb E[\text{ECE}] \approx \sum_b \frac{n_b}{n}\sqrt{\frac{2}{\pi}}\sqrt{\frac{p_b(1-p_b)}{n_b}} = \sqrt{\frac{2}{\pi}}\frac{1}{n}\sum_b\sqrt{n_b\,p_b(1-p_b)}。等量箱 nb=n/Bn_b = n/B,再把 pb(1−pb)p_b(1-p_b) 换成平均值 vˉ\bar v:≈2πBvˉn\approx\sqrt{\frac{2}{\pi}}\sqrt{\frac{B\bar v}{n}}。(b) n=100n = 100:0.798×15×0.18/100=0.798×0.164≈0.1310.798\times\sqrt{15\times0.18/100} = 0.798\times0.164 \approx 0.131(表中 0.126);n=3000n = 3000:0.798×0.030≈0.0240.798\times0.030 \approx 0.024(表中 0.024)。小 nn 时每箱只有约 7 个样本,正态近似略有偏差。(c) ∑bnb≤B∑bnb=Bn\sum_b\sqrt{n_b} \le \sqrt{B}\sqrt{\sum_b n_b} = \sqrt{Bn},等号当且仅当所有 nbn_b 相等。等宽箱让样本分布不均(有的箱很空),∑nb\sum\sqrt{n_b} 更小,所以完美校准时的噪声偏差反而更小——这就是 §3.2 那个”和预想相反”的结果。代价是空箱里的准确率估计没有意义。

练习 2(proper 与 improper). Let Y∼Bernoulli(p)Y \sim \text{Bernoulli}(p) and a reported q∈[0,1]q \in [0, 1]. (a) Show that the expected Brier score and the expected log score are both minimized at q=pq = p. (b) A “confidence-weighted accuracy” reward pays qq if the answer is correct and 0 otherwise. What qq maximizes its expectation? (c) A reward of 1{correct}1\{\text{correct}\} ignores qq entirely. Relate (b) and (c) to §4.3: what kind of training signal would keep an RLHF model calibrated?

参考答案

(a) Brier:ddq[p(1−q)2+(1−p)q2]=2(q−p)\frac{d}{dq}[p(1-q)^2 + (1-p)q^2] = 2(q - p);log:ddq[−plog⁡q−(1−p)log⁡(1−q)]=−pq+1−p1−q\frac{d}{dq}[-p\log q - (1-p)\log(1-q)] = -\frac{p}{q} + \frac{1-p}{1-q}。两者都在 q=pq = p 处为 0,二阶导数为正。(b) 期望 =pq= pq,对 qq 单调增,最优是 q=1q = 1(只要 p>0p > 0):这个奖励奖励过度自信,是 improper 的线性评分规则。(c) 只看对错的奖励对 qq 没有任何压力;如果偏好数据还偏爱听起来笃定的回答(Zhou et al. 2024),压力就和 (b) 一样指向过度自信。要保持校准,奖励里需要一个关于置信度的 proper scoring rule,例如对模型说出的置信度按 Brier 或 log score 计分(已有工作沿这个方向用 RL 训练口头置信度)。

练习 3(手算语义熵). 10 个样本:“Ilsa Moren” ×4、“It was Ilsa Moren.” ×2、“Moren.” ×1、“Kaem Delor” ×3。(a) 按标准化后的字符串聚类,算离散的字面熵。(b) 按双向蕴含聚类(“Moren.” 不能推出 “Ilsa Moren”),算离散语义熵。(c) 如果把 “Moren.” 也算进 Ilsa Moren 的簇,语义熵是多少?(d) 这说明双向蕴含有什么代价?在什么应用里你宁可接受这个代价?

参考答案

(a) 4 个字符串簇,频率 0.4、0.2、0.1、0.3:H=−(0.4ln⁡0.4+0.2ln⁡0.2+0.1ln⁡0.1+0.3ln⁡0.3)=0.367+0.322+0.230+0.361=1.280H = -(0.4\ln0.4 + 0.2\ln0.2 + 0.1\ln0.1 + 0.3\ln0.3) = 0.367 + 0.322 + 0.230 + 0.361 = 1.280。(b) 3 个语义簇 {Ilsa Moren ×6}、{Moren ×1}、{Kaem Delor ×3}:−(0.6ln⁡0.6+0.1ln⁡0.1+0.3ln⁡0.3)=0.306+0.230+0.361=0.898-(0.6\ln0.6 + 0.1\ln0.1 + 0.3\ln0.3) = 0.306 + 0.230 + 0.361 = 0.898。(c) 两个簇 0.7、0.3:0.250+0.361=0.6110.250 + 0.361 = 0.611。(d) 双向蕴含把”更笼统的同一个答案”拆成新簇,让不确定性偏高。好处是不会把”更笼统”的说法当成”更确定”:如果一个回答只说 “Moren”,在有两个姓 Moren 的人的语境里它确实信息更少。对需要精确实体的应用(医疗、法律引用),宁可接受偏高的不确定性。

练习 4(温度缩放). (a) 对两个候选的分布,证明把 logits 乘以 β\beta 等价于 q=σ(β logit p)q = \sigma(\beta\,\text{logit}\,p),并由此说明为什么模拟器里 T^≈2.5\hat T \approx 2.5、Platt 的斜率 a≈0.42a \approx 0.42。(b) 证明温度缩放不改变 argmax。(c) 对多于两个候选的情形,温度缩放可能改变不同问题之间的置信度排序。验证:问题 1 的 logits 是 (2.0,1.9,−5,−5,−5)(2.0, 1.9, -5, -5, -5),问题 2 是 (1.0,−1,−1,−1,−1)(1.0, -1, -1, -1, -1),分别在 T=1T = 1 和 T=2.5T = 2.5 下算 top-1 概率。这对 §5 “重新校准不改变 AUROC” 的说法意味着什么?

参考答案

(a) p=σ(z1−z2)p = \sigma(z_1 - z_2),乘以 β\beta 后 q=σ(β(z1−z2))=σ(β logit p)q = \sigma(\beta(z_1 - z_2)) = \sigma(\beta\,\text{logit}\,p)。chat 的 logits 是 base 的 2.5 倍,所以 T=2.5T = 2.5 恰好还原;在 top-1 概率的 logit 上做 Platt,斜率约 1/2.5=0.41/2.5 = 0.4(多类时只是近似,得到 0.42)。(b) z/Tz/T 对每个分量乘同一个正数,顺序不变。(c) T=1T = 1:问题 1 是 0.524,问题 2 是 0.649(问题 2 更自信);T=2.5T = 2.5:问题 1 是 0.467,问题 2 是 0.357(问题 1 更自信)。排序反了:问题 1 有两个势均力敌的候选,其余很低,压平之后另外三个的分量增加得少;问题 2 一个突出、四个并列,压平之后四个并列的一起涨。所以多类温度缩放对跨题的排序不是严格单调的,AUROC 可能略变(代码 02:0.786 → 0.786,几乎不变);对单一标量做的 Platt、histogram 才严格保持(或只并列)排序。

练习 5(conformal 的数字). (a) n=100n = 100、α=0.1\alpha = 0.1:kk 是多少?用 Beta(k,n+1−k)\text{Beta}(k, n+1-k) 算覆盖率的均值和标准差,和 §9.2 的表比较。(b) 证明(或至少解释)覆盖率的上界 1−α+1n+11 - \alpha + \frac{1}{n+1}。(c) 开放式回答里,若 P(真答案至少被采到一次)=0.759P(\text{真答案至少被采到一次}) = 0.759,大约能做到的最小 α 是多少?为什么 Quach et al. 要把”何时停止采样”也做成需要校准的规则?

参考答案

(a) k=⌈101×0.9⌉=⌈90.9⌉=91k = \lceil 101\times0.9\rceil = \lceil 90.9\rceil = 91;均值 91/101=0.90191/101 = 0.901,标准差 91×101012×102≈0.030\sqrt{\frac{91\times10}{101^2\times102}} \approx 0.030。表中经验值 0.897–0.899、0.030–0.031(固定测试集让均值整体低约 0.003)。(b) 没有平局时,测试分数的秩在 1,…,n+11, \dots, n+1 上均匀,覆盖 ⇔ 秩 ≤ kk,概率恰好是 k/(n+1)<(n+1)(1−α)/(n+1)+1/(n+1)k/(n+1) < (n+1)(1-\alpha)/(n+1) + 1/(n+1)。(c) 约 1−0.759=0.2411 - 0.759 = 0.241(再加上有限 n 的修正):漏采的题的分数是 ∞,能覆盖的比例不超过 0.759。固定 K 时,难题需要更多样本才能采到真答案,简单题浪费样本;Quach et al. 让采样持续到一个校准过的停止条件满足为止,使”集合里至少有一个可接受的回答”以给定概率成立,同时控制样本数和集合大小。

练习 6(设计一个会弃权的医疗问答助手). 一个基于聊天 API(只返回文本)的医疗问答助手,要回答”药物 A 和药物 B 能否同时服用”一类问题。写一份一页的设计:(a) 用什么置信度信号(考虑黑盒限制和 §6.6 的盲区);(b) 需要什么标签数据、怎样做重新校准;(c) 用什么规则决定回答 / 弃权 / 转药剂师,承诺什么(每个回答的风险?用户收到错误答案的频率?),用什么方法兑现;(d) 怎样用措辞向用户表达;(e) 部署后怎样监控分布偏移(例如新药上市)。

参考答案(一个示例)

(a) 主信号:K = 10–20 个样本的离散语义熵 / 一致率(NLI 判断”能 / 不能同时服用 + 理由”是否同义),辅以 top-k 口头置信度;因为一致性看不见”一致的错误”(例如训练截止后更新的说明书),每个回答还要做检索 grounding(第 12 讲),检索不到依据时直接降级。(b) 由药剂师标注的几百个问题(按药物类别分层),用 Platt 或 histogram binning 把信号映射成答对概率;按类别报告 ECE 和噪声地板。(c) 承诺”用户收到错误答案的频率 ≤ 2%“:在标注集上用 conformal risk control 选阈值(损失 = 回答且错误),并报告这只是期望意义、在可交换数据上的保证;对高风险类别(抗凝药等)用 Mondrian 方式单独校准,或者直接转人工。(d) 只有校准概率 ≥ 0.9 且有检索依据时直接陈述;0.6–0.9 用”根据说明书,通常……,但请向药剂师确认”;更低则转药剂师;措辞的含义要做小规模用户研究。(e) 每周抽样人工复核,监控各类别的回答率、ECE 和 CRC 风险;新药或说明书更新时视为分布偏移,重新校准;记录 model card(第 1 讲 §8.4)。

代码相关的练习(“动手改一改”)见 code/lecture13/README.md。


14. 延伸阅读 · Further reading

本讲的主要来源

  • Eric Mitchell, “Towards More Truthful LLMs”, CS329T Fall 2023 Lecture 10 slides(2023-10-26);CS329T Fall 2023 Lecture 1、Lecture 6 和 Fall 2024 Lecture 3、“Grounding and Factuality 1” slides。
  • Kadavath et al., “Language Models (Mostly) Know What They Know”, 2022(P(True)、P(IK)、RLHF 与温度 2.5)。
  • Tian, Mitchell, Zhou, Sharma, Rafailov, Yao, Finn & Manning, “Just Ask for Calibration: Strategies for Eliciting Calibrated Confidence Scores from Language Models Fine-Tuned with Human Feedback”, EMNLP 2023。
  • Kuhn, Gal & Farquhar, “Semantic Uncertainty: Linguistic Invariances for Uncertainty Estimation in Natural Language Generation”, ICLR 2023;Farquhar, Kossen, Kuhn & Gal, “Detecting Hallucinations in Large Language Models Using Semantic Entropy”, Nature 2024。
  • Lin, Trivedi & Sun, “Generating with Confidence: Uncertainty Quantification for Black-box Large Language Models”, TMLR 2024。
  • Tian, Mitchell, Yao, Manning & Finn, “Fine-tuning Language Models for Factuality”, ICLR 2024。

校准的测量与重新校准

  • Guo, Pleiss, Sun & Weinberger, “On Calibration of Modern Neural Networks”, ICML 2017;Platt 1999;Zadrozny & Elkan 2001、2002。
  • Kumar, Liang & Ma, “Verified Uncertainty Calibration”, NeurIPS 2019;Nixon et al., “Measuring Calibration in Deep Learning”, CVPR Workshops 2019;Roelofs et al., “Mitigating Bias in Calibration Error Estimation”, AISTATS 2022。
  • Gneiting & Raftery, “Strictly Proper Scoring Rules, Prediction, and Estimation”, JASA 2007;Murphy 1973。
  • Ovadia et al., “Can You Trust Your Model’s Uncertainty? Evaluating Predictive Uncertainty Under Dataset Shift”, NeurIPS 2019。

LLM 的不确定性表达

  • Lin, Hilton & Evans, “Teaching Models to Express Their Uncertainty in Words”, TMLR 2022。
  • Xiong et al., “Can LLMs Express Their Uncertainty? An Empirical Evaluation of Confidence Elicitation in LLMs”, ICLR 2024。
  • Mielke, Szlam, Dinan & Boureau, “Reducing Conversational Agents’ Overconfidence through Linguistic Calibration”, TACL 2022;Zhou, Hwang, Ren & Sap, “Relying on the Unreliable: The Impact of Language Models’ Reluctance to Express Uncertainty”, ACL 2024;Band, Li, Ma & Hashimoto, “Linguistic Calibration of Long-Form Generations”, ICML 2024。
  • Burns, Ye, Klein & Steinhardt, “Discovering Latent Knowledge in Language Models Without Supervision”, ICLR 2023;Wang et al., “Self-Consistency Improves Chain of Thought Reasoning in Language Models”, ICLR 2023;Malinin & Gales, “Uncertainty Estimation in Autoregressive Structured Prediction”, ICLR 2021。
  • Mallen et al., “When Not to Trust Language Models: Investigating Effectiveness of Parametric and Non-Parametric Memories”, ACL 2023(PopQA:流行度与事实准确率)。

Selective prediction 与 conformal

  • Geifman & El-Yaniv, “Selective Classification for Deep Neural Networks”, NeurIPS 2017;Kamath, Jia & Liang, “Selective Question Answering under Domain Shift”, ACL 2020。
  • Angelopoulos & Bates, “A Gentle Introduction to Conformal Prediction and Distribution-Free Uncertainty Quantification”;Angelopoulos, Bates, Fisch, Lei & Schuster, “Conformal Risk Control”, ICLR 2024;Angelopoulos et al., “Learn then Test”, 2021。
  • Quach et al., “Conformal Language Modeling”, ICLR 2024;Mohri & Hashimoto, “Language Models with Conformal Factuality Guarantees”, ICML 2024;Kumar et al., “Conformal Prediction with Large Language Models for Multi-Choice Question Answering”, 2023。
  • Foygel Barber, Candès, Ramdas & Tibshirani, “The Limits of Distribution-Free Conditional Predictive Inference”, 2021;Gibbs & Candès, “Adaptive Conformal Inference Under Distribution Shift”, NeurIPS 2021。

15. 下一讲预告 · Next lecture

Lecture 14:评估智能体系统——Goal–Plan–Action(Evaluating Agentic Systems)。

这一讲的对象是一个回答:它对不对、模型有多确定、该不该说出来。下一讲的对象是一条轨迹:一个会计划、调用工具、多步行动的 agent。置信度的问题在那里换了形式——第 1 讲 §6.5 的表里,“calibration of classifiers → verbalized confidence → 什么时候停下来问用户”。一个 agent 在第 3 步不确定该查哪张表时,是猜一个、继续往下做,还是停下来澄清?第 14 讲从 Autumn 2025 的 Agent GPA 框架出发(goal fulfillment、logical consistency、execution efficiency、plan quality、plan adherence、tool selection / calling),讨论怎样定位多步轨迹里的错误、怎样验证”agent-as-a-judge”本身——和本讲一样,评估器也需要被评估。

← 返回课程目录