跳到主要内容

让它每次答得不一样 — 温度与 top-p 到底在动什么

这一章讲什么: 你在任何一家模型服务的开发文档里都会撞见两个旋钮 —— temperaturetop_p。 这一章把它们拆到底:它们各自动的是哪一步、动完之后那些数变成什么样、 以及一个几乎所有人都想不到的结论 —— 它们本身几乎不提分。

它在全书链条里的位置: 「不动权重」这条路的第二站,但它不是一招提分手段,是一件前提。 第 08 章那招「多答几遍再投票」要成立,先得让它每遍答得不一样 —— 那正是这一章造出来的东西。

需要的基础: 第 03 章那张分数表(一次算出 151936 个原始分,然后挑最大的那个)。

顶层全景:同一句话,从「永远是柏林」到「一千次里柏林占 435 次」

这一章换一个更短的输入,因为书在这里给了完整的中间数字:

"The capital of Germany is"(德国的首都是)

"The capital of Germany is"
│ ① 切块 → 5 个编号:785 / 6722 / 315 / 9856 / 374

② 跑一遍,取最后一行 → 151936 个原始分(logits),取值大约在 −8 到 20 之间
│ 其中编号 19846(" Berlin")那一个的分最高

├─ ③ 老做法:直接挑最大的 ⟹ 永远是 " Berlin",问一万遍都一样

└─ ④ 新做法:先把每个分数**除以一个数**(这个数就是温度)
│ ⑤ 再换算成「加起来等于一」的可能性
│ 温度 5.0 时,Berlin 只有 0.0003
│ 温度 0.5 时,Berlin 涨到 0.3398
│ ⑥ 按这些可能性抽一个
▼ 温度 5.0 抽出来是编号 65094 = " mistress"(完全不着调)

⑦ 抽一千次看分布:温度 0.35 时 " Berlin" 出现 435 次
⑧ 再加一道闸(top-p = 0.8):" Berlin" 涨到 534 次,而 " Munich"、" Hamburg" 被整个滤掉

图说:整章只动第 ③ 步 —— 那张分数表怎么算出来的,一个字没改。

1. 先解释一件怪事:同一句话问两遍,为什么答案一字不差

先看现象。 第 05、06 章跑的每一次评测,只要输入不变,输出就一模一样 —— 连标点都不差。这和大家平时用聊天产品的体验不一样。

原因在第 03 章那张分数表上,而且非常干脆:

同一句输入
│ 模型里那 6 亿个数没变

那 151936 个分数每次算出来都一样(同样的输入 → 同样的输出)
│ 挑法是「取最大的那个」

挑出来的编号必然一样 ⟹ 写下的字必然一样 ⟹ 下一轮的输入也一样 ⟹ …

图说:这个循环里没有任何一处引入了随机。所以它必然复读。

「永远挑分最高的那个」这个动作,第 03 章第 4 节已经命名过了:贪心解码。

所以结论只有一条:想让它给出第二种解法,只能动「挑词」那一步 —— 分数表那一层动不了(那要改模型),能动的只有从分数表到一个具体的词之间那几步。

2. 温度:把每个分数除以一个数

走查第 ①、② 步先走完。 那句话被切成 5 块,编号是 785 / 6722 / 315 / 9856 / 3741; 跑一遍取最后一行,得到 151936 个原始分,这一行分数第 03 章讲过,名字叫 logits2; 书还把它们画了出来:在编号 19800 到 19899 那一小段里,取值大约从 −8 到 20, 而 20 那一个正是 " Berlin"3直接挑最大的,解出来就是 ' Berlin'4

现在动手。温度的全部动作只有一行:把这一行分数,每一个都除以同一个数。

这个数就叫温度(temperature)。书里的实现真的就只有 logits / temperature 一句5

除以一个数,凭什么就改变了结果? 因为下一步要把这些分数换算成「可能性」, 而换算的方式对差距很敏感:

