Chapter 6: Temporal-Difference Learning

第六章:时序差分学习

📑 目录 / Table of Contents

引言 / Introduction

If one had to identify one idea as central and novel to reinforcement learning, it would undoubtedly be temporal-difference (TD) learning. TD learning is a combination of Monte Carlo ideas and dynamic programming (DP) ideas. Like MC methods, TD methods can learn directly from raw experience without a model. Like DP, TD methods update estimates based in part on other learned estimates (they bootstrap).
如果要找出强化学习中一个核心且新颖的思想,那毫无疑问是时序差分(TD)学习。TD学习是蒙特卡洛思想和动态规划(DP)思想的结合。像MC方法一样,TD方法可以直接从原始经验中学习,无需模型。像DP一样,TD方法部分基于其他学习到的估计来更新估计(它们自举)。
MC方法
从完整回报学习
不依赖模型
不自举
TD方法
从TD目标学习
不依赖模型
自举
DP方法
从期望更新学习
需要模型
自举

6.1 TD预测 / TD Prediction

Monte Carlo methods wait until the return is known, then use Gt as target. TD methods need wait only until the next time step. At time t+1 they immediately form a target using Rt+1 and V(St+1). The simplest TD method, TD(0), makes the update:
蒙特卡洛方法等到回报已知后,用Gt作为目标。TD方法只需等到下一个时间步。在t+1时刻,它们立即使用Rt+1和V(St+1)形成目标。最简单的TD方法TD(0)执行如下更新:
V(St) ← V(St) + α [ Rt+1 + γV(St+1) − V(St) ]
表格型TD(0),估计V ≈ vπ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 输入:待评估策略 π;步长 α ∈ (0,1] 初始化:V(s) 任意值(V(终止)=0) 对每个回合循环: 初始化 S 对回合中每一步循环: A ← π(S) 给出的动作 执行动作 A,观察 R, S' V(S) ← V(S) + α[R + γV(S') − V(S)] S ← S' 直到 S 为终止状态
The quantity in brackets is the TD error, measuring the difference between the estimated value and the better estimate:
方括号中的量是TD误差,衡量估计值与更好估计之间的差异:
δt = Rt+1 + γV(St+1) − V(St)
Example: Driving Home. You estimate 30 minutes to get home. After reaching your car (5 min later, raining), you reestimate 35 more minutes. TD updates immediately shift your initial estimate toward 40 minutes, without waiting to actually arrive home.
例:开车回家。你估计30分钟到家。到达车旁时(5分钟后,下雨),你重新估计还需35分钟。TD会立即将你的初始估计向40分钟方向调整,而不需要等到实际到家。
💡 MC误差 = TD误差之和:如果V在回合内不变,MC误差可以写成TD误差的折扣和:Gt − V(St) = Σk=tT-1 γk-t δk

6.2 TD预测方法的优势 / Advantages of TD Prediction

TD methods have advantages over DP (no model needed) and over MC (online, fully incremental — no need to wait for episode completion). This is critical for very long episodes or continuing tasks with no episodes.
TD方法相比DP的优势(不需要模型),相比MC的优势(在线、完全增量——无需等待回合完成)。这对于非常长的回合或没有回合的持续性任务至关重要。
Convergence: For any fixed policy π, TD(0) converges to vπ — in the mean for constant α, and with probability 1 if α decreases according to standard conditions.
收敛性:对于任何固定策略π,TD(0)收敛到vπ——对常数α均值收敛,若α按标准条件递减则以概率1收敛。
Example 6.2: Random Walk. In a 5-state MRP (A-E), start at C, move left/right with equal probability. Terminate on left (reward 0) or right (reward +1). True values: 1/6, 2/6, 3/6, 4/6, 5/6. TD(0) consistently outperforms constant-α MC in convergence speed.
例6.2:随机游走。在一个5状态MRP(A-E)中,从C开始,等概率左/右移动。在最左终止(奖励0)或最右终止(奖励+1)。真实值:1/6, 2/6, 3/6, 4/6, 5/6。TD(0)在收敛速度上始终优于常数α MC。

6.3 TD(0)的最优性 / Optimality of TD(0)

