A planner that trusts one learned model walks off the cliff in 26 of 40 walks. Score each plan under five models instead, and it falls in none: where the models disagree, the average return drops.
Learn why planners exploit learned models, how to measure what a model does not know, and how to use a model only briefly.
Press Run with one model, then with the ensemble. Then drag how far the data reaches. Then we build, piece by piece, the data loop, expected reward, ensembles, gradients through a model, short rollouts and latent models.
You need model-based RL (Model-Based RL: dynamics models, CEM, ensembles, Dyna) and policy gradients (Policy Gradients). We build the rest from zero.
A supplement to UC Berkeley CS 185/285, Deep Reinforcement Learning, Sergey Levine (Spring 2026). It follows Lecture 15, model-based RL (PDF) and Lecture 16, model-based RL algorithms (PDF), plus the course's Section 8 notes. The words, toys and numbers here are our own.
Illustrative toy. The walker starts at x = 1 and takes 8 strides; stride a (0 to 1.5) moves it a × g(x). Past x = 5 the ground turns icy: g grows from 1 to 1.9 at x = 8. Real strides add noise 0.12. Reward is x/9 per step, and −3 per step after a fall past x = 9. Models fit g on 60 transitions up to the data edge; the planner is CEM with 5 rounds of 200 plans and 8 noise draws per model.
Chapter 0
Watch a plan leave the data, then limit how far each round of the data loop moves the policy
A robot collects data with a careful base policy πβ, the policy that gathers the data. Its transitions (s, a, s′) cover the low ground. You fit a model f(s, a) ≈ s′ and ask a planner for the best actions under f. The model says the ground keeps rising to the right, so the plan heads for the edge.
The cause is a mismatch of states. The planned policy πf visits states that πβ never visited: pπf(st) ≠ pπβ(st). There the model has no data, and the planner prefers the places where the errors look good. Model-Based RL, Chapter 8 shows that search in detail.
The loop corrects the model, but only after the walker has been to the bad place. So change step 3: improve the policy only a bit. Call the old policy πβ, the new one πf, and the budget per round ε (epsilon). Measure the change with the KL divergence, a measure of how different two distributions are.
The KL divergence of pi-f from pi-beta is at most epsilon.
Search only among new policies whose KL from the old one stays under epsilon. For two bell curves with the same width sigma, the KL is (μ′ − μ)²/(2σ²). So the mean may move at most σ√(2ε) per round.
With the device's numbers: sigma 0.4 and epsilon 1. The aim may move at most 0.4 × √2 = 0.57 per round. At epsilon 4 it may move 1.13; at epsilon 0.25, only 0.28.
Each row is one round. Yellow is where data exists; the warm tick is the policy's aim; dots are 20 real stops, red ones fell. Pick a budget and run 8 rounds.
Illustrative toy: the policy aims at x ~ N(μ, 0.4²), from μ = 3. A real stop overshoots by 0.1(x − 5)² past x = 5. The model knows the overshoot up to the farthest aim in its data and assumes it stays flat beyond. Each round uses 20 aims at fixed quantiles of the bell.
With no limit, round 1 jumps to aim 8.21 and 13 of 20 walks fall. Then the data covers the ice, and round 2 settles at 7.28. With ε = 4, two walks fall on the way. With ε = 0.25, eight rounds reach only 5.26. Small steps are safe and slow.
| Remedy | What it limits | Where |
|---|---|---|
| Trust region | how far the policy moves in one round | this chapter |
| Uncertainty-aware model | where the plan goes: only where the model is confident | Chapters 1 to 3 |
The failure. The budget is larger than the model's reach. Symptom: the model's predicted return jumps far above the real return in one round. Measure both every round. Fix: shrink ε, or use a model that knows where it lacks data.
Chapter 1
Average the reward over everything the model thinks can happen, and see the cliff push the plan back
A stride aimed at 8.5 usually stops near 8.5. Sometimes it slides to 9.4 and falls. A model fitted with squared error predicts one next state, the average: 8.95, which is safe. The average reward is not safe.
So a model should give a distribution over next states, not one state. Call the next state s′, the data D, and the model's weights θ (theta). The honest prediction averages over every model that the data allows:
p of s-prime given s, a and D equals the integral over theta of p of s-prime given s, a and theta, times p of theta given D.
Ask every possible model for its next-state distribution. Weight each answer by p of theta given D. Add the weighted answers.
With made-up numbers: two models explain the data equally well, so each has weight 0.5. Model A stops the walker at 8.5, reward 8.5/9 = 0.94. Model B stops it at 9.4, a fall, reward −3. The expected reward is 0.5 × 0.94 + 0.5 × (−3) = −1.03. The average stop, 8.95, would score 0.99.
The purple bell is where the walker may stop. The warm curve is the expected reward of each aim; the dashed curve scores only the predicted stop. Widen the spread and watch the best aim move back.
Illustrative. A stop at x scores x/9 below the edge at 9 and −3 past it. The stop is normal with mean at the aim and width σ. Exact formula, no sampling.
At aim 8.5 and spread 0.5, a fall needs a stop half a metre past the aim: chance 0.159. The expected reward is (8.5 × 0.841 − 0.5 × 0.242)/9 − 3 × 0.159 = 0.781 − 0.476 = 0.305. The predicted stop alone says 0.944. The best aim moves back from 8.29 at spread 0.25 to 7.70 at 0.5.
The course's point: under a high-variance prediction the expected reward drops far, even when the mean stays the same. The course also lists the limits:
| Score | What it does | Use |
|---|---|---|
| Expected value | averages the reward over the predictive distribution | the course calls it often a good start |
| Pessimistic value | scores a bad case, such as the mean minus a multiple of the spread | when one failure costs too much |
| Optimistic value | scores a good case | to explore where the model is unsure |
The failure. The model predicts one state per step. Symptom: plans hug the edge. Measure the plan's expected reward against the reward of its predicted states; a gap means the tail matters. Fix: a probabilistic model, scored by expectation.
Chapter 2
Separate noise in the world from doubt about the model, then get the doubt from a posterior or an ensemble
Give the network a Gaussian output: a mean and a variance for s′, trained by likelihood. The variance learns the noise in the world, the aleatoric uncertainty. Off its data, it can still be confidently wrong. Epistemic uncertainty is doubt about the model itself: the model is sure of its data, and we are not sure of the model.
Usual training keeps one point estimate of θ. The doubt lives in the whole posterior p(θ | D). A Bayesian neural network holds it with independent weights:
p of theta given D is about the product over i of a normal for theta-i, with mean mu-i and variance sigma-i squared.
Give every weight theta-i its own bell, with mean mu-i and width sigma-i. Train the means and widths with variational inference. To predict, draw weights from the bells and run the net.
With made-up numbers: a net with 200,000 weights needs 400,000 numbers, a mean and a width for each. Draws of a weight with mu 0.5 and sigma 0.1 fall between 0.3 and 0.7 about 95% of the time.
A bootstrap ensemble holds it as N nets, each one draw from the posterior. Call net i θi.
p of theta given D is about one over N times the sum over i of a delta at theta-i.
Train N nets from different random starts. Ask each for its prediction, and average the N answers.
From the device below, with data up to x = 5: at x = 8 the five nets predict stride factors 0.59, 1.02, 0.58, 1.67 and 0.96. Their mean is 0.96 and their spread 0.40. The truth is 1.90: the spread warns of the miss.
Yellow dots are data. Purple lines are the members' stride factors; dashed teal is the truth; the warm band is one model's own noise estimate. Change the members, and switch the random starts and the resampling.
Illustrative toy, trained on this page. Each member fits g(x) with 12 ramp features by least squares, pulled toward its own random starting weights, as SGD keeps traces of a random init. The noise band is ±2 times the residual noise of a single fit.
In theory each member trains on its own resample Di, drawn with replacement. The course notes that random starts and SGD make the nets differ enough. In the device, switch off random starts and the members collapse into one line.
| Method | Holds the posterior as | Cost | Weakness |
|---|---|---|---|
| Bayesian neural net | a bell per weight, independent | 2× the numbers | weights are not independent |
| Bootstrap ensemble | N separate nets | N times the training | N under 10 is a crude sample |
The failure. Too few members. In the hero's toy at data edge 5, our five nets fall in 0 of 40 walks; another set of five random starts falls in 11. Measure: rerun the planner with a second seed set. Fix: more members, or a pessimistic score.
Chapter 3
Roll the same actions through each member, average the returns, and see why averaging states first hides the fall
One model scores a plan by adding its rewards along one imagined path. An ensemble is a distribution over deterministic models. So roll the same plan through each member separately, and average the returns. Call the plan's actions a1 to aH, member i fi, and its imagined state at step t st,i.
J of the plan equals one over N times the sum over i of the sum over t of r of s-t-i and a-t, where s-t-plus-one-i is f-i of s-t-i and a-t.
Take one action sequence. Run it through member 1 and add its rewards r. Repeat for every member f-i. The score J is the average of the N returns.
From the device at stride 1.30: four members keep the walker on the ridge, with returns from 5.36 to 5.69. One lets it fall and returns −6.30. The average is J = 3.16.
The plan is 6 equal strides, then 2 rests. Each purple line is one member's path; the red line is the cliff. The dashed line averages the members' states step by step. Drag the stride.
Illustrative: the five members of Chapter 2 (data up to x = 5), with no noise. Reward x/9 per step, −3 per step after a fall. The real walk uses the true stride factor, also without noise.
The course's sampling version is the same idea with noise:
The hero's planner does this with 8 noise draws per member. PETS scores plans this way with particles; see CMU 16-831, Lecture 14.
The failure. The planner finds a plan that fools all N members at once. Symptom: the real return falls far below J. Measure: score the chosen plan under a held-out member that the planner never saw. Fix: more members, or rank plans by the worst member.
Chapter 4
Train a policy inside the model two ways, and see why the policy gradient can be the steadier one
Instead of planning at every step, train a policy πθ on model rollouts, which cost no robot time. The model is differentiable, so you can backpropagate the return through it: the pathwise gradient. Or you can treat the model as a simulator and use the policy gradient. Call the estimated return after step t of rollout i Q̂i,t.
The gradient of J of theta is about one over N times the sum over i and t of the gradient of log pi-theta of a-i-t given s-i-t, times Q-hat-i-t.
Sample N rollouts in the model. For each action, push its log-probability up in proportion to Q-hat, the reward that followed. Average over all steps and rollouts.
With made-up numbers: two one-step rollouts. Their log-probability gradients are 0.5 and −0.2; their returns Q-hat are 4 and 2. The estimate is (0.5 × 4 + (−0.2) × 2)/2 = 0.8.
An early action's effect must pass through every later step, each with its Jacobian: the matrix of how each output moves with each input. Call the model f and the policy π.
d s-H by d a-t equals the product over k from t plus one to H minus one of: d f by d s, plus d f by d a times d pi by d s, at step k; times d f by d a at step t.
An early action changes the next state. Every later step multiplies that change by its closed-loop Jacobian. A long product of factors above 1 explodes; below 1, it vanishes.
With made-up numbers: a 1-D model whose closed-loop factor is 1.2 at every step, ∂f/∂a = 1, H = 20 and t = 0. The product is 1.219 = 31.9. With a factor of 0.8 it is 0.819 = 0.014.
Warm: the spread of the pathwise estimate across repeated batches. Blue: the spread of the policy-gradient estimate on the same rollouts. Drag the step factor λ.
Illustrative toy: st+1 = λst + at, with actions 0.2 + 0.3 × noise and reward cos(sH). Each estimate averages 32 rollouts; the spread is over 60 batches. Log scale.
Below λ = 1 the pathwise estimate has the smaller spread: it uses the model's slope. Above 1 it explodes, though the reward stays between −1 and 1: the curse of chaos (Parmas et al., 2018, PIPP). The policy gradient multiplies no Jacobians, so with enough samples it can be steadier.
The failure. Symptom: gradient norms spike as the horizon grows, then overflow. Measure: log the gradient norm against the rollout length. Fix: shorter rollouts, or the policy gradient.
Chapter 5
Bound how model error grows with rollout length, then branch short rollouts off real data
Train a policy on long model rollouts, and each model step is a little wrong. The imagined states drift further from real ones with every step. The course states the cost: error grows as O(εH²), the same compounding as behaviour cloning. Here is the argument.
Add the reward gaps of steps 1 to H. Call the model's expected return Jmodel and the real one Jreal.
The gap between J-model and J-real is at most r-max times epsilon times the sum over t of t minus one, which is r-max epsilon H times H minus one, over two.
Step t of the rollout can be wrong with chance epsilon times (t − 1). Each wrong step costs at most r-max. Add the costs of all H steps: about epsilon H squared over two.
With made-up numbers: epsilon 0.01, r-max 1. A 100-step rollout: 0.01 × 100 × 99/2 = 49.5, half of the largest possible return of 100. A 5-step rollout: 0.01 × 5 × 4/2 = 0.10, out of 5.
Teal lines are real trajectories over 40 steps. Warm lines are model rollouts, drawn to drift as the bound grows. Pick a scheme and a rollout length.
Diagram with the bound's numbers: ε = 0.01 per step, rmax = 1, H = 40. The drift drawn is not simulated.
| Rollouts | Error | Time steps seen | Start states |
|---|---|---|---|
| Long, from the start | εH(H − 1)/2: huge | all | right |
| Short, from the start | εk(k − 1)/2: small | the first k only | right |
| Short, from real states | εk(k − 1)/2: small | all | from older policies: wrong distribution |
The third row is the recipe that MBA (Gu et al., 2016), MVE (Feinberg et al., 2018) and MBPO (Janner et al., 2019) share:
Keep model transitions in their own buffer, and empty it each time the model changes: they came from an old model. Model-Based RL, Chapter 7 builds Dyna, the tabular parent. MBPO has its own lesson.
The failure. The wrong state distribution. Branches start from states that older policies visited. Symptom: Q fits old states well and the current policy's states poorly. Measure: the age of the start states. Fix: start from recent data, and evict old data.
Chapter 6
Invent a small state for camera images, choose how to infer it, and train dynamics, images and rewards in one objective
From camera images, the state is not given. A latent state-space model invents one, st. It learns three parts: an observation model p(ot | st), a dynamics model p(st+1 | st, at) and a reward model p(rt | st, at). The course starts with the fully observed case: one image is enough to know the state.
With states given, training maximises Σ log pφ(st+1 | st, at). With latent states you never see st. So an encoder qψ guesses it from images. The objective becomes an ELBO: an average under q, plus the entropy of q (Variational Inference for RL). The encoder can read more or less:
| Encoder | Reads | Plus | Minus |
|---|---|---|---|
| Full smoothing q(st, st+1 | o1:H, a1:H) | the whole sequence | most accurate | most complicated |
| Single step q(st | ot) | one image | simplest | least accurate |
| Deterministic st = gψ(ot) | one image, no randomness | the average and the entropy drop out | keeps no doubt about s |
Blue circles are latent states s; squares are images o and actions a; small circles are rewards. Lit arrows show what the encoder reads to guess s2.
The graph of Lecture 16, redrawn: three steps, fully observed setting.
With the deterministic encoder, the objective is three log-likelihoods. Call the encoder gψ, the model weights φ, and sequence i's image at step t ot,i.
Maximise over phi and psi the sum over i and t of: log p-phi of the next encoded image given the encoded image and the action, plus log p-phi of the image given its code, plus log p-phi of the reward given the code.
Encode every image with g-psi. Score how well the dynamics predict the next code, how well the decoder rebuilds the image, and how well the reward head predicts r. Raise all three together.
With made-up numbers for one step, constants dropped: dynamics −0.8, image −310 (a 64 by 64 colour image has 12,288 pixel terms), reward −0.05. The sum is −310.85, and the image term decides almost all of it.
The course's latent MPC loop:
SLAC and Dreamer instead train an actor-critic on latent rollouts; see CMU 16-831, Lecture 15 and Latent World Models. The course notes that many practical methods use a stochastic encoder or a smoothing posterior, especially with partial observability.
The failure. The image term swamps the rest. Symptom: sharp reconstructions, but poor reward and dynamics predictions. Measure each term's size separately. Fix: weight the terms, and keep the reward term, so the code keeps what matters for reward.
Chapter 7
Keep the terms, the numbers, the checks and the code in one place
A learned model is right only near its data, and an optimiser goes where it is wrong. Limit how far each round moves the policy. Score plans by expected reward under many models, so disagreement costs return. Train policies on short rollouts that start from real states, and watch the Jacobian products when you backpropagate through the model.
| Term or result | Value on this page | Where |
|---|---|---|
| KL trust region | aim moves ≤ σ√(2ε): 0.57 at σ 0.4, ε 1; no limit falls 13 of 20 | Ch 0 |
| Expected reward | aim 8.5, spread 0.5: 0.305 against 0.944 for the predicted stop | Ch 1 |
| Aleatoric uncertainty | noise in the world; a variance output learns it | Ch 2 |
| Epistemic uncertainty | doubt about the model; ensemble spread 0.40 at x = 8, past the data | Ch 2 |
| Ensemble planner | average the N returns; hero: 0 of 40 falls against 26 for one model | Ch 3 |
| Pathwise gradient | product of Jacobians: 1.219 = 31.9 | Ch 4 |
| Rollout bound | rmaxεH(H − 1)/2: 49.5 at H 100, 0.10 at H 5 (ε 0.01) | Ch 5 |
| Latent objective | dynamics + image + reward log-likelihoods | Ch 6 |
pythonimport numpy as np def plan_score(models, s0, A, reward): # models: N functions f_i(s, a) -> s'; A: one plan of H actions returns = [] for f in models: # one path per member; paths never mix s, R = s0, 0.0 for a in A: s = f(s, a) R += reward(s) returns.append(R) return np.mean(returns) # average the returns, not the states def branched_rollouts(models, policy, reward, starts, k=5, rng=np.random): out = [] # the model buffer: empty it when models change for s in starts: # real states from recent data f = models[rng.randint(len(models))] # one member per rollout for _ in range(k): # short: the bound grows like k * (k - 1) / 2 a = policy(s) s2 = f(s, a) out.append((s, a, reward(s2), s2)) s = s2 return out # feed to an off-policy learner with real data
Now press Present or Teach and explain from memory: why the expected reward avoids the cliff, and why short branched rollouts beat long ones. Then go back to the ridge, set the data edge to 4, and predict which planner falls before you press Run.
This supplement follows UC Berkeley CS 185/285, Deep Reinforcement Learning, Sergey Levine (Spring 2026): the course page, Lecture 15 on model-based RL, Lecture 16 on model-based RL algorithms, and the Section 8 notes by Vivek Myers. Papers named: Blundell et al. (2015), Parmas et al. (2018, PIPP), Gu et al. (2016), Feinberg et al. (2018), Janner et al. (2019). The words, toys and numbers here are our own.