Simple Algorithm
Reinforcement Learning · Fixed Point · 28 min

When to Trust a Model: MBPO

Short branched rollouts and a bound on model-based policy improvement.

Model-generated data is cheap but biased; real data is unbiased but stale, collected by an older policy. Janner, Fu, Zhang and Levine (2019) turned this trade-off into bounds, found that the first one literally recommends never using the model, repaired it with an empirical fact about how models generalize, and arrived at a simple recipe: many short rollouts branched from real states. This lesson derives the bounds and weighs what the experiments show.

What you’ll learn

  • Prove a returns bound for two processes that differ in dynamics and policy, with state-distribution error growing linearly in time.
  • State MBPO's monotonic-improvement bound, prove it, and show that it is vacuous at realistic discount factors.
  • Explain branched rollouts and the kk-step bound, why it is minimized at k=0k=0 as stated, and how an empirical model of generalization yields an optimal k>0k>0.
  • Describe the MBPO algorithm, what its experiments do and do not show, and the competing explanation from high update-to-data model-free methods.

Two errors, one returns bound

Write J(π)J(\pi) for the true return of a policy and J^(π)\hat J(\pi) for its return in the learned model pθp_\theta (Janner et al. write η\eta and η^\hat\eta); rewards satisfy ∣r∣≤rmax⁡\abs{r}\le r_{\max}. The model was trained on data from an earlier policy πD\pi_D. Two errors separate J^(π)\hat J(\pi) from J(π)J(\pi):

  • Model error εm=max⁡tEs∼πD,t[TV(p(⋅∣s,a),pθ(⋅∣s,a))]\varepsilon_m=\max_t\E_{s\sim\pi_{D,t}}\big[\mathrm{TV}\big(p(\cdot\mid s,a),p_\theta(\cdot\mid s,a)\big)\big]: the model's generalization error, measured where the data came from, and estimable as a validation loss.
  • Policy shift επ=max⁡sTV(π(⋅∣s),πD(⋅∣s))\varepsilon_\pi=\max_s\mathrm{TV}\big(\pi(\cdot\mid s),\pi_D(\cdot\mid s)\big): how far the new policy has moved. In practice it is controlled through a KL divergence and Pinsker's inequality.
DerivationProof
  1. TV(p1t(s,a),p2t(s,a))≤TV(p1t(s),p2t(s))+δπ\mathrm{TV}\big(p_1^t(s,a),p_2^t(s,a)\big)\le\mathrm{TV}\big(p_1^t(s),p_2^t(s)\big)+\delta_\pi

    Let pit(s)p_i^t(s) and pit(s,a)p_i^t(s,a) be the state and state–action distributions at time tt. The total variation of a joint distribution is at most that of the first marginal plus the expected TV of the conditional, so the policy adds at most δπ\delta_\pi:

  2. TV(p1t+1,p2t+1)≤TV(p1t,p2t)+δπ+δm⟹TV(p1t(s,a),p2t(s,a))≤t (δm+δπ)+δπ\mathrm{TV}\big(p_1^{t+1},p_2^{t+1}\big)\le\mathrm{TV}\big(p_1^t,p_2^t\big)+\delta_\pi+\delta_m\quad\Longrightarrow\quad\mathrm{TV}\big(p_1^t(s,a),p_2^t(s,a)\big)\le t\,(\delta_m+\delta_\pi)+\delta_\pi

    The same decomposition for the next state adds the dynamics error, so the state distributions drift apart at most linearly, from TV=0\mathrm{TV}=0 at t=0t=0:

  3. ∣J1−J2∣≤∑t≥0γt rmax⁡∥p1t−p2t∥1≤2rmax⁡∑t≥0γt[t(δm+δπ)+δπ]\abs{J_1-J_2}\le\sum_{t\ge0}\gamma^t\,r_{\max}\big\lVert p_1^t-p_2^t\big\rVert_1\le2r_{\max}\sum_{t\ge0}\gamma^t\big[t(\delta_m+\delta_\pi)+\delta_\pi\big]

    Bound each step's reward difference by rmax⁡r_{\max} times the L1L_1 distance, which is twice the TV, and sum with ∑tγt=11−γ\sum_t\gamma^t=\frac1{1-\gamma} and ∑ttγt=γ(1−γ)2\sum_tt\gamma^t=\frac{\gamma}{(1-\gamma)^2}. ■\blacksquare

Monotonic improvement, and why the bound is vacuous