Under batch updating (repeatedly processing a finite set of experience), batch MC converges to estimates minimizing mean-squared error on training data. Batch TD(0) converges to the certainty-equivalence estimate — the true values of the maximum-likelihood model of the Markov process.
在批量更新(反复处理有限经验集)下,批量MC收敛到最小化训练数据均方误差的估计。批量TD(0)收敛到确定性等价估计——即马尔可夫过程最大似然模型的真实值。
Example 6.4: Given 8 episodes where B→1 six times and B→0 twice, V(B)=3/4. For state A (which always transitions to B with reward 0): batch TD gives V(A)=3/4 (using the model), while batch MC gives V(A)=0 (the observed return). The TD answer generalizes better.
例6.4:给定8个回合,B→1出现6次,B→0出现2次,V(B)=3/4。对于状态A(总是以奖励0转移到B):批量TD给出V(A)=3/4(利用模型),而批量MC给出V(A)=0(观察到的回报)。TD的答案泛化性更好。
💡 关键结论:批量MC最小化训练集上的均方误差,而批量TD(0)计算确定性等价估计。TD方法收敛更快,因为它朝着更好的估计方向移动。

6.4 Sarsa:On-policy TD控制 / On-policy TD Control

For control, we learn action-value function Q(s,a). Sarsa updates Q based on the quintuple (St, At, Rt+1, St+1, At+1):
对于控制问题,我们学习动作价值函数Q(s,a)。Sarsa基于五元组(St, At, Rt+1, St+1, At+1)更新Q:
Q(St, At) ← Q(St, At) + α [ Rt+1 + γQ(St+1, At+1) − Q(St, At) ]
Sarsa(on-policy TD控制),估计 Q ≈ q* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 参数:步长 α ∈ (0,1],小 ε > 0 初始化:Q(s,a) 任意值(Q(终止,·)=0) 对每个回合循环: 初始化 S 用Q导出策略选择 A(如 ε-贪心) 对每一步循环: 执行 A,观察 R, S' 用Q导出策略从 S' 选择 A' Q(S,A) ← Q(S,A) + α[R + γQ(S',A') − Q(S,A)] S ← S'; A ← A' 直到 S 终止
Example 6.5: Windy Gridworld. A gridworld with a crosswind pushing the agent upward. Sarsa learns the optimal policy. MC methods cannot easily be used here because termination is not guaranteed for all policies.
例6.5:有风网格世界。一个有向上交叉风的网格世界。Sarsa学习到最优策略。MC方法在这里不容易使用,因为不能保证所有策略都能终止。

6.5 Q-learning:Off-policy TD控制 / Off-policy TD Control

Q-learning is a breakthrough off-policy algorithm. The learned Q directly approximates q*, independent of the policy being followed:
Q-learning是一个突破性的off-policy算法。学习到的Q直接逼近q*,与所遵循的策略无关:
Q(St, At) ← Q(St, At) + α [ Rt+1 + γ maxa Q(St+1, a) − Q(St, At) ]
Q-learning(off-policy TD控制),估计 π ≈ π* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 参数:步长 α ∈ (0,1],小 ε > 0 初始化:Q(s,a) 任意值 对每个回合循环: 初始化 S 对每一步循环: 用Q导出策略选择 A(如 ε-贪心) 执行 A,观察 R, S' Q(S,A) ← Q(S,A) + α[R + γ max_a Q(S',a) − Q(S,A)] S ← S' 直到 S 终止
Example 6.6: Cliff Walking. Compares Sarsa (on-policy) vs Q-learning (off-policy). Q-learning learns the optimal path along the cliff edge, but occasionally falls off due to ε-greedy exploration. Sarsa learns a safer, longer path away from the cliff. Despite Q-learning learning optimal values, its online performance is worse than Sarsa.
例6.6:悬崖行走。比较Sarsa(on-policy)和Q-learning(off-policy)。Q-learning学习沿悬崖边缘的最优路径,但由于ε-贪心探索偶尔会掉下悬崖。Sarsa学习更安全但更长的远离悬崖的路径。虽然Q-learning学到了最优值,但其在线表现不如Sarsa。

6.6 Expected Sarsa

