AI Wiki中文/EN

资格迹:哪些较早访问还会分到这次误差?

Eligibility traces

在重复访问中比较累积迹与替换迹,再将真实TD误差沿两者传播。

本页目录直觉理解数学与方法代码实现局限与误解实验与练习研究脉络

回到同一状态,要再加一份信用吗?

学习器在终止前依次访问状态0、1、0。后来的TD误差到达时,状态0应该因两次访问各分到信用,还是让最近一次访问替换较早的痕迹?

资格迹(eligibility traces)为这个选择提供一小份记忆。折扣0.9、迹参数0.8时,旧迹每次转移衰减为原来的0.72。第三次访问时,状态0的累积迹为1 + 0.72² = 1.5184,替换迹则为1。这个数不是概率:累积迹可以大于一。

工作稿第7.3节介绍这种向后分配的机制,Silver讲义将衰减与访问的新近程度、重复次数联系起来。本文研究机制和真实更新;TD(lambda)另行解释它与未来回报混合的关系。

累积迹与替换迹对重复访问的记忆不同;最后一次TD误差基于相同的此前价值更新两种迹。
累积迹与替换迹对重复访问的记忆不同;最后一次TD误差基于相同的此前价值更新两种迹。

分配误差之前,先定迹的约定

使用表格价值函数、常数折扣gamma和迹参数lambda,两者都在[0,1]。记rho=gamma×lambda。每回合首次访问前将所有迹置零,e_t定义为从S_t离开的转移所产生TD误差使用的迹。累积迹满足:

e−1(s)=0,et(s)=ρet−1(s)+1[St=s],ρ=γλ.e_{-1}(s)=0,\qquad e_t(s)=\rho e_{t-1}(s)+\mathbf1[S_t=s],\qquad \rho=\gamma\lambda.

先衰减旧迹,再加入当前访问。展开递推,就能准确看出它记了什么:

et(s)=∑k=0tρt−k1[Sk=s].e_t(s)=\sum_{k=0}^{t}\rho^{t-k}\mathbf1[S_k=s].

每次较早访问都有自己的一份衰减贡献。迹不保存奖励与动作细节,只记录这份加权访问历史。重复状态例中,迹依次为[1,0]、[0.72,1]、[1.5184,0.72]。

替换迹只改变被访问分量的规则:

