Part VI · 第 24 章 Model-based 方法 · 24.2Part VI · Ch. 24 Model-Based Methods · 24.2

PILCO、PETS 与 MBPOPILCO, PETS & MBPO

无模型强化学习要成千上万次试验,真机器人付不起。Model-based 方法先用每一条数据学一个动力学模型,再在模型里想办法:PILCO 用高斯过程,把状态分布解析地往前推,直接对策略求梯度;PETS 用概率网络集成,让一群粒子在不同模型里各走各的,再用 CEM 做模型预测控制;MBPO 从真实状态出发做很短的模型展开,把这些“假数据”喂给 SAC。三者回答的是同一个问题:怎样用一个处处有错的模型,又不被优化器钻它的空子。本节在浏览器里现场跑这三种方法,并看清样本效率和最终性能之间的取舍。 Model-free reinforcement learning needs thousands of trials, which a real robot cannot afford. Model-based methods first learn a dynamics model from every piece of data and then work inside the model: PILCO uses a Gaussian process, pushes the state distribution forward analytically and differentiates the expected cost with respect to the policy; PETS uses an ensemble of probabilistic networks, lets particles travel through different members, and plans with CEM in model-predictive control; MBPO branches short model rollouts off real states and feeds this synthetic data to SAC. All three answer the same question: how to use a model that is wrong in places without letting the optimizer exploit its errors. In this lesson all three run live in your browser, and you will see the trade-off between sample efficiency and final performance.

