5D Library › 算法之镜 · English

第一章 作为条件分布的机器

不是神谕

语言模型是被训练来预测文本的。给定一串词元(词的片段),它为每一个可能的下一个词元分配一个概率 pθ(yt∣y<t)p_\theta(y_t \mid y_{<t}),回答就是从这些分布中逐个选出词元生成的。参数 θ\theta 在大量文本上拟合,这些文本大多出自人手,之后再通过示例和人类或自动化的偏好进一步训练调整。所以,粗略地说,模型是一个学得的条件分布:它说什么,取决于它得到什么。

这幅图景在两个重要方面并不完整。第一,模型并不是其训练文本的分布本身,而是对它的一种压缩的、可泛化的近似,能写出从未在任何文献中出现过的句子,也能流畅地陈述错误 (Ji et al. 2023)。第二,使模型变得有用的微调,也会重塑它的输出。

证据

柯克(Kirk)等人比较了用示例微调的模型与进一步用人类反馈强化学习(RLHF)训练的模型,发现 RLHF 提高了对新输入的泛化能力,却在多项指标上显著降低了输出的多样性 (Kirk et al. 2024)。更早,霍尔茨曼(Holtzman)等人表明,总是选择高概率续写来解码,会产生平淡、重复的文本,他们称之为神经文本退化;而从截断后的分布中采样,则产生更像人写的文本 (Holtzman et al. 2020)。

问题中的信息

信息论为一个问题带来了什么提供了精确的术语 (Shannon 1948; Cover and Thomas 2006)。答案的熵 H(Y)H(Y) 衡量在看到问题之前答案有多不确定。条件熵 H(Y∣X)H(Y \mid X) 衡量知道问题 XX 之后剩下的不确定性。两者之差就是互信息:

I(X;Y)=H(Y)−H(Y∣X),I(X;Y) = H(Y) – H(Y \mid X),

即知道问题平均能使答案的范围缩小多少比特。

推导

一个玩具语料:两类提示之后出现六种续写,从泛泛的总结到可检验的研究方案。程序计算 H(Y)H(Y)、H(Y∣X)H(Y \mid X) 与 I(X;Y)I(X;Y),以及每种提示把概率放在哪里。

import math

def H(p):
    return -sum(x * math.log2(x) for x in p if x > 0)

# A toy "corpus": how often each of six continuations follows two kinds of prompt.
# Columns: generic summary, cliché, textbook example, cross-field analogy, novel hypothesis, testable protocol
counts = {
    "vague prompt":       [400, 300, 200, 50, 30, 20],
    "constrained prompt": [40, 10, 60, 250, 300, 340],
}
p_x = {"vague prompt": 0.5, "constrained prompt": 0.5}
p_y_given_x = {x: [c / sum(cs) for c in cs] for x, cs in counts.items()}
p_y = [sum(p_x[x] * p_y_given_x[x][j] for x in p_x) for j in range(6)]

H_y = H(p_y)
H_y_x = sum(p_x[x] * H(p_y_given_x[x]) for x in p_x)
print(f"H(Y) = {H_y:.3f} bits, H(Y|X) = {H_y_x:.3f} bits, I(X;Y) = {H_y - H_y_x:.3f} bits")
for x, p in p_y_given_x.items():
    top = max(range(6), key=lambda j: p[j])
    print(f"{x:18s} H = {H(p):.2f} bits, most likely continuation #{top + 1} (p = {p[top]:.2f}), "
          f"share on #4–6 = {sum(p[3:]):.2f}")
# H(Y) = 2.565 bits, H(Y|X) = 2.020 bits, I(X;Y) = 0.544 bits
# vague prompt       H = 1.99 bits, most likely continuation #1 (p = 0.40), share on #4–6 = 0.10
# constrained prompt H = 2.05 bits, most likely continuation #6 (p = 0.34), share on #4–6 = 0.89

结果有一点很有启发,却容易被忽略。有约束的提示并没有使答案的不确定性小多少:它的熵与模糊提示差不多。它改变的是概率落在哪里。模糊提示之后,90% 的概率落在泛泛的总结、陈词滥调和教科书式例子上;有约束的提示之后,89% 落在类比、假说和研究方案上。问题中的信息,与其说是收窄,不如说是改变方向。