假设只有三个候选,原始分是 10、8、6 (这三个数是为演示编的,不是书里的)
│ 除以 0.5(温度小) → 20、16、12 差距被**拉大**
│ 除以 5 (温度大) → 2、1.6、1.2 差距被**压小**

换算成可能性之后:
温度小 ⟹ 分布变**尖**:最高的那个几乎独占,模型显得很有主见
温度大 ⟹ 分布变**平**:大家都有机会,模型显得敢冒险

图说:温度没有增加任何信息,它只是在**放大或压缩已有的差距**。

书还就地给了两条口径6:

  • 温度必须是正数;
  • 温度等于 1.0 等于什么都没做 —— 一个数除以 1 还是它自己。

「温度」这个名字的来历,书专门开了个边栏:来自物理学 —— 那里的温度控制一个系统里有多少随机和运动;这里借用同一个想法, 控制模型挑下一个词时有多自信、多放得开7(比方到此为止,下面一律说「除以温度」。)

除完之后还要再过一道换算

除完之后的数还是有正有负、也可能大过 1,不能直接当「可能性」用。 要再过一道手续,把它们变成「每个都落在 0 和 1 之间、全部加起来正好等于 1」的一组数 —— 做这道换算的手续,名字就叫 softmax8

换算出来的那一整套数(每个都在 0 到 1 之间、加起来正好是 1), 这一行就叫概率分布。后面每一章说到它,说的都是这样一组数。

书给的两个真数9:

温度" Berlin" 换算出来的可能性
5.00.0003
0.50.3398

同一张分数表,只是除数不同,正确答案的可能性差了一千倍。 (书顺带解释了为什么演示时用 5.0 这么大的温度:那样才好把 Berlin 那根柱子和别的画在一起看; 温度小的时候其他词几乎全贴到 0 了9。)

3. 按可能性抽一个:结果真的会不着调

走查第 ⑥ 步。有了一组「加起来等于 1」的可能性,就可以按这些权重抽一个 —— 这个动作叫多项式采样(意思就是「照着各自的份额抽签」)。

书给了一个三行的小例子,把它和贪心解码摆在一起10:

假设模型给出:Berlin 0.70、Munich 0.20、Hamburg 0.10
├─ 贪心解码:永远返回 Berlin,一万次都是
└─ 按份额抽签:抽很多次的话,Berlin 约占 70%、Munich 约 20%、Hamburg 约 10%

回到真实的例子。温度调到 5.0,抽出来的是编号 65094,解回来是 " mistress" —— 在「德国的首都是」这个上下文里完全不着调11。书说得很直白: 这就是高温度的效果 —— 它鼓励模型去抽 " Berlin" 之外的东西。

一条工程坑,书写下来了,值得记12: 抽签这个函数在显卡上跑,结果可能和别处不同,采样数一大甚至会崩 (作者在 PyTorch 2.9 上的两种显卡设备上都遇到过)。 所以书里的代码强制把这一步放回中央处理器上跑。

4. 抽一千次,看看都抽出了什么

这一节是全章最有说服力的一组数,因为它把「一组可能性」这件抽象事变成了可以数的次数。

书拿同一句输入,在两个温度下各抽 1000 次13:

温度 5.0(太高)温度 0.35
'}' 2 次、' </' 2 次、' represent' 2 次、' Inf' 2 次、'()*' 2 次、' beside' 2 次……' Berlin' 435 次' ______' 209 次、' ____' 169 次、' __' 158 次、' _____' 18 次、' Munich' 3 次、' Hamburg' 3 次
1000 次里没有任何一个词出现超过 2 次,而且全是废话435/1000 ≈ 43% 抽到正确答案

书对右边那一列的两条解读特别好14:

  1. 那几个稀有候选并非完全不相干 —— ' Munich'' Hamburg' 都是德国大城市;
  2. 那一堆下划线是怎么回事? 书的推测是:模型见过大量填空题格式的文字, 比如 "The capital of Germany is ____"