Expected Sarsa uses the expected value over next actions instead of the maximum (Q-learning) or the sampled action (Sarsa):
Expected Sarsa使用下一动作的期望值,而非最大值(Q-learning)或采样的动作(Sarsa):
Q(St,At) ← Q(St,At) + α [ Rt+1 + γ Σa π(a|St+1)Q(St+1,a) − Q(St,At) ]
Expected Sarsa moves deterministically in the same direction Sarsa moves in expectation. It eliminates variance from random action selection. When π is greedy and behavior is exploratory, Expected Sarsa becomes Q-learning. It subsumes and generalizes Q-learning while improving over Sarsa.
Expected Sarsa确定性地沿着Sarsa期望移动的方向移动。它消除了随机动作选择带来的方差。当π是贪心的且行为更具探索性时,Expected Sarsa就变成了Q-learning。它涵盖并泛化了Q-learning,同时改进了Sarsa。

6.7 最大化偏差与双重学习 / Maximization Bias & Double Learning

All control algorithms involve maximization in constructing target policies. Using a maximum of estimates as an estimate of the maximum value introduces a positive (maximization) bias. When true values are all zero but estimates are noisy, the maximum estimate is positive while the true maximum is zero.
所有控制算法都在构建目标策略时涉及最大化。用估计值的最大值作为最大值的估计会引入正偏差(最大化偏差)。当真实值全为零但估计值有噪声时,最大估计值为正,而真实最大值为零。
Double learning solves this by maintaining two independent estimates Q₁ and Q₂. Use Q₁ to determine the maximizing action, and Q₂ to estimate its value (and vice versa):
双重学习通过维护两个独立估计Q₁和Q₂来解决此问题。用Q₁确定最大化的动作,用Q₂估计其值(反之亦然):
Q₁(St,At) ← Q₁(St,At) + α [ Rt+1 + γ Q₂(St+1, argmaxa Q₁(St+1,a)) − Q₁(St,At) ]
Double Q-learning,估计 Q₁ ≈ Q₂ ≈ q* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 初始化:Q₁(s,a) 和 Q₂(s,a) 对每个回合的每一步: 用 Q₁+Q₂ 的 ε-贪心策略选择 A 执行 A,观察 R, S' 以 0.5 概率: Q₁(S,A) ← Q₁(S,A) + α[R + γQ₂(S', argmax Q₁(S',·)) − Q₁(S,A)] 否则: Q₂(S,A) ← Q₂(S,A) + α[R + γQ₁(S', argmax Q₂(S',·)) − Q₂(S,A)]
⚠️ 例6.7:Q-learning在最大化偏差示例中强烈偏好次优动作(left),即使其期望回报为负。Double Q-learning基本不受最大化偏差影响。

6.8 博弈、后状态与其他特例 / Games, Afterstates & Special Cases

Afterstates are states evaluated after the agent has made its move. They're useful when we know the immediate effects of actions but not the full dynamics. In tic-tac-toe, different position–move pairs may produce the same "afterposition" — an afterstate value function assesses them equally, enabling more efficient learning.
后状态是在智能体执行动作后评估的状态。当我们知道动作的即时效果但不知道完整动态时很有用。在井字棋中,不同的位置-动作对可能产生相同的"后位置"——后状态价值函数对它们进行相同评估,从而实现更高效的学习。

6.9 小结 / Summary

TD learning combines MC's ability to learn from experience with DP's bootstrapping. TD control extends via GPI. Sarsa is on-policy, Q-learning is off-policy, Expected Sarsa generalizes both. TD methods are today the most widely used RL methods due to their simplicity and online nature.
TD学习结合了MC从经验中学习的能力和DP的自举能力。TD控制通过GPI扩展。Sarsa是on-policy的,Q-learning是off-policy的,Expected Sarsa泛化了两者。TD方法因其简单性和在线特性,是当今使用最广泛的强化学习方法。

核心概念速查 / Key Concepts

术语 / Term英文 / English含义 / Meaning
TD误差TD Error (δt)R+γV(S')−V(S),预测与更好估计之间的差异
TD(0)TD(0) / One-step TD基于单步转移的TD更新
自举Bootstrapping基于已有估计来更新估计
确定性等价Certainty EquivalenceTD(0)批量收敛到的最大似然模型的真实值
SarsaSarsaOn-policy TD控制,使用(S,A,R,S',A')五元组
Q-learningQ-learningOff-policy TD控制,目标为R+γ max Q(S',a)
Expected SarsaExpected Sarsa用期望代替max或采样动作,消除随机方差
最大化偏差Maximization Bias用估计值的最大值估计最大值导致的正偏差
双重学习Double Learning用两个独立估计消除最大化偏差
后状态Afterstate动作执行后的状态,利用已知的即时效果