DerivationProof: route the comparison through the data policy
  1. J(π)−J^(π)=J(π)−J(πD)⏟L1+J(πD)−J^(π)⏟L2J(\pi)-\hat J(\pi)=\underbrace{J(\pi)-J(\pi_D)}_{L_1}+\underbrace{J(\pi_D)-\hat J(\pi)}_{L_2}

    The model is only known to be accurate where πD\pi_D goes, so add and subtract J(πD)J(\pi_D):

  2. L1≥−2rmax⁡[γεπ(1−γ)2+επ1−γ],L2≥−2rmax⁡[γ(εm+επ)(1−γ)2+επ1−γ]L_1\ge-2r_{\max}\Big[\frac{\gamma\varepsilon_\pi}{(1-\gamma)^2}+\frac{\varepsilon_\pi}{1-\gamma}\Big],\qquad L_2\ge-2r_{\max}\Big[\frac{\gamma(\varepsilon_m+\varepsilon_\pi)}{(1-\gamma)^2}+\frac{\varepsilon_\pi}{1-\gamma}\Big]

    L1L_1 compares two policies in the true dynamics (δm=0\delta_m=0, δπ=επ\delta_\pi=\varepsilon_\pi). L2L_2 compares πD\pi_D in the true dynamics with π\pi in the model (δm=εm\delta_m=\varepsilon_m, δπ=επ\delta_\pi=\varepsilon_\pi). Apply the returns bound to each:

  3. Adding the two gives exactly −C(εm,επ)-C(\varepsilon_m,\varepsilon_\pi). ■\blacksquare The policy shift appears twice because it is paid twice: once to compare π\pi with πD\pi_D, once again inside the model comparison.

Predict before you look

Take γ=0.99\gamma=0.99, rmax⁡=1r_{\max}=1 and small errors, εm=επ=0.01\varepsilon_m=\varepsilon_\pi=0.01. What is the improvement margin C(εm,επ)C(\varepsilon_m,\varepsilon_\pi) that the model return must exceed?

Show the answer

598

2×0.99×0.03/10−4+4×0.01/0.01=594+4=5982\times0.99\times0.03/10^{-4}+4\times0.01/0.01=594+4=598, while every return lies in [−100,100][-100,100]. No policy can clear that margin, so the guarantee never fires. The bound charges the model for compounding over the whole effective horizon, which is the motivation for using the model only over short stretches.

Branched rollouts: model bias against off-policy error

A branched rollout starts from a state drawn from the old policy's state distribution, in practice from the replay buffer, and runs kk steps of the new policy in the model. Dyna's planning updates are the case k=1k=1. Formally the scheme executes a non-stationary policy that follows πD\pi_D in the true dynamics until the branch point and then π\pi in the model for kk steps; call its return Jbranch(π)J^{\mathrm{branch}}(\pi).

Read the terms. Those with γk\gamma^k charge for the part of the return that still rests on stale, off-policy states; a longer branch discounts them away. The term linear in kk charges for kk steps of model error. In the first bound that term grows like εm+2επ\varepsilon_m+2\varepsilon_\pi, larger than anything the γk\gamma^k terms can save, so the bound is best at k=0k=0: taken literally, it says never use the model. The flaw is pessimism about generalization: the first bound assumes the model's error on the new policy's states can be as large as εm+2επ\varepsilon_m+2\varepsilon_\pi.