——第二条尤其值得停一下:它是第 01 章「模式匹配」的又一个实物证据。 模型不是在回答问题,它是在续写它见过的那种句子。

书自己提出并回答了那个最该问的问题

书把读者会问的话直接写了出来15:

既然 " Berlin" 是对的,那故意让它有时候答错,图什么?

书的回答是:随机性让模型去探索别的可能;而在推理任务上,这种多样性正是下一招的前提 —— 采出多个候选答案,再互相比较,以此提高准确率15

这句话就是这一章存在的全部理由。 温度不是用来提分的,它是用来让「多答几遍」变得有意义的。

书还给了实用取值区间16:

温度含义
0.0就是贪心解码 —— 永远挑可能性最高的那个
0.3–0.8想要一点多样性、又不至于跑飞时的常用区间
更高适合创作和大范围搜索,但在需要唯一正确答案的任务上会伤可靠性

一处书自己的不一致,我们照实说明: 书在两处代码里对「什么算贪心」写反了 —— 一处写的是「温度是 1.0 就走贪心」,另一处写的是「温度是 0.0 就走贪心」。 而正文和边栏的口径是明确的:0.0 是贪心,1.0 是什么都不改我们按正文口径(这是早期试读版的笔误)17

5. 低温也会误伤:再加一道闸,top-p

先看问题。上一节那张表右边一列里,' Munich'' Hamburg' 各出现 3 次。 它们还算沾边,但同样的机制也会让某个完全不相干的词偶尔中签 —— 书把这件事称为「意外抽到低置信度的词」18

改法是在抽签之前先做一道筛选:

把所有候选按可能性从高到低排队,从头开始累加,凑够 p 就不再往下看; 后面的全部作废。 —— 这个旋钮的名字叫 top-p,它还有个名字叫核采样19

书用一个只有 10 个词的玩具例子把四步走了一遍,每一步的数都印出来了。 这十个原始分是20:

[-0.7, -3.0, 0.1, -1.2, 2.0, -1.0, -0.5, -2.0, 0.3, 1.5]

四步走查(设 p = 0.8):

① 换算成可能性,再按从大到小排序
② 从头累加 → 0.4538, 0.7290, 0.8119, 0.8798, 0.9170, …, 1.0000
③ 卡 0.8 这条线 ——**这里有个坑,见下**—— 保留 3 个
④ 其余全部置零 → [0, 0, 0, 0, 0.4538, 0, 0, 0, 0.0829, 0.2752]
│ 再把留下的重新算一遍份额
▼ [0, 0, 0, 0, 0.5589, 0, 0, 0, 0.1021, 0.3390]
↑ 注意这三个数加起来正好是 1

第 ③ 步那个坑值得单独说,因为它是两种写法的分水岭21:

写法判据结果
最直觉的写法累加值 ≤ 0.8 才留只留 2 个(第三个累加到 0.8119,过线了)
通行的写法跨过门槛的那一个也留下3 个

通行写法才符合 top-p 的定义:留下「累计可能性至少达到 p」的最小一撮21

第 ④ 步那个「重新算一遍份额」不是可有可无的。 书解释得很清楚: 砍掉一批之后,剩下的加起来不再等于 1 了,不重算就没法正确抽签22

加上 top-p 之后,那一千次抽签变成什么样

书把第 4 节那组数重跑了一遍,只多加了 top_p = 0.823:

温度 0.35,不加 top-p温度 0.35 + top-p = 0.8
Berlin 435 次 · ______ 209 · ____ 169 · __ 158 · _____ 18 · Munich 3 · Hamburg 3Berlin 534 次 · ______ 249 · ____ 217 —— Munich 和 Hamburg 被整个滤掉

