暴露偏差:不完美的前缀会怎样影响下一步
Exposure bias
区分前缀分布变化与必然失败,计算恢复过程,并检查scheduled sampling的有限反例。
exposure-bias拐错一次以后,后面的路也可能走回来
设想一个虚构生成器,目标是交替输出0与1。训练提供正确的较早符号,生成时则使用模型自己已经输出的符号。若它重复了一次0,下一步的输入就偏离了训练前缀。之后怎样,取决于模型:它可能一直重复,可能恢复交替,也可能结束序列。
暴露偏差(exposure bias,也译曝光偏差)关注训练前缀与生成前缀之间的差异。先说清涉及哪些分布,再判断后果。前缀分布变化本身不能证明误差会逐位置增加、每种生成失败都由此引起,或似然训练在数学上无效。Bengio等人描述了这种不匹配,并提出在训练中混用观测输入与模型生成输入。前缀选择,第2.4节
应分别测真实前缀上的预测、实际解码,以及对受控前缀变化的敏感性。语言模型可能给某条参考续文较低概率,却生成另一条可接受的续文;反过来,读起来通顺的输出也可能改掉必须保留的事实。把这些现象当作暴露偏差证据之前,先定义任务和错误。
把条件误差与对它的加权分开
令h为前缀状态,d(h)为数据中的概率,q(h)为某种生成策略下的概率。固定一个介于0与1之间的条件误差g(h),则:
差值可以为正、为零或为负。若d=[1,0]、q=[0.8,0.2]、g=[0.1,0.9],则D=0.1、R=0.26,比值为2.6。但D=0时比值无定义;0/0也不能被解释成两种非零表现相同。这项有限诊断是原创教学构造。He等人比较开放式生成中的数据/模型前缀,并区分边缘续文质量与条件一致性;本程序没有实现其完整指标。量化背景,第5节
为了计算一种可能的累积过程,把当前上下文分为正常和偏离两态。epsilon是正常转为偏离的概率,rho是恢复概率,e-t是第t步处于偏离态的概率。由全概率公式:
这里描述的是当前上下文偏离,不是“整个历史前缀都正确”。过去发生的错误不会从历史中消失,但它对当前行为的影响可能减弱。当epsilon+rho>0时,减去不动点便得到精确解:
要从任意初态收敛,乘子的绝对值必须小于1。epsilon=rho=1时过程交替振荡;epsilon=rho=0时初态保持不变。无恢复且e0=0时,e-t=1−(1−epsilon)^t;有恢复时,偏离可能趋向较小值,也可能起初下降。这是带条件的玩具模型结果,不是所有自然语言错误的定理。
一种改进方法还可能改变实际拟合的目标。考虑公平二元训练对x2=x1,固定模型首符号边缘为正确的公平分布。以概率r用独立模型样本替换第一个符号,否则保留观测符号;两种情况都保留原第二符号标签。得到的联合表以行为提供给模型的前缀、列为原标签:
原因可以逐项数出来:不替换的分支给两个对角格各加(1−r)/2;替换分支给四格各加r/4。每行总质量为1/2,除以行和便得到拟合条件表。交叉熵在这个行分布处最小,因为:
数据概率为零的项贡献零;若模型给实际可能出现的结果分配零概率,损失为无穷大。把这个修改目标的最优条件模型拿回原始、未污染数据上评测,第二词NLL为:
r=1时,两行条件分布都是[0.5,0.5],符号之间的依赖丢失。这个有限例子说明Huszár分析的问题:把独立抽样前缀与不变标签配对,不一定保留原似然目标。这里r表示模型前缀概率;论文中的epsilon恰好相反,表示使用真实符号的概率。本例固定首词边缘、采用采样而非argmax,不能证明每种日程在每种任务上都失败。两符号分析,第4.1节
先穷举情况,再判断错误是否来自偏移
完整原创程序需要Python 3.12、NumPy 1.26.4。它精确传播两态概率,枚举替换事件构造联合表,并比较条件目标;没有训练真实语言模型。
"""Original finite examples: recoverable context deviation and scheduled sampling.
The Markov states describe current context, NOT an all-correct historical prefix.
No real-language benchmark or universal error-growth law is claimed.
"""
import json
import math
import numpy as np
def probability(x):
if isinstance(x, (bool, np.bool_)) or not np.isscalar(x):
raise ValueError('Expected a numeric probability')
try:
value = float(x)
except (TypeError, ValueError, OverflowError) as exc:
raise ValueError('Expected a numeric probability') from exc
if isinstance(x, (str, bytes, complex)) or not math.isfinite(value) or not 0 <= value <= 1:
raise ValueError('Probability must be finite and in [0,1]')
return value
def deviation(epsilon, recovery, steps=20, initial=0.):
"""P(bad at t), including t=0; exact two-state distribution propagation."""
epsilon, recovery, initial = map(probability, (epsilon,recovery,initial))
if isinstance(steps,(bool,np.bool_)) or not isinstance(steps,(int,np.integer)) or not 0 <= steps <= 10000:
raise ValueError('Use 0..10000 integer steps')
values = [initial]
for _ in range(steps):
e = values[-1]
values.append((1-e)*epsilon + e*(1-recovery))
return np.asarray(values)
def scheduled_joint(model_prefix_probability):
"""Joint(input prefix, original target) for fair data with x2=x1.
r is the probability of a fresh fair model prefix independent of the pair.
q1 is fixed at the true fair marginal; only q2 is optimized. Sampling,
not greedy argmax, is assumed. The supervised target is never rewritten.
"""
r = probability(model_prefix_probability)
joint = np.zeros((2,2))
for original in (0,1):
joint[original,original] += .5*(1-r)
for replacement in (0,1):
joint[replacement,original] += .5*r*.5
return joint
def conditional_fit(joint):
a = np.asarray(joint,dtype=float)
if a.shape != (2,2) or not np.isfinite(a).all() or (a<0).any() or not np.isclose(a.sum(),1,rtol=0,atol=1e-12):
raise ValueError('Expected a normalized 2 by 2 joint distribution')
mass = a.sum(1,keepdims=True)
if (mass == 0).any():
raise ValueError('A zero-mass prefix has no identified conditional')
return a/mass
def cross_entropy(joint, conditional):
"""Second-token NLL; zero assigned mass to an observed outcome gives infinity."""
conditional_fit(joint) # Validate, but do not replace the supplied model.
a = np.asarray(joint,dtype=float); q = np.asarray(conditional,dtype=float)
if q.shape != (2,2) or not np.isfinite(q).all() or (q<0).any() or not np.allclose(q.sum(1),1,rtol=0,atol=1e-12):
raise ValueError('Conditional rows must sum to one')
present = a > 0
if (q[present] == 0).any():
return math.inf
return float(-np.sum(a[present]*np.log(q[present])))
def prefix_comparison(data_prefix, model_prefix, errors):
"""Expectation of the SAME bounded conditional error under two prefix laws.
A ratio with zero data-prefix error is undefined, including 0/0.
This is a finite diagnostic, not the paper's language-generation metric.
"""
d = np.asarray(data_prefix,dtype=float); m = np.asarray(model_prefix,dtype=float)
errors = np.asarray(errors,dtype=float)
if d.ndim != 1 or not 1 <= len(d) <= 64 or m.shape != d.shape or errors.shape != d.shape:
raise ValueError('Use 1..64 corresponding prefix states')
for a in (d,m):
if not np.isfinite(a).all() or (a<0).any() or not np.isclose(a.sum(),1,rtol=0,atol=1e-12):
raise ValueError('Prefix probabilities must sum to one')
if not np.isfinite(errors).all() or ((errors<0)|(errors>1)).any():
raise ValueError('Conditional errors must be in [0,1]')
baseline = float(d@errors); rollout = float(m@errors)
return dict(data_error=baseline, model_error=rollout, gap=rollout-baseline,
ratio=None if baseline==0 else rollout/baseline,
ratio_status='undefined_zero_denominator' if baseline==0 else 'defined')
def demo():
schedules = []
gold = scheduled_joint(0)
for r in (0,.25,.5,.75,1):
joint = scheduled_joint(r); q = conditional_fit(joint)
schedules.append(dict(model_prefix_probability=r, training_joint=joint.tolist(),
q2=q.tolist(), correct_pair_probability=float(.5*np.trace(q)),
gold_second_token_nll=cross_entropy(gold,q),
perturbed_second_token_nll=cross_entropy(joint,q)))
return dict(epsilon=.1, steps=20,
no_recovery=deviation(.1,0).tolist(), recovery_point_four=deviation(.1,.4).tolist(),
periodic_boundary=deviation(1,1,steps=6).tolist(), schedules=schedules,
diagnostic=prefix_comparison([1,0],[.8,.2],[.1,.9]),
zero_denominator=prefix_comparison([1,0],[.8,.2],[0,.9]))
if __name__ == '__main__':
print(json.dumps(demo(), indent=2, allow_nan=False))
epsilon=0.1时,20步后无恢复的偏离概率为0.878423,rho=0.4时为0.1999998。从偏离很重的初态出发,轨迹可以下降。程序还故意保留周期边界,避免图形暗示所有参数都会收敛。
r依次取0、0.25、0.5、0.75、1时,真实第二词NLL分别为0、0.133531、0.287682、0.470004、0.693147。correct_pair_probability表示生成对落入“两个符号相等”的集合的概率,不是与另一条独立抽取参考逐句相同的概率。零分母比值不会被序列化成有效成绩。
检查穷举所有短Markov路径,独立枚举替换硬币及抽样符号,比较条件目标,并覆盖零质量和周期转移。递推需要O(T)时间及输出存储;只留最后状态时,额外空间可为常数。两符号联合计算规模固定。步数上限10,000、诊断前缀状态上限64,用来控制教学规模。
受控前缀实验仍要说明测的是什么任务
比较真实前缀时,应固定模型、提示、解码预算与续文长度。记录干预是提供真实前缀、打乱词序、插入随机噪声,还是使用模型自然输出;这些操作产生不同的前缀分布。更换前缀的同时也换模型,会混淆比较。
He等人在所研究的开放式生成设置中报告有限、非递增的偏离,并提出自恢复解释。这是限定范围的实证结果,不能保证机器翻译、事实一致性或任意模型同样如此。版本也需区分:本篇使用2021年9月的v10,而不是更早、题名不同的稿件。摘要与研究范围
生成不好还可能来自模型容量、优化、解码或不合适的评测目标。Scheduled sampling的直觉吸引力不能替代一个问题:改变前缀后,哪些监督标签仍然合适?反过来,这个二元反例也没有排除所有在生成上下文上训练的方法。应检验具体目标与干预,而不只看方法名称。
预测一项干预究竟能说明什么
- 在有限前缀诊断中,d=[1,0]、q=[0.8,0.2]、g=[0.1,0.9]。求D、R及差值。
答案:D=0.1,R=0.8×0.1+0.2×0.9=0.26,差为0.16。这隔离了固定条件误差的权重变化,而不是条件误差本身的变化。
- epsilon=0.1、rho=0.4、e0=0,计算e1与e2。极限是否为1?
答案:e1=0.1,e2=0.1+0.5×0.1=0.15。极限为0.1/0.5=0.2;恢复使这个特定状态概率不会趋向1。
- r=0.5时,拟合模型在前缀
0之后预测什么分布?真实第二词NLL是多少?
答案:预测[0.75,0.25]。原始数据总在0之后给出0,因此NLL为−log(0.75)=0.287682;原始精确条件分布的NLL则为0。
- 工程师把Markov状态命名为“之前某个词出过错”,却允许它恢复成“之前没有词出错”。需要修正什么?
答案:修正状态定义。历史上曾出错是不可逆事件;要研究恢复,就定义可恢复的当前上下文状态。若研究之前是否出现过任何错误,应保留吸收态。
分开看提案、批评和测得的后果
阅读Bengio等人了解输入混合方法,阅读Huszár了解理想化两符号批评,阅读He等人了解限定的实证前缀比较。所链接的Huszár v1注明当时正在ICLR 2016审稿,本篇不宣称它已获会议录用。这些来源都不能让本篇小Markov模型自动成为所有文本生成的经验证实模型。
先判断,再看解释
可恢复Markov状态能否定义为之前曾有某个词出错?
学习目标
- 比较不同前缀分布对固定条件误差的加权。
- 推导累积、恢复及周期边界。
- 枚举丢失依赖的采样前缀目标。
- 区分玩具反例与实证研究范围。
先修与延伸
查看知识关联图 →参考文献
- Scheduled Sampling for Sequence Prediction with Recurrent Neural Networks
Samy Bengio, Oriol Vinyals, Navdeep Jaitly, Noam Shazeer · 2015 - How (not) to Train your Generative Model: Scheduled Sampling, Likelihood, Adversary?
Ferenc Huszár · 2015 - Exposure Bias versus Self-Recovery: Are Distortions Really Incremental for Autoregressive Text Generation?
Tianxing He, Jingzhao Zhang, Zhiming Zhou, James Glass · 2021
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-12 · Single AI author checked six paired equations and four explained exercises against Bengio sampling, Huszar v1 two-symbol critique and scoped He v10 open-ended generation findings. Independent Markov-path and replacement-coin enumeration verify accumulation/recovery/periodic boundaries and fixed-fair-first-marginal sampled-prefix counterexample. Current deviation is not historical error occurrence; sampled-prefix r is opposite the source truth probability. Undefined ratios, gold NLL and label dependence checked. No argmax theorem, universal error law or real-model replication claimed.
- 单AI作者核六组公式和四解释习题,依据Bengio采样、Huszar v1两符号批评及限定He v10开放式生成发现。独立Markov路径与替换硬币枚举核累积/恢复/周期边界,以及固定公平首词边缘的采样前缀反例。当前偏离不等于历史错误,模型前缀r与来源真实概率互补。无定义比值、真实NLL及标签依赖已检;不宣称argmax定理、普遍错误规律或真实模型复现。
- 四张计算SVG绑定真实双语程序;本组八项数值/编码检查、24组语言/宽度及四字体检查通过,AI作者实际查看八张截图。冻结前检查普通正文AST;完整文章页面验收另记,不称人工同行审校。
核对的来源位置:
- Scheduled Sampling for Sequence Prediction with Recurrent Neural Networks · Abstract and sections2.1–2.4 excerpts, especially layout105–215: likelihood, EOS, inference and per-token mixing of true/generated inputs; gradients through sampled decisions not used. Experiments not reproduced.
- How (not) to Train your Generative Model: Scheduled Sampling, Likelihood, Adversary? · v1 dated2015-11-16, marked under review at ICLR2016. Section4.1/page4 visually read: idealized two-symbol sampled-prefix critique. Suspect equations7/8 not copied; independent finite derivation used. No argmax theorem or acceptance claim.
- Exposure Bias versus Self-Recovery: Are Distortions Really Incremental for Autoregressive Text Generation? · v10 dated2021-09-03; first submitted2019-05-25 under a different title. Abstract and scoped section5/6.1 excerpts: open-ended prefix comparisons and metric distinctions. Limited/non-incremental distortion is author-reported; no benchmark or Markov-model reproduction.
相关主题