约 60 分钟~60 min 前置:24.1 学习动力学模型 · 21.3 不确定性估计 · 6.11 无梯度与采样优化 · 20.4 PPO 与 SACPrereqs: 24.1 Learning dynamics models · 21.3 Uncertainty estimation · 6.11 Sampling-based optimization · 20.4 PPO & SAC 6 个交互6 interactives

    1为什么机器人需要它Why robots need this

    20.4 节里,SAC 让一个单关节手臂学会转到目标角度,用了大约 1000 个环境步,按 0.05 s 一步算是 50 s 的“机器人时间”;PPO 用了几千到上万步。这还只是一个关节。在 MuJoCo 的四足、人形任务上,无模型方法通常要几十万到几百万步。仿真里这只是电费,真机器人上却意味着几天的磨损、复位和看护。

    无模型方法浪费的是什么?每一条转移 \((s,a,s')\) 都告诉了我们“世界怎么动”,而无模型方法只用它来改一下价值或策略,用完就基本丢了。Model-based 强化学习把这些信息存进一个动力学模型 \(\hat p(s'\mid s,a)\)(24.1 节讲了怎样学它),然后在模型里试错:想象动作序列的后果、在模型里算梯度、或者在模型里生成训练数据,只把最有希望的动作拿到真机上执行。2011 年的 PILCO 在一台真实的小车倒立摆上,只用了总共 17.5 s 的交互数据就学会了把摆甩起来并保持倒立。

    代价是:模型在数据少的地方一定会错,而优化器专门会去找“模型说好、其实不好”的地方。这就是本节三个经典方法共同面对的难题,它们的不同之处在于怎样表示模型的不确定性、怎样把它往前传,以及怎样使用模型:

    In Lesson 20.4, SAC taught a one-joint arm to turn to a target angle in about 1000 environment steps, which at 0.05 s per step is 50 s of "robot time"; PPO needed several thousand to tens of thousands. That is a single joint. On MuJoCo quadruped and humanoid tasks, model-free methods typically need hundreds of thousands to millions of steps. In simulation that only costs electricity; on a real robot it means days of wear, resets and supervision.

    What does a model-free method waste? Every transition \((s,a,s')\) says something about how the world moves, yet a model-free method uses it to nudge a value or a policy and then mostly forgets it. Model-based reinforcement learning stores this information in a dynamics model \(\hat p(s'\mid s,a)\) (Lesson 24.1 covered how to learn it) and then experiments inside the model: it imagines the consequences of action sequences, computes gradients through the model, or generates training data in it, and only takes the most promising actions to the real robot. In 2011, PILCO learned to swing up and balance a real cart-pole with a total of 17.5 s of interaction data.

    The price: the model is bound to be wrong where data are scarce, and an optimizer goes looking for exactly the places where the model says "good" and reality says otherwise. That is the problem shared by the three classic methods of this lesson. They differ in how they represent model uncertainty, how they carry it forward in time, and how they use the model:

    方法Method模型Model不确定性怎样往前传How uncertainty is carried forward模型怎样用How the model is used
    PILCO 2011高斯过程(GP)Gaussian process (GP)矩匹配:每一步都用一个高斯近似状态分布Moment matching: one Gaussian approximates the state distribution at each step对期望代价求解析梯度,优化一个参数化策略Analytic gradient of the expected cost, optimizing a parametric policy
    PETS 2018概率网络集成(5 个)Ensemble of 5 probabilistic networks轨迹采样:粒子在集成成员里各自前进Trajectory sampling: particles advance through ensemble members每一步用 CEM 规划动作序列(MPC),没有策略网络CEM plans an action sequence at every step (MPC); no policy network
    MBPO 2019概率网络集成(论文用 7 个)Ensemble of probabilistic networks (7 in the paper)每一步随机挑一个成员采样下一状态Each step samples the next state from a randomly chosen member从真实状态分叉出 k 步短展开,放进 SAC 的缓冲区k-step short rollouts branched from real states fill SAC's buffer

    2直觉:优化器会钻模型的空子Intuition: the optimizer exploits the model

    先看一个只有一个动作参数的小问题。一台腿式机器人要选步幅 \(a\)(0 到 2 之间),目标是走得最快。真实速度先随步幅增大,在 \(a=0.95\) 附近最快(0.99),步幅超过 1.45 就会摔倒(速度记为 −0.6)。机器人一开始只谨慎地试过 6 个小步幅(0 到 0.55 之间),我们用这些数据训练一个网络,预测“步幅 → 速度”。

    Model-based 的循环是:拟合模型 → 在模型里选最好的动作 → 在真机上执行 → 把结果加进数据 → 重新拟合。问题出在第二步。网络在数据之外随意外推;ReLU 网络常常顺着最后几个点的斜率一直往上画。“在模型里选最好的”就会选中外推最乐观的地方,往往是步幅 2.0,而那里机器人会摔倒。规划器越强,越能找到模型的错误:它在和模型的误差“对抗”。

    补救的办法是让模型说出自己哪里没把握。用 5 个网络组成集成(21.3 节),成员在数据附近一致,在数据外各说各的。只取集成的平均并不够:成员们往往一起乐观。可以在打分时减去分歧,悲观地选择:\(\text{score}(a)=\bar\mu(a)-2\,\sigma_{\mathrm{ens}}(a)\)。

    Start with a small problem that has a single action parameter. A legged robot must choose its stride \(a\) (between 0 and 2) to walk as fast as possible. The true speed first grows with the stride, peaks near \(a=0.95\) (at 0.99), and past a stride of 1.45 the robot falls (speed recorded as −0.6). At first the robot has cautiously tried only 6 short strides (between 0 and 0.55), and we train a network on them to predict "stride → speed".

    The model-based loop is: fit the model → pick the best action in the model → execute it on the robot → add the outcome to the data → refit. The trouble is in step two. Outside the data the network extrapolates freely; a ReLU network often keeps drawing upward along the slope of the last few points. "Pick the best in the model" then picks wherever the extrapolation is most optimistic, often a stride of 2.0, where the robot falls. The stronger the planner, the better it finds the model's errors: it is playing against the model's mistakes.

    The remedy is to let the model say where it is unsure. An ensemble of 5 networks (Lesson 21.3) agrees near the data and disagrees away from it. Taking the ensemble mean alone is not enough, since the members are often optimistic together. Instead, subtract the disagreement when scoring and choose pessimistically: \(\text{score}(a)=\bar\mu(a)-2\,\sigma_{\mathrm{ens}}(a)\).

    $$ a^\star=\arg\max_{a}\ \hl{pess}{\bar\mu(a)-\kappa\,\sigma_{\mathrm{ens}}(a)},\qquad \bar\mu(a)=\frac1K\sum_{k=1}^K\hat J_k(a),\quad \sigma^2_{\mathrm{ens}}(a)=\frac1K\sum_{k=1}^K\big(\hat J_k(a)-\bar\mu(a)\big)^2 $$

    这里 \(\hat J_k\) 是第 \(k\) 个网络预测的速度,\(\kappa=2\)。在“悲观集成”模式下,把鼠标放在公式的打分项上,下图的蓝色悲观打分曲线会加粗。三种规划器各按一次“试一次”就是一次真机试验,看看谁摔倒、谁慢慢逼近最优。

    Here \(\hat J_k\) is the speed predicted by network \(k\), and \(\kappa=2\). In "pessimistic ensemble" mode, hover the score term of the formula to thicken the blue pessimistic score curve in the figure. Each press of "Try once" is one real trial; see which planner makes the robot fall and which creeps towards the optimum.

    FIG 1规划器会钻模型的空子:步幅、速度与摔倒A planner exploits its model: stride, speed and falls
    横轴是步幅 \(a\),纵轴是速度。青色虚线是真实速度(模型看不到),右侧红色区域是会摔倒的步幅;小圆点是已经做过的试验。紫色细线是各个网络的预测(“单个网络”模式只有一条),琥珀色是它们的平均,蓝色是悲观打分 \(\bar\mu-2\sigma_{\mathrm{ens}}\)(仅“悲观集成”模式)。绿色竖线是规划器下一次要试的步幅,空心红圈标出上一次试验的真实结果。下方条形图是每次试验的真实速度(琥珀色,摔倒为红色),青色虚线是真实最优速度 0.99。网络为 [1, 32, 32, 1] ReLU,每次试验后从头训练 400 步(Adam,学习率 0.01,全批量)。 The horizontal axis is the stride \(a\), the vertical axis the speed. The dashed cyan curve is the true speed (the model never sees it), and the red region on the right marks the strides that make the robot fall; small dots are the trials done so far. Thin purple lines are the predictions of the networks (only one in "single network" mode), amber is their mean, and blue is the pessimistic score \(\bar\mu-2\sigma_{\mathrm{ens}}\) ("pessimistic ensemble" mode only). The green vertical line is the stride the planner will try next, and the hollow red ring marks the real outcome of the last trial. The bar chart below shows the real speed of each trial (amber, red for a fall), and the dashed cyan line is the true optimal speed, 0.99. Networks are [1, 32, 32, 1] ReLU, retrained from scratch for 400 steps after each trial (Adam, learning rate 0.01, full batch).
    试一试Try this
    • 选“单个网络”,一直按“试一次”,直到机器人摔倒。摔倒那一次模型预测的速度是多少?(种子 1:第 2 次试验,模型预测 1.11,比真实最优还快。种子 1–5 都在前 5 次里摔倒一次。)Select "single network" and keep pressing "Try once" until the robot falls. What speed did the model predict for that trial? (Seed 1: trial 2, with a predicted 1.11, faster than the true optimum. Seeds 1–5 all fall once within the first 5 trials.)
    • 换成“悲观集成”(会自动重置),做满 8 次试验:一次也不摔倒,且最好的一次真实速度达到 0.9 以上(种子 1–5 都能做到:0.91–0.97)。再试“集成平均”:它在 8 次里也会摔一次。Switch to "pessimistic ensemble" (it resets) and complete 8 trials with no fall while the best real speed reaches 0.9 or more (seeds 1–5 all manage it: 0.91–0.97). Then try "ensemble mean": within 8 trials it falls once too.

    注意悲观规划器的代价:它不摔倒,但往往停在比最优略小的步幅(种子 1 的 8 次里最好 0.935,真实最优 0.99),因为它从不去试“有把握之外”的地方。乐观的规划器摔一次之后,那个数据点把模型在远处“压”了下来,之后反而学得更准:被钻的空子也是一种探索,只不过代价是一次摔倒。真机器人上,摔倒的代价往往太高,这正是三个方法都要认真对待不确定性的原因。

    Note the price of pessimism: it never falls, but it tends to settle on a stride a little short of the optimum (the best of seed 1's 8 trials is 0.935, while the true optimum is 0.99), because it never tries anything outside what it is sure of. After the optimistic planner falls once, that data point pins the model down far out, and it learns more accurately afterwards: an exploited error is also a form of exploration, paid for with one fall. On a real robot a fall is often too expensive, which is why all three methods take uncertainty seriously.

    核心洞见Key insight

    模型的误差本身不可怕,可怕的是误差和优化器配合:在 \(\arg\max\) 之下,预测里“偶然偏高”的地方会被系统地挑出来,所以规划结果的预测值总是偏乐观。Model-based 方法能不能成,取决于它能不能 (1) 知道模型哪里不可信,(2) 让这些不可信的地方在规划时得不到好处,(3) 把模型用在它可信的范围内(短展开、反复重新规划)。

    Model error alone is not the danger; the danger is error combined with an optimizer. Under an \(\arg\max\), the places where the prediction happens to be too high are picked out systematically, so the predicted value of a plan is biased upward. Whether a model-based method works depends on whether it can (1) know where the model is unreliable, (2) keep those places from looking attractive to the planner, and (3) use the model only within its range of validity (short rollouts, frequent replanning).

    3动手:PETS 现场学会甩起摆Play: PETS learns a swing-up live

    本节的主要例子是经典的摆的甩起(swing-up):角度 \(\theta\) 从竖直向下量起(\(\theta=\pi\) 为倒立),\(\ddot\theta=4a-10\sin\theta-0.1\dot\theta\),力矩指令 \(|a|\le1\)。电机最多只能提供重力力矩的 40%,所以不能直接把摆抬上去,必须来回荡几次积累能量,再在顶端稳住。每一步 0.05 s,一次试验 100 步(5 s),每一步的代价是 \(c=\Delta\theta^2+0.1\dot\theta^2+0.01a^2\),\(\Delta\theta\) 是离倒立的角度。最后 1 s 都在倒立 ±0.3 rad(约 17°)以内就算成功。

    PETS(Chua、Calandra、McAllister 与 Levine,NeurIPS 2018)的名字就是它的三件东西:Probabilistic Ensembles with Trajectory Sampling。

    • 概率集成(PE):5 个网络,每个都像 21.3 节 FIG 2 那样输出下一步变化量的均值和方差(偶然不确定性),用高斯负对数似然训练;每个网络在自己的自助采样(bootstrap)数据上训练,成员之间的分歧就是认知不确定性。
    • 轨迹采样(TS):评估一个动作序列时,从当前状态放出 \(P\) 个粒子,每个粒子每一步从某个成员的预测高斯里采样下一状态。TS∞ 让每个粒子从头到尾跟着同一个成员;TS1 每一步重新随机挑成员。序列的分数是所有粒子代价的平均。
    • CEM 规划 + MPC:用 6.11 节的交叉熵法搜索未来 \(H\) 步的动作序列:采一批序列,按上面的平均代价排名,用前几名重新拟合高斯,重复几轮。只执行第一个动作,下一步重新规划(13.4 节的滚动时域)。

    每次试验结束后,把新的 100 条转移加进数据,重新训练模型,再做下一次试验。第 0 次试验用随机动作。

    The main example of this lesson is the classic pendulum swing-up: the angle \(\theta\) is measured from hanging straight down (\(\theta=\pi\) is upright), \(\ddot\theta=4a-10\sin\theta-0.1\dot\theta\), with torque command \(|a|\le1\). The motor provides at most 40% of the gravity torque, so it cannot lift the pendulum directly; it has to pump energy by swinging back and forth a few times and then catch it at the top. A step is 0.05 s, a trial is 100 steps (5 s), and the cost per step is \(c=\Delta\theta^2+0.1\dot\theta^2+0.01a^2\), where \(\Delta\theta\) is the angle from upright. A trial succeeds if the last 1 s stays within ±0.3 rad (about 17°) of upright.

    PETS (Chua, Calandra, McAllister and Levine, NeurIPS 2018) is named after its three parts: Probabilistic Ensembles with Trajectory Sampling.

    • Probabilistic ensemble (PE): 5 networks, each outputting the mean and variance of the next-step change (aleatoric uncertainty) as in FIG 2 of Lesson 21.3, trained with the Gaussian negative log-likelihood; each network trains on its own bootstrap resample of the data, and the disagreement among members is the epistemic uncertainty.
    • Trajectory sampling (TS): to score an action sequence, release \(P\) particles from the current state; at every step each particle samples its next state from the Gaussian predicted by some member. TS∞ keeps each particle with the same member from start to end; TS1 picks a random member at every step. The score of the sequence is the average cost over all particles.
    • CEM planning + MPC: the cross-entropy method of Lesson 6.11 searches over action sequences \(H\) steps into the future: sample a batch of sequences, rank them by the average cost above, refit the Gaussian to the best few, and repeat a few times. Only the first action is executed, and the next step plans again (the receding horizon of Lesson 13.4).

    After each trial the 100 new transitions are added to the data, the model is retrained, and the next trial begins. Trial 0 uses random actions.

    FIG 2PETS 现场学习摆的甩起:一次试验 5 sPETS learning a pendulum swing-up live: 5 s per trial
    真实的摆REAL PENDULUM
    每次试验的代价COST PER TRIAL
    左:琥珀色的杆是真实的摆,绿色虚线指向倒立;底部条是这一步的力矩指令。右上:每次试验的总代价(第 0 次是随机动作;“真模型”没有第 0 次),绿色柱表示成功(最后 1 s 保持倒立),蓝色柱表示没有成功(只显示最近 13 次),青色虚线是用真实动力学规划(“真模型”,\(H=20\),种子 1)一次试验的代价 431,作为参照。右下:琥珀色是当前试验离倒立的角度 \(|\Delta\theta|\) 随时间的变化,绿色虚线为 0.3 rad;紫色细线从当前时刻起,画出规划器选中的动作序列在模型里的粒子对未来 \(H\) 步的预测(每个粒子一条),只在用学到的模型规划时显示。模型:每个网络 [4, 24, 24, 4] ReLU,输入 \((\sin\theta,\cos\theta,\dot\theta/5,a)\),输出两个变化量的均值和标准差;每次试验前在全部数据上训练 400 步(每步 32 个样本)。CEM:每步 3 轮,每轮 50 个序列,前 8 名为精英,10 个粒子(“均值”传播时每个成员 1 个、不采样噪声)。仿真与规划都在浏览器里现场进行,每帧只做几毫秒的计算:用学到的模型规划时,一次 5 s 的试验在普通笔记本上大约要 10 s;第 0 次随机试验和“真模型”快得多。 Left: the amber rod is the real pendulum, and the green dashed line points to upright; the bar at the bottom is this step's torque command. Top right: the total cost of each trial (trial 0 uses random actions; the "true model" has no trial 0), green bars for successes (held upright for the last 1 s), blue bars for failures (the latest 13 are shown); the dashed cyan line is the cost of one trial planned with the true dynamics ("true model", \(H=20\), seed 1), 431, as a reference. Bottom right: in amber, the current trial's angle from upright \(|\Delta\theta|\) over time, with a green dashed line at 0.3 rad; the thin purple lines start at the current time and show the model particles' predictions for the chosen action sequence over the next \(H\) steps (one line per particle), shown only when planning with a learned model. Model: each network is [4, 24, 24, 4] ReLU with input \((\sin\theta,\cos\theta,\dot\theta/5,a)\) and outputs the mean and standard deviation of the two changes; before each trial it trains for 400 steps on all data (32 samples per step). CEM: 3 iterations per step, 50 sequences per iteration, top 8 as elites, 10 particles (with "mean" propagation, one per member and no noise sampled). Simulation and planning run live in your browser, a few milliseconds of computation per frame: when planning with a learned model, a 5 s trial takes roughly 10 s on a typical laptop; the random trial 0 and the "true model" run much faster.
    试一试Try this
    • 用默认设置(概率集成 + TS∞,\(H=20\))按“运行”,让 PETS 在 10 次试验以内第一次成功甩起并保持倒立。种子 1 在第 6 次试验成功,此前一共只用了 30 s 的数据;种子 2、3 分别在第 4、7 次。With the default settings (probabilistic ensemble + TS∞, \(H=20\)) press "Run" and let PETS succeed for the first time (swing up and hold) within 10 trials. Seed 1 succeeds on trial 6, having used only 30 s of data before it; seeds 2 and 3 on trials 4 and 7.
    • 选“真模型”(用真实动力学规划,没有学习),把视野 \(H\) 调到 10,跑完一次试验:即使模型完美,摆也甩不上去(种子 1 代价约 757)。再把 \(H\) 调到 15 以上,再跑一次:第 1 次就成功(\(H=15\) 约 474,\(H=20\) 约 431)。两次都要在本图里跑完。Select "true model" (planning with the real dynamics, no learning), set the horizon \(H\) to 10 and run one trial: even with a perfect model the pendulum does not get up (cost about 757 with seed 1). Then set \(H\) to 15 or more and run again: it succeeds on the first trial (about 474 at \(H=15\), 431 at \(H=20\)). Both runs must finish in this figure.

    几个值得注意的数字(种子 1–3,各 9 次试验):

    • 样本效率:PETS(PE + TS∞)第一次成功在第 4–7 次试验,也就是 20–35 s 的真实数据。作为对比,20.4 节的 SAC 在一个简单得多的单关节任务上也要大约 50 s。
    • 最终性能:成功之后,学到的模型给出的单次代价在 425–449 之间,接近“真模型”规划的 418–439(种子 1–3,各 8 次试验;起点随机,所以真模型的代价也会变化)。在这个低维、几乎无噪声的问题上,模型足够准,model-based 方法几乎没有输给“完美模型”。
    • 不确定性不是总有用:一个确定性网络(“单个确定性网络”)在种子 1–3 上都在第 4 次就成功,不比集成慢。这个摆的噪声极小、状态只有两维,几次试验后数据就把相关区域盖满了。PETS 论文里概率集成的优势出现在更难的任务上(7 自由度推物体、半猎豹奔跑),那里确定性模型会被规划器钻空子。
    • 规划视野:\(H=10\)(0.5 s)时,即使用真实动力学也失败:在 0.5 s 里看不出“先往反方向荡”有什么好处。模型再准,规划器也得能看到足够远。

    A few numbers worth noticing (seeds 1–3, 9 trials each):

    • Sample efficiency: PETS (PE + TS∞) first succeeds on trials 4–7, that is, after 20–35 s of real data. For comparison, SAC in Lesson 20.4 needed about 50 s on a much simpler one-joint task.
    • Final performance: after success, the per-trial cost with the learned model is 425–449, close to the 418–439 of planning with the true model (seeds 1–3, 8 trials each; the start is random, so even the true model's cost varies). On this low-dimensional, nearly noise-free problem the model is accurate enough that model-based control loses almost nothing to a perfect model.
    • Uncertainty does not always help: a deterministic network ("single deterministic net") succeeds on trial 4 for seeds 1–3, no slower than the ensemble. This pendulum has almost no noise and only two state dimensions, and a few trials cover the relevant region. In the PETS paper, the advantage of probabilistic ensembles shows on harder tasks (a 7-DoF pusher, the half-cheetah), where deterministic models get exploited by the planner.
    • Planning horizon: with \(H=10\) (0.5 s) even the true dynamics fail: within 0.5 s the planner cannot see why it should first swing the other way. However accurate the model, the planner must look far enough ahead.

    4另一个视角:不确定性怎样往前传Another view: carrying uncertainty forward

    一步预测的不确定性好办:集成给出几组均值和方差。难的是多步:第 2 步的输入已经是一个分布,而不是一个点,网络对分布输入没有现成的公式。办法大致有三类(PETS 论文把它们放在同一个框架里比较):

    • 只传均值(E):每步只用预测均值,完全丢掉不确定性。
    • 粒子(TS1、TS∞):用一群样本表示分布,每个粒子单独过模型。能表示任意形状,包括“要么往左倒、要么往右倒”这种双峰分布;代价是要很多粒子。
    • 矩匹配(MM):每一步都用一个高斯(均值和协方差)概括分布,再往前推。PILCO 对高斯过程能精确算出下一步的均值和方差;对网络,PETS 的 MM 基线用粒子估计均值和协方差,再从这个高斯重新抽粒子。它简洁、平滑,但只能是单峰的。

    下图把三种传法放在一起:模型是一个固定的概率集成(训练数据:2 次随机试验 + 2 次带噪声的甩起,共 400 条),摆从倒立附近 \(\theta_0=\pi+\delta\) 静止释放,力矩为 0,向前预测 2 s。

    One-step uncertainty is easy: the ensemble gives several means and variances. The hard part is many steps: the input of step 2 is already a distribution, not a point, and a network has no ready formula for distributional inputs. There are roughly three approaches (the PETS paper compares them in one framework):

    • Mean only (E): use only the predicted mean at each step, discarding uncertainty altogether.
    • Particles (TS1, TS∞): represent the distribution by samples, each particle passing through the model on its own. Any shape can be represented, including bimodal ones like "falls either left or right"; the price is many particles.
    • Moment matching (MM): summarize the distribution by one Gaussian (mean and covariance) at each step and push that forward. For a Gaussian process, PILCO computes the next mean and variance exactly; for networks, the MM baseline of PETS estimates mean and covariance from particles and redraws the particles from that Gaussian. It is compact and smooth, but always unimodal.

    The figure below puts the three side by side. The model is a fixed probabilistic ensemble (trained on 2 random trials plus 2 noisy swing-ups, 400 transitions); the pendulum is released at rest near upright, \(\theta_0=\pi+\delta\), with zero torque, and predicted 2 s ahead.

    FIG 3粒子与矩匹配:摆会往哪边倒?Particles versus moment matching: which way will the pendulum fall?
    纵轴是离倒立的角度 \(\theta-\pi\)(rad,向上为往右倒),横轴是时间。青色细线是真实系统的 60 次带噪声重放(同样的起点和零力矩);紫色线是 20 个模型粒子;选矩匹配时,琥珀色的线和带是那个高斯的均值和 ±2 倍标准差。离倒立超过 1 rad 记为“倒向那一边”。用滑块改变初始偏角 \(\delta\)。模型在图第一次出现时训练(800 步,约 2 s)。 The vertical axis is the angle from upright \(\theta-\pi\) (rad, positive = falling to the right), the horizontal axis time. Thin cyan lines are 60 noisy replays of the real system (same start, zero torque); purple lines are 20 model particles; with moment matching selected, the amber line and band are the mean and ±2 standard deviations of its Gaussian. Moving more than 1 rad from upright counts as "falling to that side". Use the slider to change the initial offset \(\delta\). The model trains when the figure first appears (800 steps, about 2 s).
    试一试Try this
    • 选“矩匹配”,把 \(\delta\) 放在 0.03 到 0.1 rad 之间:真实的摆 60 次全部往右倒,矩匹配却说至少 18 个粒子“还立着”,而且它的 ±2σ 带很窄。Select "moment matching" and put \(\delta\) between 0.03 and 0.1 rad: all 60 real replays fall to the right, yet moment matching says at least 18 particles "stay up", with a narrow ±2σ band.
    • 同样的 \(\delta\),换成 TS∞:粒子往左倒和往右倒的各有至少 3 个。这是“模型不知道”的诚实表达:成员们对倒立附近的动力学意见不一。With the same \(\delta\), switch to TS∞: at least 3 particles fall to the left and at least 3 to the right. This is an honest way of saying "the model does not know": the members disagree about the dynamics near upright.

    为什么矩匹配会说“还立着”?倒立是不稳定平衡点:往左倒和往右倒的粒子平均起来,均值恰好留在顶上;重新从这个高斯抽粒子,又把粒子拉回中间。单峰高斯没法表示“两边都可能,中间不可能”。TS1 每步换成员,也把成员间的分歧平均掉了不少:这里 \(\delta\) 在 0.04 到 0.09 之间时,它和矩匹配一样说 20 个粒子都还立着。PETS 论文在四个任务上比较了这些传法:模型的选择(概率集成)比传法的选择重要得多,TS1 与 TS∞ 相差不大;矩匹配在低维的倒立摆上有竞争力,在半猎豹这样的高维任务上则不可靠。

    但这并不说明矩匹配没用:在 PILCO 的设定里,它给出一个确定的、可微的期望代价,于是可以直接用梯度优化策略,这正是下一节的主题。

    Why does moment matching say "still up"? Upright is an unstable equilibrium: averaging particles that fall left with particles that fall right leaves the mean right at the top, and redrawing particles from that Gaussian pulls them back to the middle. A unimodal Gaussian cannot express "either side is possible, the middle is not". TS1, which switches members at every step, also averages away much of the members' disagreement: here, for \(\delta\) between 0.04 and 0.09, it says just like moment matching that all 20 particles stay up. The PETS paper compares these propagation schemes on four tasks: the choice of model (a probabilistic ensemble) matters much more than the choice of propagation, TS1 and TS∞ differ little, and moment matching is competitive on the low-dimensional cart-pole but unreliable on a higher-dimensional task such as the half-cheetah.

    That does not make moment matching useless: in PILCO's setting it yields a deterministic, differentiable expected cost, so the policy can be optimized directly by gradients, the topic of the next section.

    5PILCO:把策略搜索变成确定性的优化PILCO: policy search as deterministic optimization

    PILCO(Probabilistic Inference for Learning COntrol,Deisenroth 与 Rasmussen,ICML 2011)比 PETS 早七年,思路很不一样。它有一个参数化策略 \(\pi_\theta\),目标是让 \(T\) 步内的期望代价 \(J(\theta)=\sum_t\mathbb E[c(s_t)]\) 最小。三个关键选择:

    1. 高斯过程模型:用 GP 学“状态变化量 \(\Delta=s_{t+1}-s_t\)”,每个输出维度一个 GP,平方指数核。GP 天然给出预测方差:数据附近小,远处回到先验(21.3 节的贝叶斯回归是它的有限维版本)。
    2. 解析的矩匹配:如果 \(s_t\) 服从高斯,那么对平方指数核的 GP,下一步变化量的均值和协方差可以精确算出(积分有闭式解),PILCO 再用这两个矩的高斯近似 \(p(s_{t+1})\)。这样一路推下去,得到每一步状态分布的近似 \(\mathcal N(\mu_t,\Sigma_t)\),没有任何采样。
    3. 解析梯度:\(J(\theta)\) 是 \(\mu_t,\Sigma_t\) 的光滑确定函数,而 \(\mu_t,\Sigma_t\) 又是 \(\theta\) 的光滑函数。于是 \(dJ/d\theta\) 能用链式法则精确算出,交给 L-BFGS 或共轭梯度这样的确定性优化器。和 20.3 节靠采样估计、噪声很大的策略梯度相比,这是完全不同的局面。

    代价函数用的是饱和代价 \(c(s)=1-\exp\!\big(-\|s-s^\star\|^2/(2\sigma_c^2)\big)\):离目标远时代价接近 1,不再增长。它对高斯状态的期望有闭式解(第 7 节)。

    下面是一个可以完整跑起来的小版本:一辆有黏性和库仑摩擦的小车(\(\ddot x=3a-0.8v-1.2\tanh 5v\),模型看不到这个式子),要从 \(x=0\) 开到 \(x^\star=1\) 并停住,一次试验 25 步(2.5 s)。策略只有两个参数:\(a=\sin\big(w_1(x-x^\star)+w_2 v\big)\)(正弦把动作压在 \([-1,1]\) 内,PILCO 用过同样的压缩方式)。两个参数意味着我们可以把整个 \(J(w_1,w_2)\) 画成一张地图。GP 以 \((v,a)\) 为输入预测 \((\Delta x,\Delta v)\),最多用 50 个数据点。

    PILCO (Probabilistic Inference for Learning COntrol, Deisenroth and Rasmussen, ICML 2011) predates PETS by seven years and thinks quite differently. It has a parametric policy \(\pi_\theta\) and minimizes the expected cost over \(T\) steps, \(J(\theta)=\sum_t\mathbb E[c(s_t)]\). Three key choices:

    1. A Gaussian-process model: a GP learns the state change \(\Delta=s_{t+1}-s_t\), one GP per output dimension, with a squared-exponential kernel. A GP gives a predictive variance for free: small near the data, back to the prior far away (the Bayesian regression of Lesson 21.3 is its finite-dimensional cousin).
    2. Analytic moment matching: if \(s_t\) is Gaussian, then for a GP with a squared-exponential kernel the mean and covariance of the next change can be computed exactly (the integrals have closed forms), and PILCO approximates \(p(s_{t+1})\) by the Gaussian with these two moments. Repeating this gives an approximation \(\mathcal N(\mu_t,\Sigma_t)\) of the state distribution at every step, with no sampling at all.
    3. Analytic gradients: \(J(\theta)\) is a smooth deterministic function of the \(\mu_t,\Sigma_t\), which are smooth functions of \(\theta\). So \(dJ/d\theta\) follows exactly from the chain rule and can be handed to a deterministic optimizer such as L-BFGS or conjugate gradients. Compared with the sampled, noisy policy gradients of Lesson 20.3, this is a completely different situation.

    The cost is a saturating cost, \(c(s)=1-\exp\!\big(-\|s-s^\star\|^2/(2\sigma_c^2)\big)\): close to 1 far from the target, where it stops growing. Its expectation under a Gaussian state has a closed form (Section 7).

    Below is a small version that runs completely: a cart with viscous and Coulomb friction (\(\ddot x=3a-0.8v-1.2\tanh 5v\), which the model never sees) must drive from \(x=0\) to \(x^\star=1\) and stop there, in trials of 25 steps (2.5 s). The policy has just two parameters, \(a=\sin\big(w_1(x-x^\star)+w_2 v\big)\) (the sine keeps the action within \([-1,1]\); PILCO used the same kind of squashing). Two parameters mean we can draw the whole \(J(w_1,w_2)\) as a map. The GP takes \((v,a)\) as input and predicts \((\Delta x,\Delta v)\), using at most 50 data points.

    FIG 4PILCO:预测的状态分布、期望代价地图与梯度下降PILCO: the predicted state distribution, the expected-cost map and gradient descent
    小车位置 x(t)CART POSITION x(t)
    模型预测的 J(w₁, w₂)MODEL-PREDICTED J(w₁, w₂)
    左:琥珀色线和带是 PILCO 对当前策略预测的位置均值和 ±2 倍标准差(矩匹配,起点分布 \(\mathcal N(0,0.05^2)\));红线是最近一次真实试验,紫色细线是更早的试验,绿色虚线是目标 \(x^\star=1\)。右:当前 GP 模型预测的期望代价 \(J\)(越青越低),琥珀色圆圈是当前策略参数(可以拖动),紫色折线是最近一次优化的路径,空心绿圈是用真实动力学算出的最优参数(约 \((-1.83,-0.20)\),真实代价 9.48)。“优化策略”:在模型上做 60 步 Adam(学习率 0.3),梯度用确定性 \(J\) 的中心差分(真正的 PILCO 用链式法则解析求导,结果相同);“真机试验”:用当前策略跑一次真实小车,把 25 条新数据加进 GP,重算地图。第 0 次试验是随机动作。GP 的超参数在本页固定(PILCO 用证据最大化拟合它们)。 Left: the amber line and band are PILCO's predicted mean position and ±2 standard deviations under the current policy (moment matching, starting distribution \(\mathcal N(0,0.05^2)\)); the red line is the latest real trial, thin purple lines are earlier trials, and the green dashed line is the target \(x^\star=1\). Right: the expected cost \(J\) predicted by the current GP model (more cyan = lower); the amber ring is the current policy (drag it), the purple polyline the path of the latest optimization, and the hollow green ring the optimum under the true dynamics (about \((-1.83,-0.20)\), true cost 9.48). "Optimize policy" takes 60 Adam steps on the model (learning rate 0.3), with gradients from central differences of the deterministic \(J\) (real PILCO differentiates analytically with the chain rule, with the same result); "Real trial" runs the real cart once with the current policy, adds the 25 new transitions to the GP and recomputes the map. Trial 0 uses random actions. The GP hyperparameters are fixed on this page (PILCO fits them by evidence maximization).
    试一试Try this
    • 交替按“优化策略”和“真机试验”,在 3 次学习后的试验以内让真实代价低于 10。种子 1:第 1 次 11.97,第 2 次 9.92,第 3 次 9.19(真实最优 9.48 附近,带噪声的起点会让单次结果略高或略低)。每一轮看看预测带怎样变窄、预测的 J 和真实代价怎样靠近。Alternate "Optimize policy" and "Real trial" until the real cost drops below 10 within 3 learned trials. Seed 1: 11.97 on the first, 9.92 on the second, 9.19 on the third (around the true optimum of 9.48; the noisy start makes single trials a bit higher or lower). Each round, watch the predicted band narrow and the predicted J approach the real cost.
    • 按“新种子”换随机初始数据,找一个 PILCO 卡住的种子:做完 3 次学习后的试验,真实代价仍高于 15(种子 4 和 5 会这样:策略跑到 \(w_2=1.5\) 的边界,小车冲过目标)。看看它卡在地图的哪里。Press "New seed" for different initial random data and find a seed on which PILCO gets stuck: after 3 learned trials the real cost is still above 15 (seeds 4 and 5 do this: the policy runs to the boundary \(w_2=1.5\) and the cart overshoots). See where on the map it is stuck.

    种子 4 的教训很典型。第 0 次随机试验的数据只覆盖了很小的速度范围,GP 在高速处回到先验均值 0,也就是“没有摩擦、没有变化”;在这个错的模型里,“越跑越快”的正反馈策略看起来不错。第一次真机试验暴露了问题,但新数据让局部梯度把策略推向另一个错误方向,梯度法只会走到最近的谷底。PILCO 是局部方法:它高度依赖初始数据和初始策略,原论文也是从几次随机试验开始的。

    Seed 4's lesson is typical. The random data of trial 0 cover only a small range of speeds, and at high speeds the GP falls back to its prior mean of 0, meaning "no friction, no change"; in that wrong model a positive-feedback policy that keeps speeding up looks fine. The first real trial exposes the problem, but the new data steer the local gradient towards another wrong direction, and gradient descent only reaches the nearest valley. PILCO is a local method: it depends heavily on the initial data and policy, and the original paper also starts from a few random trials.

    饱和代价:不确定性何时是好事The saturating cost: when uncertainty is welcome

    PILCO 的期望代价有一个有趣的性质。设预测位置 \(x\sim\mathcal N(\mu,\sigma^2)\),饱和代价的期望是

    PILCO's expected cost has an interesting property. With the predicted position \(x\sim\mathcal N(\mu,\sigma^2)\), the expected saturating cost is

    $$ \mathbb E[c]=1-\frac{\sigma_c}{\sqrt{\sigma_c^2+\sigma^2}}\exp\!\Big(-\frac{(\mu-x^\star)^2}{2(\sigma_c^2+\sigma^2)}\Big),\qquad \frac{\partial\,\mathbb E[c]}{\partial\sigma^2}<0\iff |\mu-x^\star|>\sqrt{\sigma_c^2+\sigma^2} $$

    离目标近时,不确定性越大期望代价越高,优化器会“求稳”;离目标很远时,均值本身已经没希望,一点运气(较大的方差)反而能让部分概率落到目标附近,期望代价下降,优化器会“愿意冒险”。Deisenroth 与 Rasmussen 把这称为饱和代价自带的探索。二次代价 \((\mu-x^\star)^2+\sigma^2\) 则永远惩罚方差。

    Close to the target, more uncertainty means more expected cost and the optimizer plays safe; far from the target, the mean alone is hopeless, and a bit of luck (a larger variance) puts some probability near the target, so the expected cost falls and the optimizer is willing to gamble. Deisenroth and Rasmussen describe this as exploration built into the saturating cost. A quadratic cost \((\mu-x^\star)^2+\sigma^2\) always penalizes variance.

    FIG 5饱和代价的期望:远离目标时,不确定性反而降低期望代价The expected saturating cost: far from the target, uncertainty lowers it
    上:蓝线是代价 \(c(x)\)(饱和或二次,按钮切换;二次代价按 \(1/(2\sigma_c^2)\) 缩放,使它在目标附近与饱和代价一致),琥珀色是状态分布 \(\mathcal N(\mu,\sigma^2)\) 的密度,拖动琥珀色圆圈改变 \(\mu\),拖动紫色圆圈(位于 \(\mu+\sigma\))改变 \(\sigma\)。绿色虚线是目标 \(x^\star=1\),\(\sigma_c=0.25\)。下:期望代价 \(\mathbb E[c]\) 随 \(\sigma\) 的变化(\(\mu\) 固定),紫色圆点是当前 \(\sigma\)。 Top: the blue line is the cost \(c(x)\) (saturating or quadratic, switch with the buttons; the quadratic cost is scaled by \(1/(2\sigma_c^2)\) so that it matches the saturating cost near the target), and amber is the density of the state distribution \(\mathcal N(\mu,\sigma^2)\); drag the amber ring to change \(\mu\) and the purple ring (at \(\mu+\sigma\)) to change \(\sigma\). The green dashed line is the target \(x^\star=1\), with \(\sigma_c=0.25\). Bottom: the expected cost \(\mathbb E[c]\) as a function of \(\sigma\) (with \(\mu\) fixed); the purple dot is the current \(\sigma\).
    试一试Try this
    • 用饱和代价,分别找到一个“加大 \(\sigma\) 会降低期望代价”的状态(例如 \(\mu=0\),\(\sigma=0.2\):\(|\mu-x^\star|=1>0.32\))和一个“加大 \(\sigma\) 会提高期望代价”的状态(\(\mu\) 靠近 1)。再切到二次代价:无论 \(\mu\) 在哪里,加大 \(\sigma\) 都只会提高期望代价。三种情况都要在读数里看到。With the saturating cost, find one state where a larger \(\sigma\) lowers the expected cost (for example \(\mu=0\), \(\sigma=0.2\): \(|\mu-x^\star|=1>0.32\)) and one where it raises it (\(\mu\) near 1). Then switch to the quadratic cost: wherever \(\mu\) is, a larger \(\sigma\) only raises the expected cost. All three cases must show up in the readout.

    6MBPO:用短的模型展开喂 SACMBPO: feeding SAC with short model rollouts

    PETS 每一步都要做大量网络前向(本页 FIG 2 每步 3 万次),规划器就是策略,部署时也得一直规划;PILCO 的 GP 计算量随数据量的立方增长,难以扩展到高维。MBPO(Model-Based Policy Optimization,Janner、Fu、Zhang 与 Levine,NeurIPS 2019)换了一种用法:模型只负责造数据,策略学习交给 SAC(20.4 节)。

    关键问题是:在模型里展开多长?从初始状态在模型里跑完整个回合,误差会一路累积(24.1 节的复合误差),策略学到的是模型的错误。MBPO 的做法是分叉展开(branched rollout):从真实缓冲区里随机取一个真实状态,用当前策略在模型里只走 \(k\) 步(论文里很多任务 \(k=1\),有的随训练从 1 增加到 15 或 25),把这些短轨迹放进模型缓冲区。短展开的误差小,又因为起点来自真实数据,覆盖的是策略真正会去的状态。模型数据很多,于是每个真实环境步可以做 20–40 次 SAC 更新,而普通 SAC 一般只做 1 次。

    论文的理论部分给出一个下界:真实回报 \(\ge\) 分叉展开下的回报 − 一个惩罚项,惩罚里与模型误差相关的部分随 \(k\) 线性增长,与策略变化相关的部分随 \(\gamma^k\) 衰减(第 7 节)。模型越准,最优的 \(k\) 越大。

    下图在 20.4 节同一个单关节任务上比较三个学习者。SAC 的代码、网络、评估都和 20.4 节完全相同;“MBPO”用 5 个概率网络的集成(结构与 FIG 2 相同,隐藏层宽 32:[4, 32, 32, 4]),前 200 步随机动作,之后每 100 步重新训练模型,每个真实步从真实状态分叉出 10 条长 \(k\) 的展开,SAC 只从模型缓冲区采样,每个真实步更新 \(G=10\) 次。

    PETS runs a great many network evaluations per step (30,000 in FIG 2 on this page); the planner is the policy, so it must keep planning when deployed. PILCO's GP costs grow with the cube of the amount of data and are hard to scale to high dimensions. MBPO (Model-Based Policy Optimization, Janner, Fu, Zhang and Levine, NeurIPS 2019) uses the model differently: the model only generates data, and policy learning is left to SAC (Lesson 20.4).

    The key question is how long to roll out in the model. Running whole episodes in the model from the initial state lets errors pile up (the compounding error of Lesson 24.1), and the policy learns the model's mistakes. MBPO uses branched rollouts: take a random real state from the real buffer, run the current policy in the model for only \(k\) steps (\(k=1\) on many tasks in the paper, on others growing from 1 to 15 or 25 during training), and put these short trajectories in a model buffer. Short rollouts have small errors, and since they start from real data they cover the states the policy actually visits. Model data are plentiful, so each real environment step can afford 20–40 SAC updates where ordinary SAC usually does 1.

    The paper's theory gives a lower bound: true return \(\ge\) return under branched rollouts minus a penalty; the part of the penalty due to model error grows linearly in \(k\), and the part due to policy change decays like \(\gamma^k\) (Section 7). The more accurate the model, the larger the best \(k\).

    The figure compares three learners on the same one-joint task as Lesson 20.4. SAC's code, networks and evaluation are exactly those of 20.4; "MBPO" uses an ensemble of 5 probabilistic networks (the same structure as FIG 2, with 32 hidden units: [4, 32, 32, 4]), takes random actions for the first 200 steps, retrains the model every 100 steps, branches 10 rollouts of length \(k\) from real states at every real step, and lets SAC sample only from the model buffer, with \(G=10\) updates per real step.

    FIG 6MBPO、SAC 与高更新比 SAC:数一数真实环境步MBPO, SAC and high-update-ratio SAC: counting real environment steps
    评估误差(°,对数)vs 真实环境步EVAL ERROR (°, log) vs REAL ENV STEPS
    模型展开 k 步后的角度误差(°)ANGLE ERROR (°) AFTER k MODEL STEPS
    左:每 100 个真实环境步评估一次(24 个固定的起点–目标对,确定性策略,最终角度误差的平均,与 20.4 节相同),纵轴对数。琥珀色 = MBPO,紫色 = SAC,\(G=10\),不用模型(只在真实数据上多更新),蓝色 = SAC,\(G=1\)(20.4 节的设置,但预热只有 200 步)。正在训练的那条线较粗,每种学习者保留最近一次的曲线;绿色虚线是 8°。右(只对 MBPO):从真实缓冲区里取 30 个状态,用当前策略同时在模型和真实手臂上走 1–20 步,两者角度之差的平均,每 100 步更新一次;红色竖线标出当前的 \(k\)。每帧只做几毫秒的计算,MBPO 和 SAC \(G=10\) 跑 800 个真实步在普通笔记本上大约需要 20–30 s,SAC \(G=1\) 跑 1500 步约 5 s。 Left: evaluation every 100 real environment steps (24 fixed start–target pairs, deterministic policy, mean final angle error, as in Lesson 20.4), log scale. Amber = MBPO, purple = SAC with \(G=10\) and no model (more updates on real data only), blue = SAC with \(G=1\) (the setting of 20.4, but with only 200 warm-up steps). The run in progress is drawn thicker, and each learner keeps its latest curve; the green dashed line is 8°. Right (MBPO only): 30 states from the real buffer, the current policy run for 1–20 steps both in the model and on the real arm, and the mean difference of their angles, updated every 100 steps; the red vertical line marks the current \(k\). With a few milliseconds of computation per frame, MBPO and SAC with \(G=10\) take roughly 20–30 s for 800 real steps on a typical laptop, and SAC with \(G=1\) about 5 s for 1500.
    试一试Try this
    • 选 MBPO(\(k=1\),种子 1)按“训练”:在 600 个真实步以内把评估误差降到 8° 以下(种子 1 在第 300 步就做到了)。Select MBPO (\(k=1\), seed 1) and press "Train": get the evaluation error below 8° within 600 real steps (seed 1 does it by step 300).
    • 同一个种子下,三种学习者各跑完一次(MBPO 与 SAC \(G=10\) 跑到 800 步,SAC \(G=1\) 跑到 1500 步),比较它们第一次低于 8° 的步数。With the same seed, run each of the three learners to the end once (MBPO and SAC \(G=10\) to 800 steps, SAC \(G=1\) to 1500), and compare the step at which each first drops below 8°.
    • MBPO 跑到第 200 步(第一次训练模型之后)时看右图:模型走 20 步后的误差是走 1 步的 3 倍以上(种子 1:0.49° 对 2.95°)。到第 800 步,同样的比值变成多少?When MBPO reaches step 200 (right after the model's first training), look at the right plot: the error after 20 model steps is more than 3 times the error after 1 step (seed 1: 0.49° versus 2.95°). What is the same ratio at step 800?
    诚实的对照组An honest control group

    种子 1–4 第一次低于 8° 的真实步数:SAC \(G=1\) 为 800、1000、1300、800;MBPO(\(k=1\))为 300、800 步内未做到(最低 12.0°)、500、500;MBPO(\(k=20\))为 400、600、300、300;而不用模型、只把 SAC 的更新比提到 \(G=10\) 是 300、500、400、400。在这个小任务上,MBPO 相对普通 SAC 的大部分好处来自“每个真实样本多更新几次”,模型本身没有再多帮什么:手臂的状态只有两维,几百步的真实数据就覆盖了策略会去的地方。MBPO 论文在 Hopper 上做过同样的对照,高更新比的 SAC 追不上 MBPO;但后来的 REDQ(Chen 等,ICLR 2021)用 Q 函数集成和高更新比,在同样的 MuJoCo 基准上不用模型就达到了 MBPO 的样本效率。做 model-based 的实验时,一定要放一个同样更新比的无模型对照组。右图也提醒我们:复合误差不只取决于模型,也取决于闭环。这个手臂的闭环是稳定的,策略把状态拉向目标,模型误差就不会一直放大。

    Real steps at which seeds 1–4 first drop below 8°: SAC with \(G=1\): 800, 1000, 1300, 800; MBPO (\(k=1\)): 300, not within 800 steps (best 12.0°), 500, 500; MBPO (\(k=20\)): 400, 600, 300, 300; and SAC without a model, only with its update ratio raised to \(G=10\): 300, 500, 400, 400. On this small task, most of MBPO's gain over plain SAC comes from "more updates per real sample"; the model adds little on top: the arm's state is two-dimensional, and a few hundred real steps cover where the policy goes. The MBPO paper ran the same control on Hopper, where high-ratio SAC could not keep up with MBPO; but the later REDQ (Chen et al., ICLR 2021) used an ensemble of Q-functions with a high update ratio and matched MBPO's sample efficiency on the same MuJoCo benchmarks without a model. When experimenting with model-based methods, always include a model-free control with the same update ratio. The right plot is a reminder too: compounding error depends on the closed loop, not only on the model. This arm's closed loop is stable, the policy pulls the state towards the target, and model errors do not keep growing.

    样本效率 vs 最终性能Sample efficiency vs final performance

    Model-based 方法的传统名声是“学得快、学不到顶”:模型的偏差会限制最终性能(PETS 和 MBPO 的论文开头都是这么说的)。PETS 的贡献是用不确定性感知的模型在若干基准任务上追平了无模型方法的最终表现(半猎豹上比 SAC 少用 8 倍、比 PPO 少用 125 倍样本);MBPO 的贡献是用短展开把模型偏差限制在可控范围,同时保留 SAC 的最终性能(Ant 上 30 万步达到 SAC 300 万步的水平)。本页的摆和手臂上也是一样:学到的模型最终和完美模型、和无模型方法表现相当。差距会在模型难学的地方重新出现:接触、高维观测、长时程。

    Model-based methods have the traditional reputation of "learning fast but plateauing low": model bias limits final performance (both the PETS and MBPO papers open with this). PETS's contribution was to match the final performance of model-free methods on several benchmark tasks with uncertainty-aware models (8 times fewer samples than SAC and 125 times fewer than PPO on the half-cheetah); MBPO's was to keep model bias in check with short rollouts while retaining SAC's final performance (on Ant, 300 thousand steps reach the level of SAC at 3 million). The pendulum and arm on this page agree: the learned models end up as good as a perfect model and as model-free learning. The gap reappears where models are hard to learn: contact, high-dimensional observations, long horizons.

    7再看数学The math

    提示:把鼠标悬停在公式中带底色的符号上,对应的图元素会被高亮:悲观打分加粗 FIG 1 的蓝色曲线(“悲观集成”模式),粒子 \(s^p_\tau\) 加粗 FIG 2 和 FIG 3 的紫色粒子轨迹,矩匹配的 \(\mu_t,\Sigma_t\) 加粗 FIG 3 的琥珀色带和 FIG 4 的预测带,\(\sigma_c\) 加粗 FIG 5 的代价曲线,\(k\) 加粗 FIG 6 右图的红色竖线。Tip: hover the highlighted symbols in the formulas and the matching figure element lights up: the pessimistic score thickens FIG 1's blue curve (in "pessimistic ensemble" mode), the particles \(s^p_\tau\) thicken the purple particle paths in FIG 2 and FIG 3, the moment-matched \(\mu_t,\Sigma_t\) outline FIG 3's amber band and FIG 4's predicted band, \(\sigma_c\) thickens FIG 5's cost curve, and \(k\) thickens the red vertical line in FIG 6 (right).

    共同的骨架The common skeleton

    本节用代价 \(c(s,a)\)(即负奖励 \(-r\))。三种方法都在重复同一个循环:(1) 用目前所有真实数据 \(\mathcal D\) 拟合模型 \(\hat p(s'\mid s,a)\);(2) 在模型里改进控制器;(3) 用新控制器在真机上采数据,并入 \(\mathcal D\)。区别只在 (1) 里的不确定性表示和 (2) 的做法。

    This section uses a cost \(c(s,a)\) (the negative reward \(-r\)). All three methods repeat the same loop: (1) fit a model \(\hat p(s'\mid s,a)\) to all real data \(\mathcal D\) so far; (2) improve the controller inside the model; (3) collect data on the real robot with the new controller and add it to \(\mathcal D\). They differ only in how (1) represents uncertainty and how (2) is done.

    PETS:概率集成 + 轨迹采样 + CEMPETS: probabilistic ensemble + trajectory sampling + CEM

    集成成员 \(b=1,\dots,B\) 各输出一个对角高斯 \(\hat p_b(s'\mid s,a)=\mathcal N\big(\mu_b(s,a),\Sigma_b(s,a)\big)\),在自己的自助样本 \(\mathcal D_b\) 上最小化负对数似然(21.3 节)。在时刻 \(t\),对候选动作序列 \(\mathbf a=a_{t:t+H-1}\) 放出 \(P\) 个粒子:

    Ensemble members \(b=1,\dots,B\) each output a diagonal Gaussian \(\hat p_b(s'\mid s,a)=\mathcal N\big(\mu_b(s,a),\Sigma_b(s,a)\big)\) and minimize the negative log-likelihood on their own bootstrap sample \(\mathcal D_b\) (Lesson 21.3). At time \(t\), release \(P\) particles for a candidate action sequence \(\mathbf a=a_{t:t+H-1}\):

    $$ \hl{ts}{s^p_{\tau+1}}\sim\hat p_{b(p,\tau)}\big(\cdot\mid s^p_\tau,a_\tau\big),\quad s^p_t=s_t,\qquad \hat C(\mathbf a)=\frac1P\sum_{p=1}^P\sum_{\tau=t}^{t+H-1}c\big(s^p_{\tau+1},a_\tau\big),\qquad b(p,\tau)=\begin{cases}b(p) & \text{TS}\infty\\ \text{uniform on }\{1..B\} & \text{TS1}\end{cases} $$

    CEM 在 \(\mathbf a\) 上维护一个对角高斯,每轮采 \(N\) 个序列、取 \(\hat C\) 最小的 \(E\) 个精英重新拟合均值和方差(6.11 节),最后执行均值序列的第一个动作。论文里 \(B=5\)、\(P=20\)。平均 \(\hat C\) 是对模型不确定性的风险中性处理:成员分歧大的序列不会被额外惩罚,只是它的平均代价通常因为“有些粒子表现很差”而变高。FIG 1 的悲观打分则是风险厌恶的;离线强化学习(20.5 节讲了它的无模型方法)里也有 model-based 的做法,例如 MOPO(Yu 等,NeurIPS 2020),就是在奖励里显式减去模型的不确定性。

    CEM keeps a diagonal Gaussian over \(\mathbf a\); each iteration samples \(N\) sequences, takes the \(E\) elites with the smallest \(\hat C\) and refits the mean and variance (Lesson 6.11); finally the first action of the mean sequence is executed. The paper uses \(B=5\) and \(P=20\). Averaging \(\hat C\) is a risk-neutral treatment of model uncertainty: a sequence on which the members disagree is not penalized as such; its average cost is just usually higher because some particles do badly. FIG 1's pessimistic score is risk-averse; offline RL (Lesson 20.5 covers its model-free methods) also has model-based methods, such as MOPO (Yu et al., NeurIPS 2020), which explicitly subtract the model's uncertainty from the reward.

    PILCO:矩匹配与解析梯度PILCO: moment matching and analytic gradients

    GP 的输入是 \(\tilde x=(s,a)\),平方指数核 \(k(\tilde x,\tilde x')=\alpha^2\exp\!\big(-\tfrac12(\tilde x-\tilde x')^\top\Lambda^{-1}(\tilde x-\tilde x')\big)\),\(\boldsymbol\beta=(K+\sigma_n^2I)^{-1}\mathbf y\)。若输入 \(\tilde x\sim\mathcal N(\tilde\mu,\tilde\Sigma)\),预测均值对输入分布的积分有闭式解:

    The GP input is \(\tilde x=(s,a)\), with squared-exponential kernel \(k(\tilde x,\tilde x')=\alpha^2\exp\!\big(-\tfrac12(\tilde x-\tilde x')^\top\Lambda^{-1}(\tilde x-\tilde x')\big)\) and \(\boldsymbol\beta=(K+\sigma_n^2I)^{-1}\mathbf y\). If the input is \(\tilde x\sim\mathcal N(\tilde\mu,\tilde\Sigma)\), the predictive mean integrated over the input distribution has a closed form:

    $$ \mathbb E[\Delta]=\boldsymbol\beta^\top\mathbf q,\qquad q_i=\frac{\alpha^2}{\sqrt{\big|\tilde\Sigma\Lambda^{-1}+I\big|}}\exp\!\Big(-\tfrac12(\tilde x_i-\tilde\mu)^\top(\tilde\Sigma+\Lambda)^{-1}(\tilde x_i-\tilde\mu)\Big) $$

    \(q_i\) 就是核函数被输入的不确定性“抹宽”之后的样子:\(\Lambda\) 变成 \(\tilde\Sigma+\Lambda\)。方差和输入–输出协方差也有类似的闭式解(包含一个 \(n\times n\) 的矩阵,见 Deisenroth 的博士论文),本页 FIG 4 用的就是这些公式,它们与 4 万个样本的蒙特卡洛估计在采样误差(约 1%)以内一致。把 \(\Delta\) 的矩和 \(s_t\) 合起来,得到 \(p(s_{t+1})\approx\mathcal N(\mu_{t+1},\Sigma_{t+1})\):

    \(q_i\) is the kernel "smeared out" by the input uncertainty: \(\Lambda\) becomes \(\tilde\Sigma+\Lambda\). The variance and the input–output covariance have similar closed forms (involving an \(n\times n\) matrix; see Deisenroth's PhD thesis); FIG 4 on this page uses exactly these formulas, which agree with a 40,000-sample Monte Carlo estimate to within its sampling error (about 1%). Combining the moments of \(\Delta\) with \(s_t\) gives \(p(s_{t+1})\approx\mathcal N(\mu_{t+1},\Sigma_{t+1})\):

    $$ \hl{mm}{\mu_{t+1}}=\mu_t+\mathbb E[\Delta],\qquad \hl{mm}{\Sigma_{t+1}}=\Sigma_t+\operatorname{Cov}[\Delta]+\operatorname{Cov}[s_t,\Delta]+\operatorname{Cov}[\Delta,s_t],\qquad J(\theta)=\sum_{t=1}^T\mathbb E_{s_t\sim\mathcal N(\mu_t,\Sigma_t)}\big[c(s_t)\big] $$

    梯度沿时间递推:\(\dfrac{dJ}{d\theta}=\sum_t\Big(\dfrac{\partial\mathbb E[c_t]}{\partial\mu_t}\dfrac{d\mu_t}{d\theta}+\dfrac{\partial\mathbb E[c_t]}{\partial\Sigma_t}\dfrac{d\Sigma_t}{d\theta}\Big)\),其中 \(\dfrac{d\mu_t}{d\theta}=\dfrac{\partial\mu_t}{\partial\mu_{t-1}}\dfrac{d\mu_{t-1}}{d\theta}+\dfrac{\partial\mu_t}{\partial\Sigma_{t-1}}\dfrac{d\Sigma_{t-1}}{d\theta}+\dfrac{\partial\mu_t}{\partial\theta}\)(\(\Sigma_t\) 同理)。每个偏导都来自上面的闭式公式。这就是 16.3 节的反向传播沿时间展开(链式法则一层层往回乘),只是沿时间传的是“分布的矩”而不是一个状态。饱和代价的期望是

    The gradient follows a recursion in time: \(\dfrac{dJ}{d\theta}=\sum_t\Big(\dfrac{\partial\mathbb E[c_t]}{\partial\mu_t}\dfrac{d\mu_t}{d\theta}+\dfrac{\partial\mathbb E[c_t]}{\partial\Sigma_t}\dfrac{d\Sigma_t}{d\theta}\Big)\), with \(\dfrac{d\mu_t}{d\theta}=\dfrac{\partial\mu_t}{\partial\mu_{t-1}}\dfrac{d\mu_{t-1}}{d\theta}+\dfrac{\partial\mu_t}{\partial\Sigma_{t-1}}\dfrac{d\Sigma_{t-1}}{d\theta}+\dfrac{\partial\mu_t}{\partial\theta}\) (and likewise for \(\Sigma_t\)). Every partial derivative comes from the closed forms above. This is the back-propagation of Lesson 16.3 unrolled through time (the chain rule multiplied back step by step), except that what travels through time is the moments of a distribution rather than a single state. The expected saturating cost is

    $$ \mathbb E\big[c(s)\big]=1-\big|I+\Sigma W\big|^{-1/2}\exp\!\Big(-\tfrac12(\mu-s^\star)^\top W(I+\Sigma W)^{-1}(\mu-s^\star)\Big),\qquad W=\hl{sat}{\sigma_c}^{-2}I\ \text{(on the penalized coordinates)} $$

    一维时就是第 5 节的公式。对 \(\sigma^2\) 求导:\(\partial\log(1-\mathbb E[c])/\partial\sigma^2=\big((\mu-x^\star)^2-(\sigma_c^2+\sigma^2)\big)/\big(2(\sigma_c^2+\sigma^2)^2\big)\),所以 \(|\mu-x^\star|>\sqrt{\sigma_c^2+\sigma^2}\) 时,方差越大期望代价越小。

    In one dimension this is the formula of Section 5. Differentiating with respect to \(\sigma^2\): \(\partial\log(1-\mathbb E[c])/\partial\sigma^2=\big((\mu-x^\star)^2-(\sigma_c^2+\sigma^2)\big)/\big(2(\sigma_c^2+\sigma^2)^2\big)\), so when \(|\mu-x^\star|>\sqrt{\sigma_c^2+\sigma^2}\), more variance means less expected cost.

    MBPO:分叉展开的回报下界MBPO: a return bound for branched rollouts

    设 \(\eta[\pi]\) 为真实回报,\(\eta^{\text{branch}}[\pi]\) 为“先按采数据的策略在真实环境里走,再在某处分叉、用当前策略在模型里走 \(k\) 步”得到的回报;\(\epsilon_\pi\) 是新旧策略的最大总变差距离,\(\epsilon_{m'}\) 是模型在新策略状态分布上的总变差误差,\(r_{\max}\) 是最大奖励。MBPO 的定理 4.3 给出:

    Let \(\eta[\pi]\) be the true return and \(\eta^{\text{branch}}[\pi]\) the return of "follow the data-collecting policy in the real environment, branch at some point, and run the current policy in the model for \(k\) steps"; \(\epsilon_\pi\) is the largest total-variation distance between the old and new policies, \(\epsilon_{m'}\) the model's total-variation error on the new policy's state distribution, and \(r_{\max}\) the largest reward. Theorem 4.3 of MBPO states:

    $$ \eta[\pi]\ \ge\ \eta^{\text{branch}}[\pi]-2r_{\max}\Big[\frac{\gamma^{\hl{kroll}{k}+1}\epsilon_\pi}{(1-\gamma)^2}+\frac{\gamma^{\hl{kroll}{k}}\epsilon_\pi}{1-\gamma}+\frac{\hl{kroll}{k}}{1-\gamma}\,\epsilon_{m'}\Big] $$

    前两项随 \(k\) 指数减小(在模型里走得越久,越少依赖旧策略采来的数据),最后一项随 \(k\) 线性增大(模型误差一步步累积)。只有当 \(\epsilon_{m'}\) 足够小时,最小化这个惩罚的 \(k\) 才大于 0,也就是说“用模型”才划算。论文还用实验测量了模型误差随策略偏移增长得有多快:数据越多,增长越慢,这正是支撑“短而非零的展开”的证据。

    The first two terms shrink exponentially in \(k\) (the longer you run in the model, the less you rely on data from the old policy); the last grows linearly in \(k\) (model error accumulates step by step). Only when \(\epsilon_{m'}\) is small enough is the \(k\) that minimizes the penalty greater than 0, that is, only then does using the model pay. The paper also measured empirically how fast model error grows with policy shift: the more data, the slower the growth, which is the evidence behind "short but nonzero rollouts".

    8在机器人中In robotics

    真实系统与真实数字Real systems, real numbers

    PILCO:ICML 2011 论文在一台真实的小车倒立摆(小车 0.7 kg,摆 0.325 kg,水平力不超过 10 N)上从零学会甩起并保持倒立,策略是 50 个径向基函数的网络(305 个参数),总共只用了 17.5 s 的交互;同一篇论文还学会了小车双摆的甩起(约 60–90 s 交互)和仿真独轮车的平衡(约 30 s)。同年 RSS 的工作(Deisenroth、Rasmussen 与 Fox)用 PILCO 让一台低成本机械臂在 Kinect 视觉反馈下学会堆积木。Deisenroth、Fox 与 Rasmussen 在 TPAMI 2015 上总结了这条路线。PILCO 的主要局限是 GP 的计算量(随数据点数立方增长)和对状态维数的扩展性,以及策略搜索的局部性。

    PILCO: the ICML 2011 paper learned the swing-up and balancing of a real cart-pole (cart 0.7 kg, pendulum 0.325 kg, horizontal force at most 10 N) from scratch with a policy of 50 radial basis functions (305 parameters), using a total of 17.5 s of interaction; the same paper also learned a cart-double-pendulum swing-up (about 60–90 s of interaction) and balancing a simulated unicycle (about 30 s). Work at RSS the same year (Deisenroth, Rasmussen and Fox) used PILCO to teach a low-cost manipulator to stack blocks with Kinect visual feedback. Deisenroth, Fox and Rasmussen summarized the line of work in TPAMI 2015. PILCO's main limits are the GP's computational cost (cubic in the number of data points), its scaling to many state dimensions, and the locality of its policy search.

    PETS:在 MuJoCo 的倒立摆、7 自由度手臂够取(reacher)、7 自由度推物体(pusher)和半猎豹上,100 次试验以内达到或接近无模型方法的最终表现;只有在低维的倒立摆上,基于 GP 的方法(矩匹配)略胜一筹。在它之前,Nagabandi 等(ICRA 2018)已经用确定性网络 + 随机采样的 MPC 做运动控制,再用无模型方法微调;之后,PDDM(Nagabandi 等,CoRL 2019)把集成模型和改进的采样规划器用到灵巧手操作上,包括在真实的 ShadowHand 上做实验。PETS 风格的“学模型 + 采样 MPC”也是 24.4 节的出发点。

    PETS: on MuJoCo's cart-pole, a 7-DoF reacher, a 7-DoF pusher and the half-cheetah it reached or approached the final performance of model-free methods within 100 trials; only on the low-dimensional cart-pole was a GP-based method (with moment matching) slightly better. Before it, Nagabandi et al. (ICRA 2018) had used deterministic networks with random-shooting MPC for locomotion, followed by model-free fine-tuning; afterwards PDDM (Nagabandi et al., CoRL 2019) brought ensemble models and an improved sampling planner to dexterous manipulation, including experiments on a real ShadowHand. PETS-style "learned model + sampling MPC" is also the starting point of Lesson 24.4.

    MBPO:在 MuJoCo 上,每个真实步做 400 次模型展开、20–40 次策略更新,集成 7 个网络(4 层隐藏层,每层 200 或 400 个单元)。论文指出,Hopper 和 Walker2d 所需的数据如果按真实时间运行,分别相当于 14 和 40 分钟;在 Ant 这样纯 model-based 方法(如 PETS)难以应付的高维任务上也能学好。同一思路被后来的许多方法沿用。把模型换成在潜在空间里学的“世界模型”、把短展开换成在想象中学习,就是 24.3 节的 Dreamer 和 TD-MPC;离线情形下,MOPO 在 MBPO 式的展开上给奖励减去不确定性惩罚,MOReL 则构造一个在集成意见不一的地方“终止并重罚”的悲观模型。

    MBPO: on MuJoCo it does 400 model rollouts and 20–40 policy updates per real step, with an ensemble of 7 networks (4 hidden layers of 200 or 400 units). The paper notes that the data it needs on Hopper and Walker2d would take 14 and 40 minutes respectively if the simulator ran in real time, and that it also learns high-dimensional tasks such as Ant that trouble purely model-based methods like PETS. Many later methods reuse the idea. Learning the model in a latent space as a "world model" and replacing short rollouts by learning in imagination leads to Dreamer and TD-MPC in Lesson 24.3; in the offline setting, MOPO subtracts an uncertainty penalty from the reward of MBPO-style rollouts, and MOReL builds a pessimistic model that halts with a large penalty wherever the ensemble disagrees.

    怎么选:几十个参数、状态维数低、每次试验都很贵(真机、易损):PILCO 式的 GP 方法仍然是最省数据的。状态几十维、能接受在线计算:PETS 式的“集成 + 采样 MPC”简单可靠,模型可以随时更新。需要一个快速执行的策略网络、或任务较长:MBPO 式地用模型给无模型算法加速,同时记得放一个同等更新比的无模型对照。

    Choosing: a few dozen parameters, a low-dimensional state, and expensive trials (real hardware, fragile): PILCO-style GP methods are still the most data-frugal. Tens of state dimensions and online computation acceptable: PETS-style "ensemble + sampling MPC" is simple and robust, and the model can be updated at any time. A fast policy network is needed, or tasks are longer: use the model to speed up a model-free algorithm as MBPO does, and remember a model-free control with the same update ratio.

    9常见误区Common pitfalls

    误区 1:相信规划器给出的预测回报Pitfall 1: trusting the planner's predicted return

    规划的结果是在模型上取了 \(\arg\max\) 的,它的预测值系统性偏乐观(FIG 1:单个网络在摔倒那次预测速度 1.11,真实是 −0.6)。评估一个 model-based 方法,要看真机或真仿真里的回报,而不是模型里的回报;调参时也要把“模型里的回报”和“真实回报”画在一起,差距变大是模型被钻空子的信号。

    A plan is the \(\arg\max\) over a model, so its predicted value is biased upward (FIG 1: the single network predicted a speed of 1.11 for the trial in which the robot fell, at −0.6). Judge a model-based method by its return on the real robot or the real simulator, not inside the model; when tuning, plot the model's return next to the real one, since a widening gap is the signature of an exploited model.

    误区 2:以为模型准了就够了Pitfall 2: thinking an accurate model is enough

    FIG 2 里,即使用完美的动力学,规划视野 \(H=10\) 也甩不起摆:规划器看不到先反向荡的好处。采样规划器的样本数、迭代数、视野和代价设计同样决定结果;“真模型”这个上限值得先测一下,它告诉你问题出在模型还是出在规划。

    In FIG 2, even the perfect dynamics cannot swing the pendulum up with horizon \(H=10\): the planner cannot see the benefit of first swinging the other way. A sampling planner's population, iterations, horizon and cost design matter just as much; measuring the "true model" ceiling first tells you whether a failure comes from the model or from the planning.

    误区 3:用单峰高斯描述会分叉的未来Pitfall 3: describing a branching future with one Gaussian

    矩匹配把“往左倒或往右倒”平均成“立在中间”,而且给出一个很窄的带(FIG 3)。在不稳定平衡点、接触的有无、绕过障碍物的左右这类地方,未来天然是多峰的。用粒子,或者至少检查一下粒子分布再决定是否可以用高斯近似。

    Moment matching averages "falls left or falls right" into "stands in the middle", with a narrow band (FIG 3). Near unstable equilibria, with or without contact, passing an obstacle on the left or the right, the future is naturally multimodal. Use particles, or at least look at the particle distribution before deciding that a Gaussian will do.

    误区 4:在模型里展开太长Pitfall 4: rolling out too long in the model

    从初始状态在模型里跑完整个回合,误差会复合(24.1 节),策略会学会利用这些误差。MBPO 从真实状态分叉、只走几步;论文报告在 Hopper 上 \(k=1\) 就保住了大部分收益,200 步的展开表现更差,500 步已经不足以学习。FIG 6 右图显示,模型刚训练完时 20 步误差是 1 步误差的 6 倍。要看的是你的闭环:不稳定的闭环会放大误差,稳定的闭环会把它收回来。

    Running whole episodes in the model from the initial state lets errors compound (Lesson 24.1) and the policy learns to exploit them. MBPO branches from real states and takes only a few steps; the paper reports that on Hopper \(k=1\) keeps most of the benefit, 200-step rollouts do worse, and 500-step rollouts are too inaccurate to learn from. FIG 6 (right) shows the 20-step error at 6 times the 1-step error right after the model is first trained. What matters is your closed loop: an unstable one amplifies errors, a stable one pulls them back.

    误区 5:没有公平的无模型对照Pitfall 5: no fair model-free control

    MBPO 每个真实步做 20–40 次更新(论文;FIG 6 里是 10 次),普通 SAC 只做 1 次。把“用模型”的收益和“多更新”的收益分开,需要一个同样更新比的无模型对照组:FIG 6 里 SAC \(G=10\) 不用模型就和 MBPO 一样快,REDQ 在 MuJoCo 上也追平了 MBPO。同样,比较“样本数”时要说清楚数的是什么:真实步、模型步还是仿真步。

    MBPO does 20–40 updates per real step (in the paper; 10 in FIG 6), plain SAC 1. Separating the benefit of "using a model" from that of "updating more" requires a model-free control with the same update ratio: in FIG 6, SAC with \(G=10\) and no model is as fast as MBPO, and REDQ matched MBPO on MuJoCo. Likewise, when comparing "samples", say what is counted: real steps, model steps or simulator steps.

    10小练习Check yourself

    1. 一个规划器在学到的模型上找出了预测回报最高的动作序列,在真机上执行,回报却低得多。最可能的原因是什么?1. A planner finds the action sequence with the highest predicted return in a learned model, but on the real robot the return is much lower. What is the most likely reason?
    选项 B。在 \(\arg\max\) 之下,预测里偏高的误差被选中的概率远高于偏低的,所以计划的预测值系统性偏乐观(FIG 1 单个网络预测 1.11,真实摔倒 −0.6)。训练集上的误差可以很小(选项 D),问题恰恰出在训练数据之外。没收敛的 CEM 会给出较差的计划,但不会造成“预测高、真实低”这种系统性落差(选项 A);噪声只会让回报上下波动(选项 C)。Option B. Under an \(\arg\max\), errors that make the prediction too high are far more likely to be selected than errors that make it too low, so a plan's predicted value is biased upward (FIG 1's single network predicted 1.11 and the robot fell at −0.6). The training-set error can be tiny (option D); the trouble lies outside the training data. An unconverged CEM gives a worse plan but not this systematic "predicted high, real low" gap (option A); noise only makes the return fluctuate (option C).
    2. PETS 中 TS∞ 与 TS1 的区别是什么?2. What is the difference between TS∞ and TS1 in PETS?
    选项 C。名字里的 ∞ 和 1 指“多少步后重新选成员”。两者都在每一步从所选成员的预测高斯里采样(选项 D 错),粒子数都是 \(P\)(论文中 20),视野都是规划视野 \(H\)。TS∞ 让同一条轨迹始终服从同一个“可能的动力学”,更符合动力学不随时间变化的假设,还能把偶然和认知方差分开。Option C. The ∞ and 1 refer to how many steps pass before a particle's member is re-drawn. Both sample from the chosen member's predicted Gaussian at every step (option D is wrong), both use \(P\) particles (20 in the paper) over the planning horizon \(H\). TS∞ keeps each trajectory consistent with one "plausible dynamics", which matches the assumption that dynamics do not change over time and lets aleatoric and epistemic variance be separated.
    3. PILCO 能用确定性的梯度方法(如 L-BFGS)优化策略,关键原因是什么?3. What is the key reason PILCO can optimize its policy with a deterministic gradient method such as L-BFGS?
    选项 A。PILCO 不采样:从 \(\mathcal N(\mu_t,\Sigma_t)\) 到 \(\mathcal N(\mu_{t+1},\Sigma_{t+1})\) 的每一步都是闭式公式,所以 \(J\) 和它的梯度都能精确算出(FIG 4 的地图是一张光滑、无噪声的曲面)。真实系统可以有噪声(选项 B 错);用粒子的是 PETS(选项 C);GP 有长度尺度、信号方差、噪声方差等超参数,PILCO 用证据最大化拟合它们(选项 D 错)。Option A. PILCO does not sample: each step from \(\mathcal N(\mu_t,\Sigma_t)\) to \(\mathcal N(\mu_{t+1},\Sigma_{t+1})\) is a closed-form formula, so \(J\) and its gradient are computed exactly (FIG 4's map is a smooth, noise-free surface). The real system may be noisy (option B is wrong); particles are PETS's tool (option C); a GP has length-scales, a signal variance and a noise variance, which PILCO fits by evidence maximization (option D is wrong).
    4. 用饱和代价 \(\sigma_c=0.25\),目标 \(x^\star=1\)。预测位置 \(x\sim\mathcal N(0.2,\,0.1^2)\)。把 \(\sigma\) 从 0.1 增大一点,期望代价会怎样?4. With a saturating cost, \(\sigma_c=0.25\) and target \(x^\star=1\), the predicted position is \(x\sim\mathcal N(0.2,\,0.1^2)\). If \(\sigma\) grows a little from 0.1, what happens to the expected cost?
    选项 D。\(\sqrt{0.0625+0.01}\approx0.269\),而 \(|0.2-1|=0.8\) 大得多,处在“远离目标”的一侧:均值附近代价已经饱和在 1 附近,更宽的分布让一部分概率落进目标附近的低代价区,期望代价下降(FIG 5 可以验证)。选项 C 的不等式本身就是错的。只有二次代价才“方差总是有害”(选项 A)。Option D. \(\sqrt{0.0625+0.01}\approx0.269\), and \(|0.2-1|=0.8\) is much larger, on the "far from the target" side: the cost near the mean is already saturated near 1, and a wider distribution puts some probability into the low-cost region near the target, so the expected cost falls (check it in FIG 5). The inequality in option C is simply false. Only a quadratic cost makes variance "always harmful" (option A).
    5. MBPO 为什么从真实缓冲区里的状态分叉出短展开,而不是从初始状态在模型里跑完整回合?5. Why does MBPO branch short rollouts from states in the real buffer rather than running whole episodes in the model from the initial state?
    选项 B。这正是定理 4.3 里两类项的权衡:模型误差项随 \(k\) 线性增长,策略偏移项随 \(\gamma^k\) 衰减;模型够准时最优 \(k\) 是一个不大的正数。分叉的起点来自真实数据,让“长度短”不等于“只看到初始状态附近”。模型完全可以多步预测(选项 A 错,论文也试过 200 步);SAC 仍是异策略算法(选项 C 错);选项 D 没有根据。Option B. This is exactly the trade-off between the two kinds of terms in Theorem 4.3: the model-error term grows linearly in \(k\), the policy-shift terms decay like \(\gamma^k\); with an accurate enough model the best \(k\) is a small positive number. Starting the branches from real data means "short" does not mean "only near the initial state". A model can certainly predict many steps (option A is wrong; the paper tried 200); SAC stays off-policy (option C is wrong); option D has no basis.

    11延伸阅读Further reading

    • M. P. Deisenroth & C. E. Rasmussen, PILCO: A Model-Based and Data-Efficient Approach to Policy Search, ICML 2011 — PILCO 原论文:GP 模型、矩匹配、解析策略梯度,真实小车倒立摆the original PILCO: GP model, moment matching, analytic policy gradients, a real cart-pole
    • M. P. Deisenroth, D. Fox & C. E. Rasmussen, Gaussian Processes for Data-Efficient Learning in Robotics and Control, IEEE TPAMI, 2015 — PILCO 的完整推导与机器人实验the full derivation of PILCO and its robot experiments
    • M. P. Deisenroth, C. E. Rasmussen & D. Fox, Learning to Control a Low-Cost Manipulator using Data-Efficient Reinforcement Learning, RSS 2011 — 低成本机械臂堆积木block stacking with a low-cost arm
    • K. Chua, R. Calandra, R. McAllister & S. Levine, Deep Reinforcement Learning in a Handful of Trials using Probabilistic Dynamics Models, NeurIPS 2018 — PETS:概率集成、轨迹采样与 CEM-MPC,以及各种传播方式的比较PETS: probabilistic ensembles, trajectory sampling and CEM-MPC, with a comparison of propagation schemes
    • A. Nagabandi, G. Kahn, R. S. Fearing & S. Levine, Neural Network Dynamics for Model-Based Deep Reinforcement Learning with Model-Free Fine-Tuning, ICRA 2018 — 网络动力学模型 + 采样 MPC 的早期工作early work on network dynamics models with sampling MPC
    • M. Janner, J. Fu, M. Zhang & S. Levine, When to Trust Your Model: Model-Based Policy Optimization, NeurIPS 2019 — MBPO:分叉短展开与回报下界MBPO: branched short rollouts and the return bound
    • X. Chen, C. Wang, Z. Zhou & K. W. Ross, Randomized Ensembled Double Q-Learning: Learning Fast Without a Model, ICLR 2021 — REDQ:不用模型、靠高更新比和 Q 集成追平 MBPOREDQ: matching MBPO without a model through a high update ratio and a Q ensemble
    • R. S. Sutton, Integrated Architectures for Learning, Planning, and Reacting Based on Approximating Dynamic Programming, ICML 1990 — Dyna:用模型生成的数据训练价值函数,MBPO 的思想源头Dyna: training a value function on model-generated data, the idea MBPO descends from
    • T. M. Moerland, J. Broekens, A. Plaat & C. M. Jonker, Model-based Reinforcement Learning: A Survey, Foundations and Trends in Machine Learning, 2023 — model-based 强化学习综述a survey of model-based reinforcement learning