Janner et al. measured instead how the model's error grows as the policy moves away from the data (on Hopper and Walker2d) and found a slow, roughly linear growth, ε^m′(επ)≈εm+επ dεm′dεπ\hat\varepsilon_{m'}(\varepsilon_\pi)\approx\varepsilon_m+\varepsilon_\pi\,\frac{d\varepsilon_{m'}}{d\varepsilon_\pi}, with a slope that shrinks as the model is trained on more data. With a small slope, the second bound is minimized at some k>0k>0.

DerivationWhere an optimal rollout length comes from
  1. B(k)=γk επ(1−γ)2+k εm′1−γB(k)=\frac{\gamma^k\,\varepsilon_\pi}{(1-\gamma)^2}+\frac{k\,\varepsilon_{m'}}{1-\gamma}

    Collect the γk\gamma^k terms of the second bound's bracket, using γ(1−γ)2+11−γ=1(1−γ)2\frac{\gamma}{(1-\gamma)^2}+\frac{1}{1-\gamma}=\frac{1}{(1-\gamma)^2}:

  2. B(k+1)−B(k)=εm′−γk επ1−γB(k+1)-B(k)=\frac{\varepsilon_{m'}-\gamma^k\,\varepsilon_\pi}{1-\gamma}

    Lengthen the branch by one step: the stale-data term shrinks by a factor γ\gamma, and one more step of model error is added.

  3. k⋆=⌈ln⁡(επ/εm′)ln⁡(1/γ)⌉ ≈ 11−γ ln⁡επεm′k^\star=\Big\lceil\frac{\ln(\varepsilon_\pi/\varepsilon_{m'})}{\ln(1/\gamma)}\Big\rceil\ \approx\ \frac{1}{1-\gamma}\,\ln\frac{\varepsilon_\pi}{\varepsilon_{m'}}

    So BB falls while γkεπ>εm′\gamma^k\varepsilon_\pi>\varepsilon_{m'} and rises afterward. The best length is the first kk at which the saving drops below the model error, and it is positive exactly when εm′<επ\varepsilon_{m'}<\varepsilon_\pi. Branch longer when the effective horizon is long and when the model generalizes well relative to how far the policy moves. In the first bound the per-step cost is εm+2επ\varepsilon_m+2\varepsilon_\pi, which always exceeds γkεπ\gamma^k\varepsilon_\pi: there k⋆=0k^\star=0.

Predict before you look

With γ=0.9\gamma=0.9, policy shift επ=0.05\varepsilon_\pi=0.05 and model error on the new policy's states εm′=0.01\varepsilon_{m'}=0.01, which integer rollout length kk minimizes the second bound?

Show the answer

16

ln⁡(0.05/0.01)/ln⁡(1/0.9)=1.609/0.1054≈15.3\ln(0.05/0.01)/\ln(1/0.9)=1.609/0.1054\approx15.3, so k⋆=16k^\star=16: 0.915×0.05≈0.01030.9^{15}\times0.05\approx0.0103 still exceeds 0.010.01, while 0.916×0.05≈0.00930.9^{16}\times0.05\approx0.0093 does not. The penalty 2rmax⁡[⋅]2r_{\max}[\cdot] falls from 10 rmax⁡10\,r_{\max} at k=0k=0, where returns span 20 rmax⁡20\,r_{\max}, to about 5.05 rmax⁡5.05\,r_{\max}. The derivation treats εm′\varepsilon_{m'} as the error on real states of the new policy; inside a rollout the model also meets its own drifting predictions (Model Errors & Value-Equivalent Models), so long branches are worse than the linear term admits.

MBPO in practice, and what the experiments show

text
MBPO(epochs, E, M, k, G):                       # after Janner et al. (2019), Algorithm 2
    ensemble ← B = 7 probabilistic networks, each p(s′, r | s, a) = Gaussian(μ_θ, diag Σ_θ)
    for each epoch:
        fit every member to D_env by maximum likelihood
        repeat E times:
            act with π in the real environment; add the transition to D_env
            repeat M times:                         # many short branched rollouts
                s ← a state drawn uniformly from D_env
                repeat k times:
                    a ~ π(· | s);  pick an ensemble member at random
                    (s′, r) ~ that member;  add (s, a, r, s′) to D_model;  s ← s′
            repeat G times:                         # G ≈ 20–40 updates per real step
                update the policy (and SAC's critics) on minibatches from D_model
        raise k on a schedule (for example 1 → 15 over training on Hopper)

Probabilistic members capture noise in the transitions (aleatoric uncertainty); disagreement among members trained on bootstrapped data captures epistemic uncertainty, and drawing a random member at every step stops the policy from exploiting the quirks of any one of them. The policy learner is soft actor–critic.

On the MuJoCo benchmarks, at their standard length of 1,000 steps, MBPO learned substantially faster than the model-free methods of its time, an order of magnitude on some tasks, while reaching comparable final performance: its return on Ant after 300 thousand steps matched soft actor–critic's after 3 million. It also learned high-dimensional tasks such as Ant, which posed problems for the purely planning-based PETS. The ablations were as informative:

  • Short rollouts suffice. A rollout length fixed at k=1k=1 kept much of the benefit, a baseline the authors found "surprisingly difficult to beat". The model was accurate enough for 200-step rollouts, but these trained worse policies, and 500-step rollouts were too inaccurate to learn from.
  • No exploitation at short range. Returns predicted by the model tracked real returns closely on Hopper, and across tasks the model tended to underestimate real returns. Short branches give the policy little room to steer into the model's errors.
  • The model is not just more updates. Soft actor–critic run with the same high number of gradient updates per environment step, but without model data, sped up only marginally.

MBPO uses short rollouts to make training data. Value expansion uses them to make targets: MVE (Feinberg et al., 2018) imagines only to a fixed depth HH and bootstraps from a learned value beyond it, and STEVE (Buckman et al., 2018) interpolates between rollout horizons separately for each example, using a horizon only where it does not introduce large errors. M2AC (Pan, He, Tu & He, 2020) instead masks model rollouts, keeping predictions only where the model is confident. When Janner et al. added value expansion to soft actor–critic, a single model step was again a surprisingly effective baseline, as it had been for branched rollouts.

Key takeaways

  • Two processes that differ by δm\delta_m in dynamics and δπ\delta_\pi in policy drift apart linearly in total variation, so their returns differ by O(γ(δm+δπ)/(1−γ)2)\order{\gamma(\delta_m+\delta_\pi)/(1-\gamma)^2}.
  • MBPO's monotonic-improvement bound J(π)≥J^(π)−C(εm,επ)J(\pi)\ge\hat J(\pi)-C(\varepsilon_m,\varepsilon_\pi) needs the model return to beat the old policy by CC, which at γ=0.99\gamma=0.99 and 1% errors is about 600: vacuous.
  • Branched rollouts trade off-policy error (terms in γk\gamma^k) against model error (linear in kk). With pessimistic generalization the bound prefers k=0k=0; with measured error εm′\varepsilon_{m'} it prefers k⋆≈11−γln⁡(επ/εm′)k^\star\approx\frac{1}{1-\gamma}\ln(\varepsilon_\pi/\varepsilon_{m'}).
  • MBPO (short rollouts from buffer states, a probabilistic ensemble, SAC, many updates per step) matched soft actor–critic's final performance with far fewer samples; single-step rollouts were a strong baseline, and REDQ suggests stable high update ratios explain much of the gain.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Taken literally, which rollout length minimizes the penalty in the first branched-rollout bound (Theorem 4.2)?

  1. k=0k=0: the per-step model term εm+2επ\varepsilon_m+2\varepsilon_\pi outweighs what longer branches save on off-policy error
  2. k=1k=1, as in Dyna
  3. k=1/(1−γ)k=1/(1-\gamma), the effective horizon
  4. The longest rollout the model can produce
Show the answer

A. k=0k=0: the per-step model term εm+2επ\varepsilon_m+2\varepsilon_\pi outweighs what longer branches save on off-policy error

Lengthening the branch from kk to k+1k+1 saves γkεπ/(1−γ)\gamma^k\varepsilon_\pi/(1-\gamma) in the γk\gamma^k terms but adds (εm+2επ)/(1−γ)(\varepsilon_m+2\varepsilon_\pi)/(1-\gamma) in the linear one, and γkεπ<εm+2επ\gamma^k\varepsilon_\pi<\varepsilon_m+2\varepsilon_\pi always, so every extra step makes the bound worse. Replacing that pessimistic cost by the measured error εm′\varepsilon_{m'} is what makes k>0k>0 optimal.

Question 2

Why does MBPO start its model rollouts from states in the replay buffer rather than from the initial state distribution?

  1. Replay-buffer states have exact model predictions
  2. It decouples the rollout length from the task horizon: many short rollouts cover the visited state distribution while each stays short enough to limit compounding error
  3. The initial state distribution is unknown
  4. It makes the rollouts on-policy for the data-collecting policy
Show the answer

B. It decouples the rollout length from the task horizon: many short rollouts cover the visited state distribution while each stays short enough to limit compounding error

Starting from the initial states, covering a 1,000-step task needs 1,000-step rollouts, which compound errors badly; earlier methods often shortened the benchmarks instead. Branching from buffer states reaches deep into the task with kk of 1 to about 15.

Question 3

Which finding most complicates attributing MBPO's sample efficiency to its learned model?

  1. Its model returns underestimate real returns
  2. Its rollouts are only 1 to 15 steps long
  3. It uses an ensemble of 7 dynamics models
  4. A model-free agent with a very high update-to-data ratio and an ensemble of Q-functions (REDQ) performed as well as a state-of-the-art model-based method
Show the answer

D. A model-free agent with a very high update-to-data ratio and an ensemble of Q-functions (REDQ) performed as well as a state-of-the-art model-based method

MBPO performs 20–40 gradient updates per real step; REDQ showed that a model-free agent made stable at a similar update ratio can match model-based sample efficiency. The model may contribute mainly by regularizing many updates per sample.

Further reading

  • M. Janner, J. Fu, M. Zhang & S. Levine, "When to trust your model: Model-based policy optimization," NeurIPS (2019).
  • V. Feinberg, A. Wan, I. Stoica, M. I. Jordan, J. E. Gonzalez & S. Levine, "Model-based value estimation for efficient model-free reinforcement learning," arXiv:1803.00101 (2018).
  • J. Buckman, D. Hafner, G. Tucker, E. Brevdo & H. Lee, "Sample-efficient reinforcement learning with stochastic ensemble value expansion," NeurIPS (2018).
  • F. Pan, J. He, D. Tu & Q. He, "Trust the model when it is confident: Masked model-based actor-critic," NeurIPS (2020).
  • X. Chen, C. Wang, Z. Zhou & K. W. Ross, "Randomized ensembled double Q-learning: Learning fast without a model," ICLR (2021).