解码能做什么、不能做什么

系统通常允许使用者或设计者设定一个温度,用来使下一个词元的分布变尖或变平。人们容易以为温度越高,输出越有原创性。

推导

一个在 1000 个候选词元上呈齐普夫形状的分布,在不同温度下的变化。

import math

# Next-token choice from a Zipf-shaped distribution over 1,000 candidates.
# Greedy decoding always returns rank 1; sampling at temperature T reshapes p ∝ p^(1/T).
N = 1000
base = [1 / (r + 1) ** 1.1 for r in range(N)]
Z = sum(base)
p = [x / Z for x in base]

def reshape(p, T):
    q = [x ** (1 / T) for x in p]
    s = sum(q)
    return [x / s for x in q]

for T in (0.3, 0.7, 1.0, 1.3):
    q = reshape(p, T)
    print(f"T = {T}: P(rank 1) = {q[0]:.2f}, P(top 10) = {sum(q[:10]):.2f}, P(rank > 100) = {sum(q[100:]):.2f}")
print("Temperature spreads probability, but it spreads it over whatever the context made likely;")
print("it cannot put mass on continuations the context did not support.")
# T = 0.3: P(rank 1) = 0.90, P(top 10) = 1.00, P(rank > 100) = 0.00
# T = 0.7: P(rank 1) = 0.43, P(top 10) = 0.82, P(rank > 100) = 0.04
# T = 1.0: P(rank 1) = 0.18, P(top 10) = 0.48, P(rank > 100) = 0.23
# T = 1.3: P(rank 1) = 0.08, P(top 10) = 0.26, P(rank > 100) = 0.44
# Temperature spreads probability, but it spreads it over whatever the context made likely;
# it cannot put mass on continuations the context did not support.

低温几乎把全部概率集中在最可能的词元上;高温把概率沿着排名往下摊开。但它摊开的比例,取决于上下文已经使哪些内容变得可能。温度增加的是多样性,不是方向。用高温回答一个信息量很低的提示,得到的是典型答案的各种变体,有时是胡言乱语,而不是一个框定良好的问题所能得到的答案。

假说 · 尚未验证

尚未确立。 对于研究规划类任务,提示的信息量(按第八章的审计衡量:约束、跨领域桥接、可证伪条件和独立来源)比采样温度更能预测专家对模型输出新颖性的评分。该假说预测:在匹配的主题上,提高审计分数对盲评新颖性评分的提升,大于把温度从 0.7 提高到 1.2 的提升。如果在一项同时改变两个因素的析因实验中,温度的效应与提示的效应一样大或更大,假说就被推翻。

注

「信息量低的提示产生泛泛的回答」是一种倾向,不是定律。模型确实会对简短的提示给出出人意料的具体回答,特别是当简短的提示指向一个狭窄的主题时。这一说法针对的是大量问题上的平均情况。

参考文献

Cover, Thomas M., and Joy A. Thomas. 2006. Elements of Information Theory. 2nd ed. Wiley-Interscience.
Holtzman, Ari, Jan Buys, Li Du, Maxwell Forbes, and Yejin Choi. 2020. “The Curious Case of Neural Text Degeneration.” International Conference on Learning Representations (ICLR).
Ji, Ziwei, Nayeon Lee, Rita Frieske, et al. 2023. “Survey of Hallucination in Natural Language Generation.” ACM Computing Surveys 55 (12): 248:1–38.
Kirk, Robert, Ishita Mediratta, Christoforos Nalmpantis, et al. 2024. “Understanding the Effects of RLHF on LLM Generalisation and Diversity.” International Conference on Learning Representations (ICLR).
Shannon, Claude E. 1948. “A Mathematical Theory of Communication.” Bell System Technical Journal 27 (3): 379–423.

Fan, Y. (2026). The Algorithmic Mirror: Self-Attention Geometry, Ashby's Requisite Variety and Cognitive Projection in Information Theory and East Asian Philosophy of Mind [算法之镜] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.23723557 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).