走查第 ⑧ 步就此走完。 注意最后一栏的代价:滤掉的不只是废话,连两个沾边的城市也没了。 这就是这道闸的性质 —— 它是在拿多样性换稳当。

顺带认一下它的兄弟:top-k

书专门开了个边栏对比24:

保留几个
top-k固定 k 个 —— 排序之后,前 k 个之外全砍掉
top-p个数是浮动的 —— 取决于累计到多少才够 p

书还说明了自己的取舍:top-k 更好实现,他在前一本书里讲过;而 top-p 近年更流行24。 (top-k 的完整实现在我们书架上的那一章25。)

6. 落点:这两个旋钮本身几乎不提分

这一节是全章最反直觉的部分,也是这一章为什么必须存在的理由。

把温度和 top-p 都设成 0.9,在全部 500 道题上跑一遍26:

准确率耗时
基线(贪心解码)15.2%10.1 分钟
加上温度 + top-p17.8%30.7 分钟

只涨了 2.6 个点,而时间涨了三倍。 对比第 06 章那一招(涨 25.4 个点),几乎等于没涨。

书对这个结果的判词非常重要,原话的意思是:这在预期之内26:

温度和 top-p 本身不是推理时扩展技术。 它们只是控制采样多样性的旋钮。

而那三倍的耗时,书也澄清了不是采样代码的开销 —— 是因为模型有时候会生成更长的回答26

所以这一章的落点是:

第 06 章:加一句话 ⟹ 直接提分(15.2% → 40.6%)
第 07 章:温度 + top-p ⟹ 几乎不提分(15.2% → 17.8%),但**让「多答几遍」成为可能**
第 08 章:多答几遍再投票 ⟹ 靠上一行造出来的多样性提分

图说:这一章是一件前提,不是一招手段。
**把它当手段用(「调调温度看看能不能提分」),量出来就是那 2.6 个点。**

书自己也顺手把这一章的走查题重跑了一遍: 温度 0.5 + top-p 0.8 之下, 那道「3x−9 的一半等于 x+37」答出了 \boxed{18} —— 还是错的27一次采样并不会让它变聪明。它只是换了个错法。

作者的判断与证据

说法性质
5 个编号、[1, 151936] 的形状、' Berlin'、0.0003 / 0.3398、' mistress'书里印出来的实测
两组 1000 次抽样的分布书里印出来的实测(设了随机种子,可复现)
那 10 个玩具分数走完的四步书里印出来的实测
加 top-p 之后 Berlin 从 435 涨到 534书里印出来的实测
15.2% → 17.8%,10.1 → 30.7 分钟书里的实测,表 4.1 第 1、4 行
下划线是因为模型见过填空题格式作者的推测,措辞是「很可能」
「温度和 top-p 本身不是推理时扩展技术」作者的判断,而且是这一章的落点
耗时变长是因为回答变长了,不是采样代码的开销作者的解释,书没有给出「回答长度」的对照数据
0.3–0.8 是常用区间作者给的经验值,不是从实验里推出来的
「温度」来自物理学词源说明,不影响任何机制
两处代码里「什么算贪心」写反了这是我们发现的,书没有承认(见第 4 节末尾那个块)

判断(我们的,不是书里的): 这一章最该带走的不是两个旋钮的用法, 而是那句「它们本身不是提分手段」。 在实践里,「调调温度看看能不能变好」大概是最常见的一种时间浪费 —— 而这本书用一行成绩(2.6 个点、三倍耗时)把它钉死了。 如果错,会错在: 这一行只测了 0.9 这一组取值。 书自己也说 0.3–0.8 才是常用区间,却在表 4.1 里全用 0.9 —— 如果换成 0.5 结果明显不同,那么「几乎不提分」这个结论就要收窄成 「在 0.9 这个偏高的设置下几乎不提分」。书没有做这组对照。