etrep(s)={1,s=St,ρet−1rep(s),s≠St.e_t^{\mathrm{rep}}(s)= \begin{cases} 1,&s=S_t,\\ \rho e_{t-1}^{\mathrm{rep}}(s),&s\ne S_t. \end{cases}

用I_t(s)标记截至时刻t是否已访问过该状态;存在访问时,令ell_t(s)为最近一次访问时刻。替换迹可以显式写为:

etrep(s)={0,It(s)=0,ρt−ℓt(s),It(s)=1.e_t^{\mathrm{rep}}(s)= \begin{cases} 0,&I_t(s)=0,\\ \rho^{t-\ell_t(s)},&I_t(s)=1. \end{cases}

当前访问的指数为零,值为1,rho为零也如此。替换迹记最近访问,不把全部访问相加;在这个非负表格环境中,数值始终位于零和一之间。Sutton与Barto区分了这两条规则,它们不是同一算法的两个名字。

将一次局部误差分给多个更新

用更新前的价值表计算一个TD误差,再按迹的大小更新所有非终止项:

δt=Rt+1+γVt(St+1)−Vt(St),Vt+1(s)=Vt(s)+αδtet(s).\delta_t=R_{t+1}+\gamma V_t(S_{t+1})-V_t(S_t),\qquad V_{t+1}(s)=V_t(s)+\alpha\delta_t e_t(s).

终止后继价值为零。必须先算一次delta,再改变各表项;不能遍历状态的途中重新计算delta。在这个意义上,各项更新是同时进行的。

有些写法先加当前访问、更新,再在本步末尾衰减迹;这样两次转移之间保存的是已衰减迹。只要索引一致,就与上方约定等价;若前后都衰减一次,就把时间算了两遍。van Seijen与Sutton对普通TD的讨论将常数步长并入迹,这里则把alpha放在外面。常数alpha下两者是等价缩放约定,不意味着步长变化时也能随意混用公式。

rho<1时,即使每步都访问同一状态,累积迹仍受几何和约束:

et(s)≤∑j=0tρj=1−ρt+11−ρ≤11−ρ.e_t(s)\leq\sum_{j=0}^{t}\rho^j =\frac{1-\rho^{t+1}}{1-\rho}\leq\frac{1}{1-\rho}.

rho为1时,迹改为累计访问次数,可以持续增长。替换迹把每个分量限制在一以内,但这不等于学习稳定性证明。alpha乘上很大的累积迹,会产生较大更新;迹是信用权重,不是各状态之间归一化后的分配概率。

在同一轨迹上运行两条规则

将程序保存为traces.py,用Python 3.12运行,只需标准库。完整原创回合为0 → 1 → 0 → 终止,奖励0、1.6、2,初值[1,2,0],alpha0.5、gamma0.9、lambda0.8。代码实现使用累积迹或替换迹的普通在线表格TD更新。

"""Original traces teaching example; synthetic episodes, not benchmark reproduction."""
import json
import math
import random


def unit(x, name):
    if isinstance(x, bool) or not isinstance(x, (int, float)) or not math.isfinite(x) or not 0 <= x <= 1:
        raise ValueError(name+' must be finite in [0,1]')


def sequence(states, rewards, values, terminated):
    if type(terminated) is not bool or len(values)<2 or any(not math.isfinite(x) for x in values) or values[-1]!=0:
        raise ValueError('finite values with a zero terminal component required')
    if not rewards or len(states)!=len(rewards)+1 or any(not math.isfinite(r) for r in rewards):
        raise ValueError('nonempty finite rewards and one extra state required')
    terminal=len(values)-1
    if any(type(s) is not int or not 0<=s<=terminal for s in states):
        raise ValueError('state index outside table')
    if terminal in states[:-1] or (states[-1]==terminal)!=terminated:
        raise ValueError('termination flag must match the final state only')


def trace_step(trace,state,gamma=.9,lam=.8,mode='accumulating'):
    unit(gamma,'gamma');unit(lam,'lambda')
    if mode not in ('accumulating','replacing'):raise ValueError('unknown trace convention')
    if not trace or type(state) is not int or not 0<=state<len(trace) or any(not math.isfinite(e) or e<0 for e in trace):raise ValueError('valid nonnegative trace required')
    out=[gamma*lam*e for e in trace]
    out[state]=out[state]+1 if mode=='accumulating' else 1.0
    return out


def learn_episode(states,rewards,initial,alpha=.5,gamma=.9,lam=.8,mode='accumulating'):
    unit(alpha,'alpha');unit(gamma,'gamma');unit(lam,'lambda');sequence(states,rewards,initial,True)
    if mode not in ('accumulating','replacing'):raise ValueError('unknown trace convention')
    v=list(initial);e=[0.]*(len(v)-1);history=[]
    for t,r in enumerate(rewards):
        s=states[t];delta=r+gamma*v[states[t+1]]-v[s];e=trace_step(e,s,gamma,lam,mode)
        for i in range(len(e)):v[i]+=alpha*delta*e[i]
        history.append(dict(time=t,delta=delta,trace=e.copy(),values=v.copy()))
    return dict(values=v,history=history)


def demo():
    states=[0,1,0,2];rewards=[0,1.6,2];initial=[1,2,0]
    return dict(results={mode:learn_episode(states,rewards,initial,mode=mode) for mode in ['accumulating','replacing']},
                lambda_zero=learn_episode(states,rewards,initial,lam=0),
                next_episode=learn_episode([1,2],[4],[0,0,0]),decay=.9*.8)


if __name__=='__main__':
    print(json.dumps(demo(),indent=2,allow_nan=False))

第一次误差0.8,将状态0改为1.4。第二次误差1.6+0.9×1.4−2=0.86,两个版本的值都变成[1.7096,2.43,0]。最后更新前,两者误差仍同为2−1.7096=0.2904,但状态0的迹不同:累积最终得到[1.93007168,2.534544,0],替换得到[1.8548,2.534544,0]。

差异出现在重复访问处;本例在那之前的迹规则结果相同。替换也不是“先算累积更新,再把结果截断”,而是在分配误差之前改变迹。

lambda为零时,仅当前状态有非零迹,结果[1.7,2.43,0]与顺序TD(0)一致。新回合要重置迹。另一个1 → 终止回合奖励4、初值全零,只把状态1改成2;状态0不会因上一个回合而分到信用。

S个表格状态、T次转移时,稠密迹衰减与价值更新耗时O(TS),工作空间O(S)。本演示还记录每次迹和价值表,输出存储为O(TS)。稀疏实现可以删掉很小的迹,但这种近似会改变精确更新。程序要求完整回合,拒绝终止状态不一致的输入,不静默把截断当终止。

迹必须与使用它的学习器匹配

累积和替换在重复访问、较大步长下可能表现不同,没有“替换总是更好”的通用结论。迹分量有界,不等于价值误差有界;迹较大也不必然代表证据更可靠。

这里是非负表格状态迹。动作价值迹、异策略修正和特征梯度迹都需要自己的定义。不能由这个表格例子推出可以逐分量替换任意神经网络特征。Dutch迹和true-online TD还有额外约定;只换一行迹递推,并不等于实现van Seijen与Sutton的完整true-online方法。

普通在线累积迹通常不重现冻结价值的离线lambda回报更新。等价关系需要下一篇讨论的条件,不能只因为用了同一个lambda符号就成立。

先追踪访问,再分配误差

  1. 累积迹1.5184超过一,是否非法?

    答案:不是。它相加了衰减访问,是信用权重,不是概率,也不是总信用中归一化的一份。

  2. rho0.72、访问0、1、0,计算两种最终迹。

    答案:累积迹的状态0为1+0.72²=1.5184、状态1为0.72;替换将再次访问的状态0设为1,状态1仍为0.72。

  3. gamma和lambda都为1,反复访问状态0会怎样?

    答案:累积迹每次加一,不再有有限几何界;替换迹每次访问后仍为一。两者本身都不能证明价值学习过程收敛。

  4. 实现把迹带过独立重置回合,怎样用最小测试暴露错误?

    答案:让一个回合结束时状态0仍有迹,再开启只访问状态1、收到奖励并终止的新回合。正确重置后,状态0不应从第二回合得到更新。

索引和缩放也属于算法

工作稿给出累积/替换规则,区分在线与离线;Silver讲义解释其向后视角;van Seijen与Sutton2014在引入另一种在线等价之前,讨论了普通资格迹。这里仅核其引言框架和普通方法页,没有冒充复现后文true-online算法或实验。

先判断,再看解释

再次访问某状态时,它衰减后的迹为0.5184;替换迹会变成多少?

选择一个答案

学习目标

  • 从访问历史推导表格资格迹,区分累积与替换。
  • 按正确顺序执行迹加权TD更新,在新回合重置资格迹。

先修与延伸

查看知识关联图 →

参考文献

  1. Reinforcement Learning: An Introduction — second-edition working draft (2014–2015)
    Richard S. Sutton; Andrew G. Barto · 2015
  2. Lecture 4: Model-Free Prediction
    David Silver · 2015
  3. True Online TD(lambda)
    Harm van Seijen; Richard S. Sutton · 2014

版本1 · 编写状态: 已完成记录中的技术复核

查看技术检查记录(AI)

Codex · 2026-09-23 · Single AI author. Scoped reading: PDF191–192 §7.3/Fig7.7 decay, accumulating traces, TD updates; PDF194–195 replacing traces and online/offline distinction. Read2026-09-23. Author teaching page identifies the 2015 course; 2025 upload path is not the lecture year. Read PDF1,3–7,10–13,16–18,29,32,35,37,40–45. MC visit conventions, incremental means, TD targets, n-step returns, accumulating traces and explicitly offline equivalence. General bias/variance summaries are teaching heuristics, not unconditional variance or efficiency theorems. No slides/images copied. Read PDF1–3, with PDF2–3 visually inspected, and PMLR publication metadata. Frozen-within-episode forward/backward equality, changed online estimates, conventional accumulating/replacing traces and alpha folded into the paper's trace. New online equivalence is an author claim from the introduction; algorithm/proof later in the paper and empirical comparisons were not audited or implemented here. 6 paired equations and four explained exercises. Checked accumulating versus replacing traces at repeated states, nonzero initial values, shared final TD errors, lambda zero, trace decay and episode reset. Tabular conventional traces only; no Dutch trace or true-online implementation.

  • 核重复状态累积/替换迹、非零初值、相同最终TD误差、lambda零、迹衰减与回合重置;仅表格普通迹,没有Dutch迹或true-online实现。
  • 每题四张计算SVG,12数值/几何与36响应式组合通过;12张当前选定图均直接实看,浏览器输入由插图前SHA绑定,完整页面验收另记。

核对的来源位置:

  • Reinforcement Learning: An Introduction — second-edition working draft (2014–2015) · PDF191–192 §7.3/Fig7.7 decay, accumulating traces, TD updates; PDF194–195 replacing traces and online/offline distinction. Read2026-09-23.
  • Lecture 4: Model-Free Prediction · Author teaching page identifies the 2015 course; 2025 upload path is not the lecture year. Read PDF1,3–7,10–13,16–18,29,32,35,37,40–45. MC visit conventions, incremental means, TD targets, n-step returns, accumulating traces and explicitly offline equivalence. General bias/variance summaries are teaching heuristics, not unconditional variance or efficiency theorems. No slides/images copied.
  • True Online TD(lambda) · Read PDF1–3, with PDF2–3 visually inspected, and PMLR publication metadata. Frozen-within-episode forward/backward equality, changed online estimates, conventional accumulating/replacing traces and alpha folded into the paper's trace. New online equivalence is an author claim from the introduction; algorithm/proof later in the paper and empirical comparisons were not audited or implemented here.

相关主题

没有匹配结果,试试英文名或缩写。

已显示 0 条结果

将整个查询放入双引号,可按短语匹配。