引言 / Introduction
Eligibility traces are one of the basic mechanisms of reinforcement learning. They unify and generalize TD and Monte Carlo methods, producing a family of methods spanning from Monte Carlo (λ=1) to one-step TD (λ=0). The mechanism is a short-term memory vector zt ∈ ℝd that parallels the long-term weight vector wt.
资格迹是强化学习的基本机制之一。它统一并泛化了TD和蒙特卡罗方法,产生从蒙特卡罗(λ=1)到单步TD(λ=0)的方法族。其机制是一个短期记忆向量zt∈ℝd,与长期权重向量wt并行。
The primary computational advantage: only a single trace vector is required rather than a store of the last n feature vectors. Learning occurs continually and uniformly in time rather than being delayed. The forward view looks ahead to future rewards; the backward view uses eligibility traces to assign credit to past states.
主要计算优势:只需要单个迹向量而非存储最近n个特征向量。学习在时间上持续均匀进行而非延迟。
前向视图展望未来奖励;后向视图使用资格迹将功劳分配给过去的状态。
12.1 λ-回报 / The λ-return
A valid update can be done toward any average of n-step returns for different n's, as long as weights are positive and sum to 1. Such an update is called a compound update. The TD(λ) algorithm averages all n-step updates, each weighted proportionally to λn−1, normalized by (1−λ).
只要权重为正且总和为1,就可以对不同n的n步回报进行平均来执行有效更新。这种更新称为复合更新。TD(λ)算法对所有n步更新进行平均,每个更新按λn−1加权,并用(1−λ)归一化。
Gtλ = (1−λ) Σn=1∞ λn−1 Gt:t+n
When λ=1, the λ-return reduces to the conventional Monte Carlo return Gt. When λ=0, it reduces to the one-step return Gt:t+1. The offline λ-return algorithm makes updates at the end of the episode using this target:
当λ=1时,λ-回报退化为传统蒙特卡罗回报Gt。当λ=0时,退化为单步回报Gt:t+1。离线λ-回报算法在回合结束时使用此目标进行更新:
wt+1 = wt + α [Gtλ − v̂(St, wt)] ∇v̂(St, wt)
12.2 TD(λ)
TD(λ) improves over the offline λ-return algorithm in three ways: (1) updates on every step, (2) computations equally distributed in time, (3) applicable to continuing problems. The eligibility trace is initialized to zero, incremented by the value gradient, then fades:
TD(λ)在三个方面改进离线λ-回报算法:(1)每步更新,(2)计算在时间上均匀分布,(3)适用于持续性任务。资格迹初始化为零,按价值梯度递增,然后衰减:
z−1 = 0
zt = γλzt−1 + ∇v̂(St, wt)
The TD error: δt = Rt+1 + γv̂(St+1, wt) − v̂(St, wt). The weight update:
TD误差:δt=Rt+1+γv̂(St+1,wt)−v̂(St,wt)。权重更新:
wt+1 = wt + αδtzt
Semi-gradient TD(λ) for estimating v̂ ≈ vπ
────────────────────────────────────────────
Input: policy π, differentiable v̂: S⁺ × ℝd → ℝ
Parameters: step size α > 0, trace decay rate λ ∈ [0,1]
Initialize w arbitrarily
Loop for each episode:
Initialize S
z ← 0 (d-dimensional vector)
Loop for each step:
Choose A ~ π(·|S)
Take action A, observe R, S'
z ← γλz + ∇v̂(S, w)
δ ← R + γv̂(S', w) − v̂(S, w)
w ← w + αδz
S ← S'
until S' is terminal
If λ=0, TD(λ) reduces to one-step TD(0). For λ=1 (TD(1)), traces don't decay → Monte Carlo behavior for undiscounted episodic tasks. TD(1) can be performed incrementally and online, unlike classical MC methods. Linear TD(λ) converges on-policy with VE bound:
如果λ=0,TD(λ)退化为单步TD(0)。当λ=1(TD(1))时,迹不衰减→无折扣回合任务的蒙特卡罗行为。TD(1)可以增量在线执行,不同于经典MC方法。线性TD(λ)在on-policy下收敛,VE界:
VE(w∞) ≤ (1−γλ)/(1−γ) · minw VE(w)
12.3 n步截断λ-回报方法 / n-step Truncated λ-return Methods
The offline λ-return is not known until episode end. We can truncate after n steps, replacing missing rewards with estimated values:
离线λ-回报直到回合结束才知道。可以在n步后截断,用估计值替代缺失奖励:
Gt:hλ = (1−λ) Σn=1h−t−1 λn−1 Gt:t+n + λh−t−1 Gt:h
This gives rise to Truncated TD(λ) (TTD(λ)). The k-step λ-return can be written as:
这产生了截断TD(λ)(TTD(λ))。k步λ-回报可写为:
Gt:t+kλ = v̂(St, wt−1) + Σi=tt+k−1 (γλ)i−t δ'i
where δ't = Rt+1 + γv̂(St+1, wt) − v̂(St, wt−1). Efficient implementation: per-step computation does not scale with n.
其中δ't=Rt+1+γv̂(St+1,wt)−v̂(St,wt−1)。高效实现:逐步计算不随n缩放。
12.4 重做更新:在线λ-回报算法 / Online λ-return Algorithm
Idea: on each time step, go back and redo all updates since the beginning of the episode with the new data. Always use the latest horizon for the truncated λ-return target. Multiple passes over the episode, each at a different horizon h:
思想:在每个时间步,回溯并重做自回合开始以来的所有更新,使用新数据。始终使用最新截断λ-回报目标。对回合进行多次遍历,每次在不同水平h:
wt+1h = wth + α [Gt:hλ − v̂(St, wth)] ∇v̂(St, wth)
Fully online but computationally complex — passes over the episode on every step. Performs subtly better than the offline algorithm at the end of the episode because bootstrapping uses a more informed weight vector.
完全在线但计算复杂——每步都遍历回合。在回合结束时比离线算法表现略好,因为自举使用了更有信息的权重向量。
12.5 真正的在线TD(λ) / True Online TD(λ)
The online λ-return algorithm is currently the best performing TD algorithm. For linear function approximation, there is an exact efficient implementation called True Online TD(λ):
在线λ-回报算法是目前表现最好的TD算法。对于线性函数近似,有一个精确高效实现称为真正的在线TD(λ):
wt+1 = wt + αδtzt + α(wtTxt − wt−1Txt)(zt − xt)
zt = γλzt−1 + (1 − αλzt−1Txt)xt
True Online TD(λ) for estimating wTx ≈ vπ
────────────────────────────────────────────
Input: policy π, feature function x: S⁺ → ℝd
Parameters: step size α > 0, trace decay rate λ ∈ [0,1]
Initialize w ∈ ℝd
Loop for each episode:
Initialize state, get feature vector x
z ← 0, Vold ← 0
Loop for each step:
Choose A ~ π, take A, observe R, x'
V ← wTx, V' ← wTx'
δ ← R + γV' − V
z ← γλz + (1 − αλzTx)x
w ← w + α(δ + V − Vold)z − α(V − Vold)x
Vold ← V', x ← x'
until x' = 0 (terminal)
The eligibility trace (12.11) is called a dutch trace. It produces exactly the same weight vector sequence as the online λ-return algorithm with O(d) per-step complexity (50% more than TD(λ)). Earlier replacing traces are crude approximations to dutch traces.
资格迹(12.11)称为dutch迹。它产生与在线λ-回报算法完全相同的权重向量序列,逐步复杂度O(d)(比TD(λ)多50%)。早期的替换迹是dutch迹的粗略近似。
💡 三种迹类型:
累积迹(TD(λ)使用)、Dutch迹(真正的在线TD(λ)使用,理论基础最好)、替换迹(仅适用于表格/二值特征,已被dutch迹取代)。
12.6 *蒙特卡罗学习中的Dutch迹 / Dutch Traces in MC Learning
Eligibility traces arise even in Monte Carlo learning — they have nothing specific to do with TD. The linear MC algorithm (LMS rule) makes updates only at episode end. By algebraic manipulation, we can derive an equivalent incremental algorithm using dutch traces with O(d) per-step complexity:
资格迹甚至出现在蒙特卡罗学习中——与TD无特定关系。线性MC算法(LMS规则)仅在回合结束时更新。通过代数推导,可以得到使用dutch迹的等价增量算法,逐步复杂度O(d):
wT = aT−1 + αGzT−1
where at and zt are auxiliary vectors updated incrementally. The trace zt is exactly the dutch trace with γλ=1. This shows eligibility traces are more fundamental than TD — they arise whenever one tries to learn long-term predictions efficiently.
其中at和zt是增量更新的辅助向量。迹zt恰好是γλ=1时的dutch迹。这表明资格迹比TD更基本——只要试图高效学习长期预测就会出现。
12.7 Sarsa(λ)
Extension to action values uses the action-value form of n-step returns. Sarsa(λ) has the same update rule as TD(λ): wt+1 = wt + αδtzt, but with action-value TD error and eligibility trace:
向动作价值的扩展使用n步回报的动作价值形式。Sarsa(λ)具有与TD(λ)相同的更新规则:wt+1=wt+αδtzt,但使用动作价值TD误差和资格迹:
δt = Rt+1 + γq̂(St+1, At+1, wt) − q̂(St, At, wt)
zt = γλzt−1 + ∇q̂(St, At, wt)
Sarsa(λ) with binary features and linear function approximation
────────────────────────────────────────────
Input: feature function F(s,a), policy π
Parameters: α > 0, λ ∈ [0,1]
Initialize w, z ∈ ℝd
Loop for each episode:
Initialize S, choose A ~ π(·|S) or ε-greedy
z ← 0
Loop for each step:
Take A, observe R, S'
δ ← R
For i in F(S, A): δ ← δ − wi; zi ← zi + 1 (or 1 for replacing)
If S' terminal: w ← w + αδz → next episode
Choose A' ~ π(·|S') or ε-greedy
For i in F(S', A'): δ ← δ + γwi
w ← w + αδz
z ← γλz
S ← S', A ← A'
Example 12.1: Traces in Gridworld. One-step methods increment only the last action value; n-step methods equally increment the last n actions; eligibility trace methods update all action values with fading credit. The fading strategy is often the best.
例12.1:Gridworld中的迹。单步方法只增加最后一个动作价值;n步方法均匀增加最后n个动作;资格迹方法以衰减功劳更新所有动作价值。衰减策略通常最好。
True Online Sarsa(λ) extends True Online TD(λ) to action values using state-action features xt=x(St,At). It outperforms regular Sarsa(λ) with both accumulating and replacing traces on Mountain Car.
真正的在线Sarsa(λ)使用状态-动作特征xt=x(St,At)将真正的在线TD(λ)扩展到动作价值。在Mountain Car上优于使用累积迹和替换迹的常规Sarsa(λ)。
12.8 可变λ和γ / Variable λ and γ
Generalize bootstrapping and discounting to state-action dependent functions: λt=λ(St,At) and γt=γ(St). The termination function γ(s) is particularly significant — it changes the return itself:
将自举和折扣泛化为状态-动作相关函数:λt=λ(St,At)和γt=γ(St)。终止函数γ(s)特别重要——它改变了回报本身:
Gt = Rt+1 + γt+1Rt+2 + γt+1γt+2Rt+3 + ···
A terminal state becomes a state where γ(s)=0, transitioning to the start distribution. This unifies episodic and discounted-continuing cases. The state-based λ-return becomes recursive:
终止状态变为γ(s)=0的状态,转移到起始分布。这统一了回合制和折扣持续性情况。基于状态的λ-回报变为递归:
Gtλs = Rt+1 + γt+1[(1−λt+1)v̂(St+1,wt) + λt+1Gt+1λs]
The action-based λ-return comes in Sarsa form or Expected Sarsa form using V̄t(s) = Σa π(a|s)q̂(s,a,wt).
基于动作的λ-回报有Sarsa形式或Expected Sarsa形式,使用V̄t(s)=Σaπ(a|s)q̂(s,a,wt)。
12.9 *带控制变量的Off-policy迹 / Off-policy Traces with Control Variates
Incorporate importance sampling into the λ-return. The final state-based λ-return generalizes to off-policy:
将重要性采样纳入λ-回报。最终的基于状态的λ-回报泛化到off-policy:
Gtλs = ρt[Rt+1 + γt+1((1−λt+1)v̂t+1 + λt+1Gt+1λs)] + (1−ρt)v̂t
The approximate sum-of-TD-errors form leads to the general accumulating trace update for state values:
TD误差之和的近似形式导致状态价值的通用累积迹更新:
zt = ρt(γtλtzt−1 + ∇v̂(St, wt))
For action values (Expected Sarsa form), the eligibility trace is:
对于动作价值(Expected Sarsa形式),资格迹为:
zt = γtλtρtzt−1 + ∇q̂(St, At, wt)
⚠️ 注意:如果λ<1,所有这些off-policy算法都涉及自举,致命三要素适用(第11.3节),意味着它们仅在表格情况和状态聚合下保证稳定。
12.10 Watkins的Q(λ)到Tree-Backup(λ)
Watkins's Q(λ): decays traces as long as greedy actions are taken, then cuts traces to zero after the first non-greedy action. Tree-Backup(λ) (TB(λ)): the true successor to Q-learning, retaining the absence of importance sampling:
Watkins的Q(λ):只要采取贪婪动作就衰减迹,在第一个非贪婪动作后将迹截为零。Tree-Backup(λ)(TB(λ)):Q-learning的真正继承者,保持不使用重要性采样:
Gtλa = Rt+1 + γt+1[V̄t(St+1) + λt+1π(At+1|St+1)(Gt+1λa − q̂(St+1,At+1,wt))]
The TB(λ) eligibility trace uses target-policy probabilities:
TB(λ)资格迹使用目标策略概率:
zt = γtλtπ(At|St)zt−1 + ∇q̂(St, At, wt)
12.11 稳定的Off-policy迹方法 / Stable Off-policy Methods with Traces
Four key algorithms achieve stability guarantees under off-policy training (all assume linear function approximation):
四个关键算法在off-policy训练下实现稳定性保证(均假设线性函数近似):
| 算法 | 类型 | 关键特点 |
| GTD(λ) | 状态价值 Gradient-TD | TDC的资格迹版本;需要辅助向量v和第二长β |
| GQ(λ) | 动作价值 Gradient-TD | 用于off-policy控制;使用平均特征向量x̄t |
| HTD(λ) | 混合状态价值 | GTD(λ)和TD(λ)的混合;当b=π时退化为TD(λ) |
| Emphatic TD(λ) | 强调TD | 保留强off-policy收敛保证;使用followon迹Ft和emphasis Mt |
HTD(λ) is appealing because it is a strict generalization of TD(λ): if behavior=target policy, HTD(λ) becomes TD(λ) (unlike GTD(λ)). Emphatic TD(λ) uses emphasis Mt=γtIt+(1−λt)Ft and followon trace Ft=ρt−1γtFt−1+It.
HTD(λ)有吸引力因为它是TD(λ)的严格泛化:如果行为=目标策略,HTD(λ)变为TD(λ)(GTD(λ)不是)。Emphatic TD(λ)使用emphasis Mt=γtIt+(1−λt)Ft和followon迹Ft=ρt−1γtFt−1+It。
12.12 实现问题 / Implementation Issues
For tabular methods, a naive implementation seems complex (update all states every step). But for typical γ and λ, almost all traces are near zero — only recently visited states have significant traces. In practice, only update the few significant traces → expense is just a few times that of one-step methods.
对于表格方法,朴素实现看似复杂(每步更新所有状态)。但对于典型的γ和λ,几乎所有迹都接近零——只有最近访问的状态有显著迹。实践中只更新少数显著迹→开销仅为单步方法的几倍。
With function approximation (e.g., ANNs), eligibility traces generally cause only a doubling of memory and computation. The tabular case is the worst case for trace complexity.
使用函数近似(如ANN),资格迹通常只导致内存和计算翻倍。表格情况是迹复杂度的最坏情况。
12.13 结论 / Conclusions
Eligibility traces + TD errors provide an efficient, incremental way of shifting between MC and TD methods. Key takeaways:
资格迹+TD误差提供了在MC和TD方法之间切换的高效增量方式。关键要点:
- Intermediate λ typically performs best — brings us toward MC but not all the way
- True online methods exactly reproduce expensive ideal methods with O(d) complexity
- Eligibility traces are the first line of defense against long-delayed rewards and non-Markov tasks
- Use traces when data is scarce (online); skip when data is cheap (offline simulation)
- The elegant theory connects forward views ↔ backward views via trace equivalences
- 中间λ通常表现最好——趋向MC但不到达
- 真正的在线方法以O(d)复杂度精确重现昂贵的理想方法
- 资格迹是对抗长延迟奖励和非Markov任务的第一道防线
- 数据稀缺时使用迹(在线);数据廉价时跳过(离线仿真)
- 优雅的理论通过迹等价连接前向视图↔后向视图
📋 核心概念速查 / Key Concepts Summary
| 概念 / Concept | 定义 / Definition |
| 资格迹 Eligibility Trace | 短期记忆向量zt,跟踪哪些权重分量对最近状态估值有贡献 |
| λ-回报 | 所有n步回报的几何加权平均:Gλ=(1−λ)Σλn−1Gt:t+n |
| 前向视图 Forward View | 从当前状态展望未来奖励来确定更新目标 |
| 后向视图 Backward View | 用当前TD误差配合资格迹回溯更新过去状态 |
| Dutch迹 | zt=γλzt−1+(1−αλzt−1Txt)xt,用于真正的在线TD(λ) |
| 累积迹 Accumulating | zt=γλzt−1+∇v̂,经典TD(λ)使用 |
| 替换迹 Replacing | 特征为1时重置为1,否则衰减;已被dutch迹取代 |
| 复合更新 Compound Update | 多个n步回报的加权平均 |
| 终止函数 γ(s) | 状态相关的折扣,统一回合制和持续性设定 |
| 致命三要素与迹 | λ<1时off-policy迹方法仍涉及自举,需要稳定化技术 |