边界与局限

  1. 只测了 0.9 这一组取值。 见上面那个判断块 —— 这是这一章最大的空白。
  2. top-p 的门槛只演示了 0.8,真实评测用的是 0.9。 两者之间的差别书没有量。
  3. 那个玩具例子只有 10 个词。 真实词表有 151936 个,排序和累加的开销书没有讨论。
  4. 抽签在不同设备上结果可能不同,甚至会崩。 书给了绕开的办法(放回中央处理器), 但没有解释为什么会这样。
  5. top-k 只在边栏里对比了一下,没有实现、没有测。
  6. 两处代码里「什么算贪心」写反了(第 4 节末尾),引用书里的代码时要当心。

可带走的

  1. 同一句话问两遍答案一模一样,不是缓存,是机制: 分数表不变、挑法是「取最大」, 所以结果必然复读。想要不一样,只能动「挑词」那一步。
  2. 温度的全部动作:把那一行原始分数,每一个都除以同一个数。 实现只有一行。 它不增加任何信息,只放大或压缩已有的差距。
  3. 除完之后要过一道 softmax,把它们换算成「加起来等于一」的一组数 —— 那组数叫概率分布。
  4. 记住三个刻度:0.0 就是贪心解码,1.0 等于什么都没改,0.3–0.8 是常用区间。
  5. 温度高会抽到完全不着调的词。 书的实例:温度 5.0 时,「德国的首都是」后面抽出了 " mistress"
  6. top-p 是第二道闸:把词按可能性排队,累计够 p 就不再往下看。 它还有个名字叫核采样。
  7. 砍完必须重新算一遍份额 —— 否则剩下的加起来不等于 1,抽签就是错的。
  8. 卡门槛要把跨过线的那一个也留下,否则留的比定义要求的少。这是实现时最容易写错的一处。
  9. top-p 留几个是浮动的,top-k 留几个是固定的。 出门两个名字都会撞见。
  10. 这两个旋钮本身几乎不提分(15.2% → 17.8%,耗时却涨三倍)。 它们是「多答几遍」的前提,不是手段 —— 这一条能省下你大量瞎调参数的时间。
  11. 加了随机之后,一次采样并不会让模型变聪明。 那道题从答 20 变成答 18,还是错的。 真正提分的是下一章:把采出来的好几个答案放在一起比。

原文地图

