对应 Maps to:Stanford CS229 Autumn 2018(Andrew Ng)Lecture 16 后半–20(MDPs & Value/Policy Iteration; Continuous State MDP & Model Simulation; Reward Model & Linear Dynamical System; RL Debugging and Diagnostics),以及 CS229 main notes(Andrew Ng & Tengyu Ma)Chapters 15–17(Reinforcement learning; LQR, DDP and LQG; Policy gradient)。 CS229 当前学期的课表需要 Stanford 登录才能看,所以本讲以公开的讲义和 2018 年的录像为准。这是 CS229 的最后一讲;更深入的 RL(TD、Q-learning、DQN、PPO、offline RL、探索理论)见同目录的 CS234。 配套代码 Code:
code/lecture06/(numpy;5 个脚本,最长的约 20 秒) 预计阅读时间:2.5–3 小时(含运行代码)。这是一份复习讲义:假设你见过 MDP 和 value iteration,重点放在”为什么”、“什么时候会失效”和”它和前五讲的联系”上。
0. 本讲导航 · How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- 用 MDP 的语言描述一个序贯决策问题,写出 和 的 Bellman 方程,并说清它们一个是线性方程、一个是带 max 的不动点方程。
- Explain why value iteration converges (γ-contraction), why it is often much faster than the bound, and why policy iteration needs only a handful of iterations (it is Newton’s method).
- Name the two traps of learning a model from experience: planning on an estimated model is overconfident (the optimizer’s curse), and acting greedily on it may never discover what it hasn’t tried.
- For continuous states, compare discretization (curse of dimensionality) and fitted value iteration (regression inside VI — and why it may not converge).
- 推出 LQR 的关键一步(二次函数经过线性动力学再取 min 仍是二次函数),解释 Riccati 增益为什么收敛到常数、certainty equivalence、LQG 的分离原理,以及 DDP / iLQR 在解决什么问题。
- Derive REINFORCE from the likelihood-ratio trick, recognize the score function as the GLM gradient of Lecture 1, and explain what reward-to-go and baselines do to the variance.
- Diagnose an RL system that “doesn’t work” with Ng’s three questions: is it the simulator, the RL algorithm, or the reward?
0.2 默认你已经会的 / 本讲会省略的
| 默认你已经会 Assumed | 本讲省略或只给结论 Skipped |
|---|---|
| MDP、策略、价值函数的定义 | 最优策略存在且可取为确定性、平稳策略的证明 |
| 动态规划的基本思想 | Bellman 算子收缩性的完整证明(只给关键一步) |
| 第 1 讲的最小二乘、softmax 回归、GLM 梯度 | Riccati 方程和 Kalman 滤波的完整代数(只给关键一步和结果) |
| 高斯分布的条件分布公式 | DDP 的二阶展开细节、iLQR 的正则化与 line search |
| 梯度上升 | 策略梯度定理的严格证明(给 likelihood-ratio 的推导);PEGASUS;POMDP 理论 |
| TD learning、Q-learning、DQN、actor-critic、PPO(见 CS234,§7 有对照表) |
0.3 本讲地图 Map
§1 RL 问题:从监督学习到序贯决策;MDP;Bellman 方程
§2 有限 MDP:价值迭代(收缩)/ 策略迭代(Newton)/ 奖励定义最优 / 学模型 / 探索 ← 代码 01、02
§3 连续状态:模拟器 / 离散化与维数灾难 / fitted value iteration 及其不稳定性 ← 代码 03
§4 LQR:Riccati 递推 / vs 手调 / certainty equivalence / 线性化与 DDP / Kalman 与 LQG ← 代码 04 演示 1–4
§5 Policy gradient:REINFORCE / 得分函数 = GLM 梯度 / reward-to-go 与 baseline ← 代码 05
§6 RL debugging:模拟器、算法还是奖励? ← 代码 04 演示 5
§7 CS229 讲了什么、没讲什么(与 CS234 的对照)
§8–12 小结、常见误区、练习、延伸阅读、课程总结
0.4 Notation(和 CS229 main notes 一致)
| 符号 | 含义 |
|---|---|
| MDP:状态集、动作集、转移概率 、折扣因子 、奖励 | |
| 状态奖励。CS229 讲义大部分地方让奖励只依赖状态,本讲照做( 的推广是平凡的) | |
| ;、、 | 策略;策略 的价值、最优价值、动作价值 |
| Bellman 最优算子: | |
| ; | LQR 的线性动力学矩阵;状态 / 动作的代价矩阵(控制论教材写作 ,这里 已经是奖励了) |
| 、 | LQR 的反馈增益()和 cost-to-go 矩阵() |
| ;;; | 策略的期望回报;轨迹;reward-to-go;baseline |
符号上的一个约定:§1–3、§5 说”奖励”、最大化;§4(LQR)说”代价”、最小化,代价 = −奖励。CS229 讲义的 LQR 写成奖励 、价值 (),本讲的 。两种写法的结果完全相同。
0.5 关于数字
讲义里所有实验数字都来自配套代码的实际输出(固定随机种子,python3 -W error 下运行两次逐字相同)。环境都是合成的小问题:Ng 的 4×3 网格、走廊、mountain car、双积分器、倒立摆。我们知道”真相”(能精确算出 、、期望代价),所以能检查算法有没有学对;但数字本身不代表任何真实系统。
1. The RL problem · 从监督学习到强化学习
1.1 What changes when there are no labels
前五讲都有一个”正确答案”可以比较:标签 (监督学习),或者数据本身(无监督学习)。Andrew Ng 讲 RL 时用的例子是自主直升机:在每个时刻该把操纵杆推多少?没有人能给出每一时刻的”正确”动作,但我们能说出飞得好不好(稳不稳、有没有坠毁)。RL 的设定就是:只给一个奖励信号,让算法自己找出动作。
这带来三个监督学习里没有的困难:
- Delayed reward / credit assignment:坠机是因为刚才那一下,还是 10 秒前的某个决定?
- Sequential decisions:现在的动作改变了将来的状态,所以不能把每一步当作独立样本。
- The data depend on your own actions:你只看得到你走过的地方的后果(§2.5 的探索问题)。第 3 讲的 i.i.d. 假设在这里不成立。
1.2 Markov decision processes
An MDP is a tuple . Start in , choose , move to , choose , and so on. The return is
Why discount? Three levels of answer: (1) mathematically it keeps the infinite sum finite (at most ) and makes the Bellman operator a contraction (§2.1); (2) economically, a dollar today is worth more than a dollar tomorrow; (3) as a model, can be read as a per-step probability that the world ends. is roughly how many steps ahead the agent “looks”.
策略 的价值 是从 出发、按 行动的期望回报。最优价值 。一个重要的事实(这里不证明):存在一个策略 同时在所有状态上达到最优,而且可以取成确定性的。
1.3 Bellman equations
把回报拆成”这一步的奖励 + 折扣后的将来”:
Two equations, two very different objects:
- 第一个是线性方程组: 个未知数、 个方程,。这就是 policy evaluation。
- 第二个因为有 ,是非线性的不动点方程 。没有封闭解,要迭代 —— 这就是 §2 的两个算法。
本讲的实现约定:终止状态(如网格世界的 ±1 出口)进入时拿到 ,之后什么也没有;代码里把它的转移概率行设成全 0,Bellman 方程在那里自动变成 。
2. Finite MDPs · 有限 MDP:规划、学模型与探索
2.1 Value iteration and the contraction argument
Value iteration just applies repeatedly: . Why does this converge, from any starting point? The key step is one inequality. For any two value functions and any state ,
(第一步用了 ,第二步用了”概率加权平均不超过最大值”。)所以 是 -范数下的 -收缩(contraction),由 Banach 不动点定理, 唯一,而且
这是最坏情况的保证。代码 01_value_policy_iteration.py 演示 1 在 Ng 课上的 4×3 网格世界(转移 0.8 / 0.1 / 0.1,每步 ,)上比较实际误差和上界:
| 1 | 2 | 5 | 10 | 15 | 20 | 30 | 100 | 200 | |
|---|---|---|---|---|---|---|---|---|---|
| : | 0.952 | 0.936 | 0.878 | 0.112 | 0.018 | 已收敛 | 已收敛 | ||
| : | 1.98 | 1.96 | 1.90 | 1.38 | 1.16 | 1.02 | 0.842 | 0.395 | 0.144 |
| 的上界 | 1.98 | 1.96 | 1.90 | 1.81 | 1.72 | 1.64 | 1.48 | 0.732 | 0.268 |
| 每步奖励 | VI 收敛步数(tol ) | 贪心策略从第几步起已是最优 | |
|---|---|---|---|
| −0.02 | 41 | 0.433 | 11 |
| +0.02 | 1842 | 0.990 | 13 |
时上界要约 2300 步才降到 ,可 时 VI 只用了 41 步。原因是回合会结束:最优策略下 agent 很快走到出口,后期误差每步乘以 的谱半径 0.433,而不是 。“每一步有一定概率终止”等价于一个更小的有效折扣。 时活着本身有奖励,最优策略是永远不进出口:回合不结束,前几步的误差恰好等于上界,后期速率 0.990 ≈ ,要 1842 步。
两个实践要点:
- The greedy policy converges long before the values. 从第 11 步起,对 贪心的策略已经是最优的,而 的误差还有 0.08。我们要的是策略,价值只是中间产物。
- The stopping rule is not the error. 只保证 。 接近 1 时这个放大很可观(下一节表格的第 4 列)。
2.2 Policy iteration is Newton’s method
Policy iteration alternates exact evaluation with greedy improvement. The policy improvement theorem guarantees everywhere, and there are finitely many deterministic policies, so it terminates. But “finite” is — why does it take only a handful of steps in practice?
The answer is that policy iteration is Newton’s method on the Bellman equation (Puterman & Brumelle, 1979). Let ; we want . is piecewise linear: near , where is greedy with respect to , so the Jacobian of is . One Newton step gives
— exactly “act greedily, then evaluate exactly”. Value iteration is the fixed-point iteration , the analogue of fixed-step GD in Lecture 1: linear convergence at the contraction rate. 代码 01 演示 2 在一个随机 MDP(200 个状态、4 个动作、没有终止状态)上把 推向 1:
| VI 步数(tol ) | PI 轮数 | VI 结果的实际误差 | PI 各轮的误差 | |
|---|---|---|---|---|
| 0.9 | 170 | 5 | 9 → 3 → 0.6 → 0.06 → 0 | |
| 0.99 | 1769 | 5 | 70 → 20 → 4 → 0.06 → 0 | |
| 0.999 | 17758 | 6 | 700 → 200 → 30 → 0.6 → 0.007 → 0 |
VI 的步数 ,PI 几乎不变;PI 的误差越到后面缩得越快(Newton 的超线性收敛,对照第 1 讲 §3.7)。在 4×3 网格上,PI 从”全部向下”出发,改进 4 轮就找到最优策略(第 5 轮评估只是确认不再需要改),在 个确定性策略里只看了 5 个。
代价也和 Newton 一样:每轮要解 的线性方程组(),VI 每步只要 。状态很多时用折中的 modified policy iteration:评估时只做几次迭代。
2.3 The reward defines what “optimal” means
Same grid, same dynamics, only the per-step reward changes (code 01, demo 3, ):
| Behavior of | (start) | |
|---|---|---|
| −2 | Living hurts: from (3,2) and (4,1) it dives straight into the −1 exit | −10.56 |
| −0.4 | Shortest route, accepting some risk of slipping into −1 | −1.59 |
| −0.04 | (2,1) switches to going left, around the long way | 0.65 |
| −0.02 (Ng’s value) | (3,1) also switches to left: a big detour along the top row | 0.78 |
| 0 | Bumps into the wall at (3,2) and the border at (4,1): can never slip into −1, leaves only via the 10% slips | 0.93 |
| +0.02 | Living is rewarded: avoids even the +1 exit and never finishes | 2.00 |
一个看起来无害的小数字能完全改变行为。奖励函数不是”告诉 agent 怎么做”,而是”定义什么叫好”—— §6 的调试流程里第三个问题就是”奖励函数是不是错了”。(CS234 第 2 讲的 05_gridworld.py 在同一个网格上把 和噪声也扫了一遍。)
2.4 Learning a model from experience
So far and were given. CS229’s answer when they aren’t is the simplest possible one: count.
这就是第 1 讲的 MLE:每个 的下一状态服从一个 categorical 分布,它的 MLE 是频率。然后在 上做 VI,执行得到的策略,收集更多数据,再估计 …… 这是最朴素的 model-based RL。 代码 02_model_based_rl.py 演示 1 在 4×3 网格里随机乱走,学模型、规划,再在真实环境里评估(每行 50 次重复的平均,):
| 回合数 | 转移数 | 试过的 | TV 误差 | 模型认为的 (start) | 真实的 (start) | regret | P(最优) |
|---|---|---|---|---|---|---|---|
| 1 | 34 | 42% | 0.272 | −0.064 | −0.075 | 0.855 | 0.02 |
| 3 | 106 | 76% | 0.224 | 0.531 | 0.271 | 0.509 | 0.12 |
| 10 | 320 | 98% | 0.155 | 0.791 | 0.665 | 0.116 | 0.16 |
| 30 | 976 | 100% | 0.084 | 0.792 | 0.756 | 0.024 | 0.46 |
| 100 | 3204 | 100% | 0.045 | 0.787 | 0.778 | 0.002 | 0.84 |
| 1000 | 32331 | 100% | 0.014 | 0.783 | 0.780 | 0.000 | 1.00 |
两件值得记住的事:
- 不需要把模型学得很准:100 个回合时 TV 误差还有 0.045,84% 的情况已经得到最优策略。和 §2.1 一样,只要动作的排序对了就行。
- 在学到的模型上规划会过度自信(optimizer’s curse): = +0.260(3 个回合)、+0.127(10 个)、+0.009(100 个)。在有噪声的估计上取 max,会系统性地挑中那些”恰好被高估”的动作(练习 3)。这是所有”先学模型再优化”方法的通病,§3.4 的 fitted VI 里还会以 maximization bias 的形式再出现一次。
2.5 The exploration problem (briefly)
CS229 的循环是”执行当前最优策略 → 更新模型 → 重新规划”。它有一个致命的盲点:数据是 agent 自己的行为产生的。演示 2 是一条走廊:从状态 2 出发,左边 2 步是 +1 的出口,右边 7 步是 +10 的出口(,最优是向右,;向左只有 )。每个 agent 先随机走一个回合,再按计划走 30 个回合,独立跑 200 次(第 次运行时三个 agent 的随机回合相同):
| agent | 随机回合碰巧到 +10 | 曾经到过 +10 | 最终计划向右 | 最后 10 回合的平均回报 | 总步数 |
|---|---|---|---|---|---|
| greedy(CS229 的循环) | 0.200 | 0.200 | 0.200 | 2.119 | 103.5 |
| ε-greedy,ε = 0.1 | 0.200 | 0.200 | 0.200 | 2.052 | 113.2 |
| optimistic(R-max 式) | 0.200 | 1.000 | 1.000 | 6.983 | 232.2 |
- greedy 只有在第一个随机回合碰巧走到右边时才学会向右(随机游走先到 9 的概率是 2/9 ≈ 0.22,200 次的标准误约 0.03)。否则模型只见过 +1,计划就是”向左”,执行”向左”又只会看到 +1 —— 永远不会知道右边有什么。模型没有估计错;它只是从来没有关于右边的数据。
- ε-greedy 一次也没帮上:200 次运行里靠随机动作多找到 +10 的次数是 0。要连续很多步”恰好”随机地向右,概率随距离指数下降。这种”抖动式”探索(dithering)在需要深度探索的问题上几乎无效。
- 乐观(optimism in the face of uncertainty):把没试过的 当成”通往一个每步都拿 的天堂”。agent 会有计划地去试每个没试过的动作,试过之后乐观自动消失。代价是前期多走一倍的路。
CS229 只点到为止。Regret、UCB、Thompson sampling、R-max、PSRL 这些系统的探索理论见 CS234 第 11–13 讲。
3. Continuous states · 连续状态
3.1 Simulators
直升机的状态是连续的(位置、姿态、速度、角速度,12 维),表格 存不下。CS229 的思路是先有一个模拟器(simulator / model):给定 ,返回 或它的一个样本。模拟器从哪来?
- Physics:刚体动力学、空气动力学的方程。准确,但写起来费力,而且总有没建模的效应。
- Learned from data:让人类飞一会儿,记录 ,然后拟合 (就是第 1 讲的线性回归,每个输出维度一个)。Ng 的直升机就是这样做的,再在线性模型上加噪声 让它变成随机的。Ng 课上的建议:训练时用随机模拟器,否则策略会利用模拟器”过于完美”的地方,到了真实世界就脆弱。
有了模拟器,有两条路:离散化,或者用函数逼近 。
3.2 Discretization and the curse of dimensionality
把状态空间切成格子,每个格子当作一个离散状态,转移概率从模拟器采样估计(§2.4 的计数法,只是数据来自模拟器),然后做 VI。代码 03_continuous_fitted_vi.py 演示 1 用 mountain car:一辆动力不足的小车要开上右边的山顶,必须先往左倒车蓄势 —— “先远离目标才能到达目标”,需要向前看很多步。状态 = (位置, 速度),每步奖励 −1,;评估 100 个固定起点的平均到达步数(上限 1000):
| 每维格子数 | 5 | 10 | 20 | 40 | 80 | 160 |
|---|---|---|---|---|---|---|
| 格子总数 | 25 | 100 | 400 | 1600 | 6400 | 25600 |
| 平均步数 | 1000(全部失败) | 162 | 155 | 108 | 98.8 | 98.6 |
太粗,表示不出”先倒车”这种精细的策略; 基本饱和。两个代价:
- Resolution: 和 在一个格子内是常数(piecewise constant),真实的 却在格子内部变化。
- Curse of dimensionality:每维 段, 维要 个格子。每维 20 段时,2 维 400 个,4 维(cart-pole)16 万,6 维 6400 万,12 维的直升机 。CS229 讲义的经验法则:1–2 维离散化很好用,3–4 维要小心地设计格子,6 维以上基本不行。
3.3 Fitted value iteration
The fix is the same as everywhere else in ML: replace the table by a parametric function , with a number of parameters that doesn’t grow like . Fitted value iteration (CS229 main notes, Ch. 15):
- Sample states once.
- Repeat:
- For each and each action , sample next states from the simulator and set .
- — one Bellman backup on the samples.
- — least squares from Lecture 1.
- Act greedily, using the simulator to look one step ahead.
So FVI is VI where each sweep is a supervised regression problem: the Bellman backup produces the labels, least squares fits them. Demo 2 (, 1000 sweeps, no regularization; mountain car is deterministic so ; the true lies in ):
| Features | #params | last | range of on samples | mean steps | failed |
|---|---|---|---|---|---|
| polynomial, degree 2 | 6 | [−64.5, 18.4] | 904 | 89% | |
| polynomial, degree 4 | 15 | [−64.1, 12.7] | 201 | 9% | |
| polynomial, degree 6 | 28 | [−61.8, 34.2] | 129 | 0% | |
| RBF 5×5 | 26 | [−61.0, 1.7] | 125 | 0% | |
| RBF 9×9 | 82 | 17 | [−65.8, 1.2] | 108 | 0% |
| RBF 15×15 | 226 | 0.89 | [−60.9, 3.0] | 119 | 0% |
(For comparison: discretization with , 6400 cells → 98.8 steps.)
- Low-degree polynomials are too rigid to represent the shape of . RBFs are local features, and 82 of them come close to what 6400 cells achieve. But 15×15 is no better than 9×9: more features did not buy a better policy.
- Every row has positive values of , although the truth is . Function approximation has errors, but the greedy policy only asks which action has the larger , so the policy can be fine while is wrong — and FVI’s should not be read as “steps to go”.
- For RBF 9×9 and 15×15, θ is still changing after 1000 sweeps. Exact VI always converges; FVI may not settle.
3.4 Fitted VI need not converge
演示 3 跟踪 RBF 15×15 的迭代过程(各记录点上的 ,以及贪心策略的失败率):
| 第 轮 | 10 | 30 | 100 | 300 | 1000 | 2000 |
|---|---|---|---|---|---|---|
| 无正则化,: | 5.1 | 28 | 8.7 | 19 | 0.89 | 0.0072 |
| max / 失败率 | 32 / 100% | 364 / 100% | 780 / 100% | 739 / 100% | 833 / 0% | 833 / 0% |
| 无正则化,: | 5.8 | 14 | 6.6 | 1.0 | 0.025 | |
| 失败率 | 100% | 100% | 100% | 28% | 8% | 8% |
| ridge ,: | 0.66 | 0.41 | 0.11 | 0.0021 | ||
| max / 失败率 | 7.3 / 100% | 18 / 100% | 27 / 0% | 26 / 0% | 26 / 0% | 26 / 0% |
不加正则化时, 的变化量忽大忽小,几百轮都没有稳定的下降趋势,系数大到 833(高度重叠的 RBF 用大的正负系数互相抵消),前 300 轮的贪心策略全部失败;最后慢慢停下来。多采样没有治好它: 同样乱跳了几百轮,最后停在一个更差的地方(8% 失败)。加一点 ridge, 就像收缩映射一样单调下降,第 100 轮的策略已经全部成功。
Why? Exact VI is a contraction in . Least-squares fitting is a projection that is optimal in (sample-weighted) — it can make the error larger at some states (extrapolation, wild cancelling coefficients). The composition “fit ∘ backup” need not be a contraction in any norm, so errors can be amplified from one sweep to the next, oscillate, or settle somewhere that depends on details (Tsitsiklis & Van Roy, 1996). Gordon (1995) showed that if the regressor is an averager — prediction = a weighted average of training targets, as in -NN or discretization — FVI does converge. Ridge doesn’t make least squares an averager, but it tames the large coefficients.
Ridge 修好的是”不稳定”,不是”高估”。三种设置下 的最大值都在山顶旁边(–0.5),而且那里的回归目标本身就是正的(3.14、12.13、4.33)。机制:山顶是一个悬崖(再走一步就到的状态 ,山顶本身为 0 且不在回归样本里),光滑的特征在悬崖边冲过头;bootstrapping 把高估喂进邻居的目标 , 又专挑被高估的那个动作 —— 高估一轮轮传开。这就是 maximization bias,和 §2.4 的 optimizer’s curse 是同一件事。它的后代 DQN 有同样的问题(Double DQN 就是为它设计的),“函数逼近 + bootstrapping + off-policy”会发散的现象叫 deadly triad(CS234 第 4–5 讲)。
4. Linear quadratic regulation · LQR、DDP 与 LQG
4.1 The one continuous case with an exact solution
Linear dynamics, quadratic cost, finite horizon. Why care about such a special case?
- 任何光滑的非线性系统在平衡点附近都近似线性,任何光滑的代价在最小值附近都近似二次。“让直升机悬停在原地”、“让倒立摆保持竖直”都是这个形状(§4.4)。
- 它有精确解: 恰好是二次函数,最优策略恰好是线性反馈 。连续状态、无穷多个状态,却只需要一个 矩阵 —— 不需要离散化,也不需要 FVI。
4.2 The Riccati recursion
动态规划从 往回做。关键一步:假设 ,那么
括号里对 是凸二次函数(),令导数为 0 得 ,代回去仍是 的二次函数 —— “二次函数经过线性动力学、再对动作取 min,还是二次函数”,所以归纳成立:
的递推叫 (discrete-time) Riccati equation。它就是线性-二次情形下的价值迭代:每一步 backup 都能用一个矩阵精确表示。
代码 04_lqr_lqg.py 演示 1 用双积分器(直线上的小车, = [位置, 速度], = 力,,,,):
| 时刻 | 0 | 50 | 80 | 90 | 95 | 97 | 98 | 99 |
|---|---|---|---|---|---|---|---|---|
| 剩余步数 | 100 | 50 | 20 | 10 | 5 | 3 | 2 | 1 |
| [2.762, 2.508] | [2.762, 2.508] | [2.718, 2.485] | [2.475, 2.047] | [1.084, 0.830] | [0.428, 0.388] | [0.195, 0.229] | [0.049, 0.104] |
| 时域 | 1 | 2 | 5 | 10 | 20 | 30 | 50 |
|---|---|---|---|---|---|---|---|
| 2.7 | 2.6 | 1.7 | 0.46 | 0.044 | 0.0048 |
- 离终点很远时 是常数 ,只有最后十来步迅速变小:快结束时,“现在用力、以后享受小偏差”的买卖不划算了。
- 时域每多 10 步, 大约缩小 10 倍:Riccati 递推本身是一个收缩的不动点迭代(又是 §2.1)。所以无限时域 LQR = 迭代 Riccati 到不动点(discrete algebraic Riccati equation, DARE),实践中常常直接用 。
- 从 出发,预测代价 ,实际执行的代价也是 9.077561,逐位相同: 是精确的,不是近似。
4.3 LQR vs hand tuning; certainty equivalence
A PD controller is exactly a linear feedback . Demo 2 computes expected costs exactly by propagating covariances (, no sampling error):
| Controller | Gains | E[cost] with process noise | E[cost] without noise |
|---|---|---|---|
| LQR (time-varying ) | [2.76, 2.51] | 12.339 | 9.769 |
| LQR stationary | [2.76, 2.51] | 12.424 | 9.769 |
| hand-tuned PD | (1, 1) | 18.435 | 12.930 |
| hand-tuned PD | (3, 2) | 13.002 | 10.274 |
| hand-tuned PD | (10, 4) | 27.529 | 23.730 |
| hand-tuned PD | (30, 10) | 123.029 | 112.761 |
A brute-force search over finds the best PD gains to be (2.75, 2.50) both with and without noise — that is . Two lessons:
- LQR does the tuning for you — given and . The hand-tuning problem doesn’t disappear; it moves into choosing the cost matrices. That is reward design again (§2.3, §6).
- Certainty equivalence:过程噪声让每一行的代价都变大,但不改变最优增益。Riccati 递推里 根本不含 ;噪声只贡献一个和策略无关的常数 (预测 ,和精确计算一致)。直觉:加性高斯噪声是”不管你怎么做都会被吹偏这么多”,它对任何策略都一样。(这个结论依赖线性 + 二次 + 加性高斯;有约束或非线性时不成立。)
4.4 Nonlinear dynamics: linearization, DDP and iLQR
For a nonlinear simulator , the simplest move is to linearize around a point you want to stay near (e.g. hover, or the upright pendulum): , then run LQR. Demo 3 uses an inverted pendulum (, upright, ), linearized at the top, started at rest from angle (1 = stabilized after 6 s):
| Torque limit | = 0.1 | 0.3 | 0.5 | 0.8 | 1.0 | 1.5 | 2.0 | 2.5 | 3.0 |
|---|---|---|---|---|---|---|---|---|---|
| ∞ (peak torque used: 68.5) | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 20, 10 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 5 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 3 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
结果和”离线性化点远了就失效”的简单说法不完全一样:力矩不限时,线性化的 LQR 从 3.0 rad(离竖直向下只差 8°)都能救回来,代价是约 7 倍 的峰值力矩 —— 力气够大时,“偏了就往回推”这条线性规则在很大范围内都对。力矩上限低于 时,成功的边界(0.5、0.3)几乎正好是”静止撑住”的极限 、。再远,任何控制器都没法直接推上去,必须先往反方向摆、一来一回地荡上去(swing-up)。这是一个非线性的计划,线性模型根本表示不出来:在线性模型里, 再大也只是”多推一点”。
这就是 DDP(differential dynamic programming)/ iLQR 的动机:不在一个固定点线性化,而是沿着当前猜测的轨迹 线性化(DDP 还保留动力学的二阶项),解一个时变 LQR 得到改进的轨迹,再沿新轨迹线性化 …… 迭代到收敛。每一轮都是 §4.2 的 Riccati 递推,所以它继承了 LQR 的效率;加上动作约束(control-limited DDP),它能找到 swing-up 这样的轨迹。它是机器人轨迹优化和 MPC 的主力。
4.5 Partial observability: Kalman filter and LQG
Real sensors are noisy and incomplete: we observe , , not . In the linear-Gaussian world the belief about stays Gaussian, so it’s enough to track a mean and a covariance — the Kalman filter:
(这里的 是 Kalman gain,不是 LQR 的增益。)修正量 = gain × “观测比预测多出来的部分”(innovation):观测噪声大 → 更相信预测;当前不确定性大 → 更相信观测。数学上它就是”联合高斯分布的条件分布”(第 5 讲 factor analysis 的 E 步用的也是它)在时间上的递推。
LQG = Kalman filter + LQR: plug the estimate into the controller, . In the linear-quadratic-Gaussian setting this is not merely a reasonable heuristic but optimal (the separation principle: estimation and control can be designed separately — the other face of certainty equivalence). Demo 4 observes only the position (); 2000 simulations per setting, with the same random numbers for all three controllers:
| oracle (sees true state) | LQG (Kalman + LQR) | naive (velocity by differencing) | Kalman velocity error | differencing velocity error | |
|---|---|---|---|---|---|
| 0.1 | 12.59 | 18.22 | 161.58 | 0.228 | 1.330 |
| 0.3 | 12.59 | 25.87 | 1345.07 | 0.299 | 3.976 |
| 1.0 | 12.59 | 53.43 | 14806.77 | 0.401 | 13.248 |
(oracle 的精确期望代价是 12.339;表中平均值的标准误约 0.3。)差分 把观测噪声放大了约 倍,LQR 拿着这么吵的速度去用力,代价是 oracle 的上百倍。Kalman 滤波利用动力学模型把历史观测融合起来: 时位置误差 0.148,小于观测本身的噪声,LQG 的代价是 oracle 的 2.1 倍、naive 的 1/52。看不到真实状态总要付出代价,但模型能把这个代价压得很低。
5. Policy gradient · 策略梯度
5.1 Why search over policies directly?
Sections 2–4 all compute (or a model) first and derive the policy from it. The alternative is to parameterize the policy and run gradient ascent on the expected return . Why would we?
- Continuous / high-dimensional actions: over a continuous action is itself an optimization problem.
- Simple policies can be good even when is complicated: mountain car’s has cliffs and spiral boundaries, yet “push in the direction you are moving” is a very simple policy.
- Stochastic policies are sometimes optimal (partial observability), and they make differentiable even when the dynamics aren’t.
- No model needed (next subsection). Ng’s helicopter used policy search on a learned simulator (PEGASUS: fix the simulator’s random seeds so that becomes a deterministic function you can optimize).
5.2 REINFORCE and the likelihood-ratio trick
,, 是总回报。直接对期望求导的困难是: 在分布里,不在被积函数里。Likelihood-ratio(score function)技巧:
而 ,动力学那一项和 无关,求导时消失:
所以只要能采样轨迹,就能无偏地估计梯度,不需要知道 ,也不需要 可导。这就是 REINFORCE(Williams, 1992)。
The score function is a GLM gradient. 代码 05_policy_gradient.py 演示 1:
- Softmax policy : —— 和第 1 讲 §4.6 softmax 回归单个样本的梯度 一模一样,只是”标签”换成了自己刚做的动作。
- Gaussian policy : —— 线性回归的”误差 × 输入”。
So REINFORCE is return-weighted maximum likelihood on your own actions: fit a GLM to the actions you took, weighting each by how well things went afterwards. 代码里 reinforce_grad 就是这么写的:, = one-hot 状态, = one-hot 动作, = 回报。第 1 讲的主线”loss = −log-likelihood”在 RL 里变成了”objective = 回报加权的 log-likelihood”。
还有一个性质撑起了下一节:得分函数的期望为 0,。演示 1 中 softmax 的情形精确为 0,高斯的情形 20 万个样本的平均是 −0.0023(标准误 0.0026)。
5.3 Variance: reward-to-go and baselines
REINFORCE 无偏,但方差很大。两个几乎免费的改进(CS229 main notes Ch. 17):
- Reward-to-go:动作 影响不了它之前(和当时)的奖励,把 换成 。去掉的部分期望为 0,只贡献噪声。
- Baseline:再减去任何不依赖 的 :,期望不变。好的 baseline 让权重变成”比平均好多少”(advantage)。
演示 2 用一条 5 个状态的走廊(状态 4 每步 +1,10% 滑动,固定时域 ,表格 softmax,),精确算出 后比较四种估计量(每种 20000 条轨迹)。再给每一步奖励加一个常数 :时域固定时所有轨迹的回报都多 ,最优策略和 都不变:
| 估计量 | 预期抽样误差 | 10 条轨迹的估计与 的 cos | |||
|---|---|---|---|---|---|
| 0 | 全回报 | 0.018 | 0.021 | 8.88 | 0.583 |
| 0 | reward-to-go | 0.012 | 0.016 | 5.06 | 0.715 |
| 0 | (时间 baseline) | 0.011 | 0.014 | 3.98 | 0.760 |
| 0 | (状态 baseline) | 0.010 | 0.012 | 3.04 | 0.803 |
| 1 | 全回报 | 0.168 | 0.169 | 568.03 | 0.078 |
| 1 | reward-to-go | 0.071 | 0.092 | 170.44 | 0.152 |
| 1 | 0.011 | 0.014 | 3.98 | 0.760 | |
| 1 | 0.010 | 0.012 | 3.04 | 0.803 |
All four estimators are unbiased (the deviation is of the same size as the expected sampling error). With the variance of the full-return estimator grows 64×, and a 10-trajectory estimate is nearly uncorrelated with the true gradient: every is multiplied by a large positive number ≈ , so “every action taken gets reinforced”, and the useful signal (which action was better than which) drowns. The two baseline rows are identical to their counterparts: the baseline subtracts the constant exactly. Using a learned as the baseline is actor-critic (CS234 Lecture 7).
5.4 What the variance does to learning
Demo 3: 10 trajectories per update, learning rate 0.1, 20 independent runs, evaluated with the exact (the table shows ; ):
| Method | = 0 | 10 | 25 | 50 | 100 | 200 | worst run @200 | |
|---|---|---|---|---|---|---|---|---|
| — | exact gradient | 0.625 | 1.289 | 2.770 | 3.938 | 4.411 | 4.589 | — |
| 0 | full return | 0.625 | 1.334 | 2.754 | 3.902 | 4.383 | 4.590 | 4.548 |
| 0 | reward-to-go | 0.625 | 1.319 | 2.779 | 3.909 | 4.398 | 4.593 | 4.554 |
| 0 | reward-to-go + LOO baseline | 0.625 | 1.343 | 2.817 | 3.959 | 4.416 | 4.587 | 4.564 |
| 1 | full return | 0.625 | 1.091 | 1.363 | 2.161 | 3.250 | 3.704 | 0.082 |
| 1 | reward-to-go | 0.625 | 1.048 | 1.880 | 2.997 | 3.883 | 4.511 | 3.043 |
| 1 | reward-to-go + LOO baseline | 0.625 | 1.343 | 2.817 | 3.959 | 4.416 | 4.587 | 4.564 |
(LOO = leave-one-out: trajectory ‘s baseline is the average of the other trajectories in the batch, which does not depend on its own actions, so it is unbiased. In RL fine-tuning of LLMs this is called RLOO; GRPO’s “group mean” is the same idea.)
- 时 baseline 几乎没有影响:三条曲线几乎重合,都跟着精确梯度走。方差只差约 2 倍,被 200 步平均掉了。这和预想的”baseline 一定明显加速”不一样 —— baseline 不是在任何问题上都有显著效果。
- 时差别就出来了。全回报平均只到 3.704,最差的一次 0.082:训练结束时它在起点向右的概率只剩 0.008(一直往墙上撞), 从 0.663 缩到 0.052,几乎出不来了 —— 高方差的更新把 softmax 推进了一个很差的、几乎确定性的策略(premature convergence)。加 baseline 后的曲线和 时一样。奖励的”零点”在 RL 里本来是任意的(每步 +1 存活奖励、每步 −1 时间惩罚都很常见),好的估计量不应该被它左右。
- 精确梯度 200 轮后也只到 4.589:softmax 越接近确定性,梯度越小( 因子),普通梯度上升在最后一段很慢。Natural policy gradient / TRPO / PPO 改的就是这个几何问题(CS234 第 7 讲)。
6. RL debugging and diagnostics · 是模拟器、算法还是奖励?
Ng 在 2018 年最后一讲用直升机讲了一个调试流程。系统由三个部件组成,各带一个假设:
- Simulator:模拟器足够准确,在模拟器里飞得好的策略在现实里也飞得好。
- RL algorithm:算法能在模拟器里找到(近似)最大化奖励的策略。
- Reward function:最大化这个奖励 ⇔ 飞得好。
RL 策略 在现实里飞得不好时,按顺序问三个能用数字回答的问题( 是飞得好的人类驾驶员):
- 在模拟器里飞得好、在现实里飞得不好?→ 模拟器的问题。
- 否则,比较奖励:?存在一个奖励更高的策略,RL 却没找到 → RL 算法的问题。
- 否则 :RL 把奖励优化得比人还好,却飞得更差 → 奖励函数的问题。
代码 04 演示 5 在双积分器上分别注入三种错误(“飞行质量” = 位置误差的 RMS,是我们真正关心的东西;人类 = PD (3, 2),; 算飞得好; = 按 RL 用的代价函数在真实系统上的期望代价):
| 场景 | 诊断 | ||||
|---|---|---|---|---|---|
| 一切正确 | 0.270 | 0.270 | 12.34 | 13.00 | nothing to fix |
| 模拟器把质量估成 0.1(真实 1) | 0.174 | 0.367 | 19.46 | 13.00 | simulator |
| 算法只往前看 3 步 | 0.679 | 0.679 | 49.59 | 13.00 | RL algorithm |
| 代价函数忘了惩罚位置 | 1.035 | 1.035 | 4.56 | 16.71 | cost / reward |
- 模拟器以为飞行器轻 10 倍,LQR 选了偏软的增益:模拟器里比人还好(0.174),现实里软绵绵的(0.367)。该做的是收集真实数据重新拟合动力学(§3.1 的回归),而不是去调算法或奖励。
- 短视的规划器在模拟器和现实里一样差(模拟器没问题);人类的代价 13.00 远低于 RL 的 49.59 —— 有更好的策略它没找到。
- 忘了位置的代价函数:RL 的代价 4.56 比人类的 16.71 还低,它完美地完成了”最小化这个代价”,却只学会了刹车、停在哪儿算哪儿。
The value of the procedure is that it turns a vague complaint (“it doesn’t fly well”) into comparisons you can compute, so you don’t spend months improving the wrong component. It is the same idea as the ML diagnostics of Lecture 3 (“is it the optimization algorithm or the objective?”): check whether a solution you know to be better is actually better under your objective.
7. What CS229 covers, and where to go next · 与 CS234 的关系
CS229’s treatment of RL is model-centric: with a known model, plan (VI / PI / LQR); without one, learn the model first (counting, regression) or use a simulator. It says almost nothing about the large family of methods that learn values directly from experience without a model. CS234, in the same folder, fills that in:
| Topic | CS229 (this lecture) | CS234 (same folder) |
|---|---|---|
| MDPs, VI, PI, contraction | §1–2.2 | Lecture 2: more derivations, finite horizon, Mars rover example |
| Learning values from experience (model-free) | — | L3 MC / TD; L4 SARSA, Q-learning, Double Q |
| Function approximation | §3.3–3.4 fitted VI | L5 linear FA, deadly triad, DQN |
| Policy gradient | §5 REINFORCE + baseline | L6 REINFORCE; L7 actor-critic, GAE, TRPO / PPO |
| Exploration | §2.5 (one example) | L11–13 bandits, UCB, Thompson sampling, R-max, PSRL |
| Model-based RL | §2.4, §3 | L15 Dyna, compounding model error |
| Learning from humans | the human pilot as a reference in §6 | L8 imitation learning / IRL (Abbeel & Ng’s helicopter aerobatics used apprenticeship learning); L9 RLHF / DPO |
| Offline RL, search | — | L10 offline RL; L14 MCTS / AlphaZero |
| Continuous control: LQR / DDP / LQG | §4 | not covered — this part is unique to CS229 |
8. 小结 · Summary
| Method | What you need | What it learns / computes | The optimizer underneath | Main pitfall | Code |
|---|---|---|---|---|---|
| Value iteration | , | fixed-point iteration (-contraction) | iterations when episodes don’t end; stopping rule ≠ error | 01 | |
| Policy iteration | , | Newton’s method | per iteration | 01 | |
| Counting + planning | ability to sample | (MLE), then plan | frequencies + VI | optimizer’s curse; no exploration, no data | 02 |
| Discretization | simulator, low dimension | on a grid | sampled transitions + VI | resolution; | 03 |
| Fitted VI | simulator | backup + least squares | may not converge; overestimation; needs regularization | 03 | |
| LQR / LQG | linear dynamics, quadratic cost (Gaussian noise, linear observations) | , Kalman estimate | Riccati recursion (exact VI) | only as good as the linearization | 04 |
| DDP / iLQR | smooth nonlinear model | one optimal trajectory + local feedback | repeated time-varying LQR along a trajectory | local optima; needs a decent initial trajectory | (§4.4) |
| REINFORCE | ability to sample trajectories | return-weighted log-likelihood gradient ascent | variance; premature convergence | 05 |
Six takeaways:
- Bellman equations are fixed-point equations: VI is fixed-point iteration (a -contraction, often much faster when episodes end), PI is Newton’s method, and the Riccati recursion is VI when is exactly quadratic.
- Optimizing over an estimate is biased upward. Planning on a learned model, or taking over a fitted , systematically overestimates (optimizer’s curse, maximization bias).
- Your data depend on your actions. Greedy and ε-greedy can miss what they never try; optimism explores on purpose.
- Function approximation inside dynamic programming is not just regression. Errors feed back through bootstrapping; FVI may oscillate or settle in the wrong place, and regularization (Lecture 3) helps.
- REINFORCE is weighted maximum likelihood on your own actions, with a GLM score function from Lecture 1. Baselines don’t change the expectation but can change the variance by orders of magnitude.
- The reward/cost is part of the design. It defines “optimal” (grid world), replaces hand-tuning (LQR), and is one of the three things to check when a system fails (§6).
9. 常见误区 · Common pitfalls
- “VI needs to be tiny before the policy is right.” The bound is worst case; with termination it can be orders of magnitude faster, and the greedy policy is usually optimal long before converges.
- “‖V_{k+1} − V_k‖ is small, so V is accurate.” The error can be times larger — 1000× at .
- “The learned model says this plan is worth 0.53, so it is.” Planning on an estimated model overestimates (0.531 promised vs 0.271 actual with 3 episodes).
- “ε-greedy takes care of exploration.” Not when the reward is many deliberate steps away; it found the far exit 0 extra times in 200 runs.
- “Fitted VI is VI with a regressor, so it converges like VI.” It may not; watch and the range of (positive values when all rewards are negative are a red flag).
- “Noise should change the LQR gains.” Not for additive Gaussian noise in the LQ setting (certainty equivalence) — it only adds a constant cost. It does matter with constraints, nonlinearity, or multiplicative noise.
- “Adding a constant to every reward is harmless.” With termination it changes the optimal policy (§2.3); in policy gradient without a baseline it inflates the variance (64× for in §5.3).
- “The RL policy fails, so let’s tune the reward.” Ask the three questions in order first (§6).
10. 练习 · Exercises
Exercise 1 (Contraction and iteration counts). (a) Prove . (b) Starting from with , how many iterations guarantee ? (c) In code 01 with and step reward , VI needed only 41 iterations. Use “each step has some probability of terminating” to explain why the actual rate is rather than .
参考答案 Answer
(a) For each : with , whose absolute value is at most (a probability-weighted average never exceeds the maximum). (b) , so we need , i.e. . (c) Once the greedy policy has settled on , the error propagates as among non-terminal states. Terminal rows are zero, so is sub-stochastic — probability mass leaks out every step — and its spectral radius is below 1, giving the effective rate . When the agent never wants to finish (+0.02 per step), is stochastic on the non-terminal states, its spectral radius is 1, and the rate returns to .
练习 2(PI = Newton). 设 。证明:在 处对 做一步 Newton 法(用贪心策略 给出的 Jacobian ),得到的正是 。为什么 PI 的误差序列(9 → 3 → 0.6 → 0.06 → 0)越到后面缩得越快?
参考答案
。 是有限个仿射函数的上包络(每个确定性策略一个),所以 是凸的分段线性函数。Newton 法在分段线性函数上:一旦当前点落在”正确的那一段”(贪心策略 = ),下一步就精确落到根上。这就是为什么 PI 在有限步内精确终止,而不只是渐近收敛;越到后面,贪心策略和 的差别越小,误差缩得越快。
Exercise 3 (The optimizer’s curse). There are actions, all with true value 0, and estimates , . (a) For , compute . (b) If we pick , what is its true value, and what is the expected “believed minus true” value? (c) Relate this to the table of code 02: why does the gap shrink with more data?
参考答案 Answer
(a) with and , so ; it grows with (roughly ). (b) The true value is 0, so the expected overconfidence is even though every individual estimate is unbiased. (c) More data means a smaller error in (a smaller ), hence a smaller overestimate: +0.260 with 3 episodes → +0.009 with 100. Remedies: cross-fitting (choose the action with one half of the data, evaluate it with the other — the idea behind Double Q-learning), or pessimism where uncertainty is high (offline RL, CS234 Lecture 10).
练习 4(Scalar LQR). ,代价 。(a) 求稳态 Riccati 方程的解 和增益 。(b) 讨论 和 的极限,并给出直觉。(c) 闭环系统 一定稳定吗?
参考答案
(a) ,:,即 ,取正根 ;。(b) (用力免费):,,一步把状态推到 0(deadbeat control)。(用力很贵):,,慢慢地、温柔地往回推。代码 04 演示 1 里 在时域末尾变小是同一个直觉的时间版本:剩下的步数少,“用力”的回报就少。(c) ,所以一定稳定(单调收敛,不振荡)。
练习 5(Baselines). (a) 证明对任何只依赖 的 ,。为什么 不能依赖 ?(b) 只考虑一步问题(一个 、一个回报 ),在所有常数 里,使 最小的 是什么?它和”平均回报”有什么不同?(c) 解释代码 05 里为什么减去 baseline 后 和 的结果完全相同。
参考答案
(a) 先对 取条件期望:。如果 依赖 ,就不能提到期望外面,一般不为 0(会引入偏差)。(b) 对 求导令其为 0: —— 按得分函数的大小加权的平均回报。实践中直接用平均回报(或学一个 ),差别通常不大。(c) 时域固定、没有终止状态时, 让每条轨迹的 都加上同一个数 ;时间 baseline(或 LOO baseline)在同一个 上也加上同一个数,相减后权重完全不变,所以梯度估计(以及整个训练过程,在相同随机数下)不变。
练习 6(Diagnose it). 一个机械臂在模拟器里用 RL 训练,奖励 = −(手到杯子的距离)。部署后,模拟器里和真实环境里它都能很快抓到杯子,但经常把旁边的水瓶撞倒。人类操作员也能抓到杯子而且不撞水瓶,但按这个奖励算,人类的回报比 RL 低(动作慢)。按 §6 的流程,问题出在哪?如果换一种情况:RL 在真实环境里的回报比人类低,而模拟器和真实环境里的表现一致呢?
参考答案
第一种:问题 1——模拟器和现实一致(都能抓到、都撞瓶子),不是模拟器的问题。问题 2——RL 的回报高于人类,算法确实把这个奖励优化得很好。所以是奖励函数的问题:奖励里没有表达”别撞倒东西”(加一个碰撞惩罚,或者用人类示范学奖励,即 inverse RL,CS234 第 8 讲)。第二种:模拟器没问题,而人类在同一个奖励下得分更高,说明存在更好的策略 RL 没找到 —— 是 RL 算法的问题(训练不够、探索不足、函数逼近能力不够、规划时域太短 ……)。
代码相关的练习(“动手改一改”)见 code/lecture06/README.md。
11. 延伸阅读 · Further reading
- CS229 main notes(Ng & Ma),Ch. 15–17:本讲的主要来源(MDP、VI / PI、learning a model、continuous states、fitted VI;LQR、DDP、LQG;REINFORCE)。
- CS229 Autumn 2018 lectures 16–20(YouTube):Andrew Ng 讲 MDP、模拟器、直升机、RL 调试的原版。
- Sutton & Barto, Reinforcement Learning: An Introduction(2nd ed., 2018):Ch. 3–4(MDP、动态规划)、Ch. 9–11(函数逼近与 deadly triad)、Ch. 13(policy gradient)。
- Puterman, Markov Decision Processes(1994);Puterman & Brumelle(1979):“On the convergence of policy iteration in stationary dynamic programming”:PI = Newton。
- Gordon(1995),“Stable function approximation in dynamic programming”;Tsitsiklis & Van Roy(1996),“Feature-based methods for large scale dynamic programming”:§3.4 的收敛与发散。
- Bertsekas, Dynamic Programming and Optimal Control:LQR、Kalman、certainty equivalence 的标准教材。
- Jacobson & Mayne(1970),Differential Dynamic Programming;Li & Todorov(2004),iLQR;Tassa, Mansard & Todorov(2014),control-limited DDP。
- Williams(1992),“Simple statistical gradient-following algorithms for connectionist reinforcement learning”:REINFORCE。
- Ng & Jordan(2000),PEGASUS;Ng et al.(2004),“Autonomous inverted helicopter flight via reinforcement learning”;Abbeel, Coates & Ng(2010),“Autonomous helicopter aerobatics through apprenticeship learning”:直升机的故事。
- Brafman & Tennenholtz(2002),R-max;Smith & Winkler(2006),“The optimizer’s curse”。
12. 课程总结 · Wrapping up CS229
这是 CS229 复习的最后一讲。回头看,大纲开头的三个想法贯穿了全部 6 讲,在本讲里也各自出现了一次:
- Loss = negative log-likelihood. 第 1 讲:选损失 = 选噪声模型,所有 canonical GLM 的梯度都是 (target − prediction) × input;第 2 讲:GDA 和 Naive Bayes 是生成模型的 MLE;第 5 讲:EM 最大化似然的下界。本讲:REINFORCE 的梯度就是”按回报加权的 log-likelihood 梯度”,得分函数恰好是第 1 讲的 GLM 梯度;计数估计 是 categorical 分布的 MLE;Kalman 滤波是高斯的条件分布。
- Model + loss + optimizer are independent choices. 第 1 讲的 normal equation / GD / Newton,第 2 讲的 SMO,第 5 讲的 EM,本讲的 VI(不动点迭代)、PI(Newton)、Riccati(精确的 DP)、policy gradient(随机梯度上升)—— 同一个目标可以换优化器;fitted VI 把”VI”和”第 1 讲的最小二乘”拼在一起。
- Fitting the training data is not the goal; generalizing is. 第 3 讲的 bias–variance、正则化、交叉验证。本讲:在学到的模型上规划会过度自信(optimizer’s curse),FVI 需要 ridge 才稳定,在模拟器里飞得好不代表在现实里飞得好(§6 的第一个问题就是在检查”对模拟器过拟合”)。
接下来学什么(都在同一个目录里):
- CS234(强化学习):本讲 §7 的表格列出了对应关系。从第 3–5 讲(TD、Q-learning、DQN)开始,补上 CS229 没讲的 model-free 方法;第 6–7 讲接着本讲 §5 讲 actor-critic 和 PPO。
- CS224N(NLP 与深度学习):第 4 讲的 MLP + backprop 之后的下一步 —— word vectors、RNN、Transformer、预训练。第 1 讲 §4.7 的”输出层 = GLM”在那里每天都在用。
- CS336(从零构建语言模型):把 Transformer、分布式训练、scaling laws、对齐(RLHF / GRPO —— 正是本讲 §5 的 policy gradient 加上 baseline)落到工程上。
- CS329H(从人类偏好中学习):Bradley–Terry 奖励模型(第 1 讲的逻辑回归)+ RLHF(本讲的 policy gradient),把第 1 讲和第 6 讲连在一起。