主题原书章原文位置
五个编号与那一行原始分4.4 Controlling output diversity with temperature scalingtext/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:128(搜「785, 6722」) · :134(搜「also called logits」) · :149(搜「[1, 151936]」)
直接挑最大的解出 Berlin同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:178(搜「Decoded token」)
分数取值范围 −8 到 20同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:254(搜「values range approximately from -8 to 20」)
温度的实现只有一行同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:298(搜「return logits / temperature」) · :311(搜「A temperature of 1.0 means no change」)
「温度」这个词的来历同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:430(搜「comes from physics」)
softmax 的定义同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:583(搜「converts a vector of raw scores」)
0.0003 与 0.3398同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:538(搜「The probability is 0.0003」) · :570(搜「0.0003 to 0.3398」)
抽出 " mistress"同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:697(搜「mistress」)
抽签在显卡上可能不同甚至崩同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:709(搜「may even crash when we draw larger numbers」)
Berlin/Munich/Hamburg 那个三行例子同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:783(搜「assigns the following probabilities」) · :806(搜「Multinomial sampling instead draws one token」)
两组 1000 次抽样的分布同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:847(搜「'}': 2x」) · :889(搜「' __': 158x」)
对分布的两条解读同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:907(搜「big cities in Germany」)
「故意答错图什么」与回答同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:913(搜「what's the point in making the model occasionally give the wrong answer」) · :925(搜「self-consistency」)
取值建议同上text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:1039(搜「A temperature of 0.0 corresponds to greedy decoding」)
低置信度的词会被意外抽到4.5 Balancing diversity and coherence with top-p samplingtext/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:7(搜「we may end up sampling "weird" tokens」)
top-p 与核采样这两个名字同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:13(搜「nucleus sampling」) · :185(搜「keep the smallest set of tokens」)
为什么必须重新算份额同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:32(搜「no longer sum to one」)
十个玩具分数同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:81(搜「toy_logits」)
累加值与两种写法同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:213(搜「Cumulative sum」) · :215(搜「Tokens kept: 2」) · :246(搜「returns 3 as the number of tokens kept」)
置零与重算份额的两行输出同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:346(搜「0.4538」) · :374(搜「0.5589」)
加 top-p 后 Berlin 534 次同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:554(搜「' Berlin': 534x」) · :556(搜「' ______': 249x」)
走查题答成 18(仍错)同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:653(搜「still not correct」)
top-k 对照同上text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:666(搜「Top-k filtering is another way」) · :690(搜「whereas top-p keeps a variable number」) · :696(搜「Top-p sampling has become more popular」)
表 4.1 第 4 行与作者的解读4.6 Improving response accuracy with self-consistencytext/33-ch04-06-4-6-improving-response-accuracy-with-self-consis.txt:431(搜「17.8%」) · :433(搜「30.7 min」) · :575(搜「not inference-time scaling techniques themselves」)

Footnotes

  1. 出处:「4.4 Controlling output diversity with temperature scaling」第 128 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:128,搜「785, 6722」)。

  2. 出处:「4.4 Controlling output diversity with temperature scaling」第 134 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:134,搜「also called logits」)与第 149 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:149,搜「[1, 151936]」)。第 149 段还就地解释了词表大小是什么:分词器能处理、模型能生成的所有不重复的小块。

  3. 出处:「4.4 Controlling output diversity with temperature scaling」第 254 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:254,搜「values range approximately from -8 to 20」)。那张图只画了编号 19800 到 19899 这 100 个,不是全部 151936 个。

  4. 出处:「4.4 Controlling output diversity with temperature scaling」第 178 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:178,搜「Decoded token」)。这一步用的是取最大值,即第 03 章讲过的贪心解码。

  5. 出处:「4.4 Controlling output diversity with temperature scaling」第 298 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:298,搜「return logits / temperature」)。整个温度机制在代码上就是这一行。

  6. 出处:「4.4 Controlling output diversity with temperature scaling」第 311 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:311,搜「A temperature of 1.0 means no change」)。

  7. 出处:「4.4 Controlling output diversity with temperature scaling」第 430 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:430,搜「comes from physics」)。这是书里的一个边栏。

  8. 出处:「4.4 Controlling output diversity with temperature scaling」第 583 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:583,搜「converts a vector of raw scores」)。原文的说法是:softmax 把一组原始分数换算成一个概率分布,每个值都在 0 和 1 之间、加起来等于 1;这样既好解读,后面也才能拿来抽签。

  9. 出处:「4.4 Controlling output diversity with temperature scaling」第 538 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:538,搜「The probability is 0.0003」)与第 570 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:570,搜「0.0003 to 0.3398」)。后一段同时解释了为什么演示时用 5.0 这么大的温度。 2

  10. 出处:「4.4 Controlling output diversity with temperature scaling」第 783 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:783,搜「assigns the following probabilities」)与第 806 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:806,搜「Multinomial sampling instead draws one token」)。这是书里的一个边栏,Berlin 0.70 / Munich 0.20 / Hamburg 0.10 是它假设的数,不是实测。

  11. 出处:「4.4 Controlling output diversity with temperature scaling」第 697 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:697,搜「mistress」)。

  12. 出处:「4.4 Controlling output diversity with temperature scaling」第 709 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:709,搜「may even crash when we draw larger numbers」)。原文同时说明了书里设了随机种子,好让这一章的代码可复现。

  13. 出处:「4.4 Controlling output diversity with temperature scaling」第 847 段起(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:847,搜「'}': 2x」)是温度 5.0 那一组;第 889 段起(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:889,搜「' __': 158x」)是温度 0.35 那一组。两组都抽了 1000 次。

  14. 出处:「4.4 Controlling output diversity with temperature scaling」第 907 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:907,搜「big cities in Germany」)。下划线那条解释书用的措辞是「很可能」(likely)。

  15. 出处:「4.4 Controlling output diversity with temperature scaling」第 913 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:913,搜「what's the point in making the model occasionally give the wrong answer」)与第 925 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:925,搜「self-consistency」)。后一段明确点名了下一招的名字,那正是我们第 08 章的内容。 2

  16. 出处:「4.4 Controlling output diversity with temperature scaling」第 1039 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:1039,搜「A temperature of 0.0 corresponds to greedy decoding」)。这是书里的一个边栏。

  17. 出处:两处代码清单的判断条件,「4.4 Controlling output diversity with temperature scaling」第 968 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:968,搜「temperature == 1.0」)与「4.5 Balancing diversity and coherence with top-p sampling」第 596 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:596,搜「temperature == 0.0」)。这处不一致是我们通读时发现的,书没有承认也没有解释;正文与边栏的口径见同章第 311 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:311,搜「A temperature of 1.0 means no change」)与第 1039 段(text/31-ch04-04-4-4-controlling-output-diversity-with-temperatur.txt:1039,搜「A temperature of 0.0 corresponds to greedy decoding」)。

  18. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 7 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:7,搜「we may end up sampling "weird" tokens」)。

  19. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 13 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:13,搜「nucleus sampling」)与第 185 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:185,搜「keep the smallest set of tokens」)。后一段还说明了名字里那个 p 就是 probability(可能性)。

  20. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 81 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:81,搜「toy_logits」)。

  21. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 213 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:213,搜「Cumulative sum」)、第 215 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:215,搜「Tokens kept: 2」)与第 246 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:246,搜「returns 3 as the number of tokens kept」)。 2

  22. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 32 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:32,搜「no longer sum to one」)。置零与重算之后的两行数见第 346 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:346,搜「0.4538」)与第 374 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:374,搜「0.5589」)。

  23. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 554 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:554,搜「' Berlin': 534x」)与第 556 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:556,搜「' ______': 249x」)。

  24. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 666 段起的边栏(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:666,搜「Top-k filtering is another way」),对照句在第 690 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:690,搜「whereas top-p keeps a variable number」),作者的取舍在第 696 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:696,搜「Top-p sampling has become more popular」)。 2

  25. 补充(不在书里,依据我们的 book 书架):这本书明说 top-k 的实现在作者前一本书里。依据: shelf=ai-book-reference/build-large-language-model#07-decoding-and-weights.md 事实=该章从零实现了贪心解码、按份额抽签、温度与 top-k 这几种挑词的做法 —— 它没讲 top-p,那正是本章补上的增量。

  26. 出处:表 4.1 第 4 行,「4.6 Improving response accuracy with self-consistency」第 431 段(text/33-ch04-06-4-6-improving-response-accuracy-with-self-consis.txt:431,搜「17.8%」)与第 433 段(text/33-ch04-06-4-6-improving-response-accuracy-with-self-consis.txt:433,搜「30.7 min」);作者的解读在第 575 段(text/33-ch04-06-4-6-improving-response-accuracy-with-self-consis.txt:575,搜「not inference-time scaling techniques themselves」)。同一段说明了表 4.1 里所有涉及温度和 top-p 的实验都用 0.9 这个取值,以及耗时变长是因为回答变长。 2 3

  27. 出处:「4.5 Balancing diversity and coherence with top-p sampling」第 653 段(text/32-ch04-05-4-5-balancing-diversity-and-coherence-with-top-p.txt:653,搜「still not correct」)。同一段书也说明了做这些的目的:主要是为了能采出不同的输出,好在下一节实现那招投票。