Nobody can write the equations for a camera feed, so the robot learns a small model of the world and practises inside it. A 4-step dream plus a learned value reaches the goal. A 4-step dream alone never finds it.
Pick a dreamer and compare its pink dream with its teal real path. Then we build, piece by piece, the learned model, the latent space, Dreamer, TD-MPC, and the error that grows inside every dream.
You need what a probability is, a little Python and the closed loop from Lecture 1. We build the rest from zero.
Lecture 15 of 25 in Introduction to Robot Learning, CMU 16-831 (Spring 2024), taught by Guanya Shi. Slides on the course lectures page. Every idea is re-taught here in our own words and devices. Previous: Lecture 14. Next: Lecture 16, Structured World Models.
Illustrative toy. The goal pays a reward only inside its ring. The model knows the current at the strength set by Model accuracy (1 = exact). The robot plans 33 candidate headings in the model, runs the first action of the best, and plans again. The dashed line is what the chosen plan does in the real world.
Chapter 0
Price robot practice in real steps, and see how a learned model stretches each one
A robot arm watches a cluttered table through one camera. Each frame is 64 by 64 pixels in 3 colours: 12,288 numbers, twenty times a second. Lectures 12 to 14 planned with known dynamics, the rule that maps a state and an action to the next state. Nobody can write that rule for a camera feed.
Model-based reinforcement learning (MBRL) makes one move: learn the dynamics from data, then practise inside them. The learned dynamics are a world model: it predicts what happens next, and how good it is, for an action the robot has not taken.
The reason is data. A model-free learner uses each real step for one update. A model-based learner fits its model to that step, then imagines many more from it.
Both learners need 1,000,000 transitions of practice. Slide how many imagined steps each real step seeds.
Illustrative numbers: a 20 Hz robot, and every imagined step counted as good as a real one. That last assumption is the catch this lesson is about.
Work the number. At 20 steps per second, 1,000,000 real steps take 50,000 s: 13.9 hours of robot time. If each real step seeds 40 imagined ones, the robot supplies 1,000,000 / 41 = 24,390 real steps. That is 20 minutes.
The symptom is a policy that scores well in the dream and fails on the robot. To measure it, run the same actions in the model and on the robot, then compare. The fix fills Chapters 5 to 7: short dreams, a value for the rest, and a smooth latent space.
Chapter 1
Fit a dynamics model to logged steps, then see a perfect fit that no controller can use
Start without pixels. A 4-joint arm has a state of 8 numbers: 4 angles and 4 speeds. The action is 4 torques. A small network takes those 12 numbers and predicts the 8 numbers of the next state.
Call the state x, the action u, and the next state x-prime. Call the logged data D: N triples of (x, u, x-prime). Call the network f-hat and its weights θ (theta).
L of theta equals one over N, times the sum over i, of the squared length of x-prime-i minus f-hat-theta of x-i and u-i.
For each triple, ask f-hat-theta for its guess of the next state. Subtract the guess from the real x-prime-i and square the gap. Average over all N triples. Training moves theta to make L small.
With made-up numbers and a 1-number state: the real next states are 0.52, 0.61 and 0.70. The guesses are 0.50, 0.64 and 0.70. The squared gaps are 0.0004, 0.0009 and 0. L = 0.0013 / 3 = 0.00043.
The lecture calls this regression with two differences.
| Question | Image classification | Model learning |
|---|---|---|
| Who picks the data? | Nobody. The images arrive independent of each other. | The policy that collected D. A bad policy leaves the model blind where control needs it. |
| What does a low loss buy? | A low test error is the goal. | Not the goal. Good control is. A model can fit well and still be useless to plan on. |
Blue dots show logged steps. The warm curve is the true next state. The pink curve is the model. Raise the wiggle: the model still passes through every dot.
Illustrative toy. The wiggle is zero at every data point, so the loss on the data stays at the noise level while the slope between points grows.
A controller reads the model's slope to pick an action: "how much does the next state move if I push a little more?" A jagged model answers with huge, wrong slopes, so the controller jerks.
The lecture lists five challenges of model learning. This lecture answers the fourth. Chapter 7 returns to the fifth.
The symptom is a low validation loss and a controller that shakes. To measure it, compute the model's steepest slope along the paths you plan. The fix is to keep the model smooth: limit each layer's slope (spectral normalisation), add physics priors, and collect data with the current policy.
Chapter 2
Shrink each image to a short vector, follow the tensor shapes, and count how many dreams fit in memory
Now the camera. Could the model predict the next image from this image and the action? Each step means 12,288 predicted numbers, most of them texture and noise. Fifteen steps for 1,000 candidate plans means 184 million.
Deep MBRL predicts a latent state instead: a short learned vector, s, of 32 to 256 numbers that keeps what matters for control. The table follows one step of data with a 32-number latent and a 6-number action.
| Part | In | Out | Job |
|---|---|---|---|
| Encoder | image, 64×64×3 | latent s, 32 | A convolutional network compresses the image. |
| Latent dynamics | s (32) and action a (6) | next s, 32 | Predicts the next latent with no image. |
| Reward head | s, 32 | r̂, 1 | Scores a latent, so the agent can judge a dream. |
| Decoder | s, 32 | image, 64×64×3 | Training only: forces s to keep the scene. Dropped when acting. |
The blue block is one image. The pink bar is its latent. Slide the latent size and watch how many 15-step dreams fit in 16 MB.
Counts use 4-byte floats for every stored number and a 16 MB budget for one batch of 15-step dreams. Real systems also store activations, so the true counts are lower; the ratio holds.
Work the number. A 32-number latent is 12,288 / 32 = 384 times smaller than the image. One step costs 32 × 4 = 128 bytes. In 16 MB, 16,777,216 / (128 × 15) = 8,738 dreams of 15 steps fit. Stored as floats, raw images fit 22.
The latent buys three things. It is small, so the robot can dream thousands of futures at once. It drops texture, so its dynamics are smoother, and Chapter 1 showed why smooth matters. And one latent feeds the dynamics, the reward, the value and the policy.
The lab tests the claim behind all of this: dynamics are simpler in the right coordinates.
The symptom is a robot that ignores a small object that matters, such as a thin handle. To measure it, decode the latent and check whether the object is there, or test the reward prediction near it. The fix is a training signal that needs the object: reconstruction, reward or value prediction, or a larger latent.
Chapter 3
Split the model into one part that sees the image and one that dreams without it, then train them to agree
One frame cannot show speed. A ball in a photo could move left or right. So the latent must carry memory. Dreamer uses a recurrent state-space model (RSSM), taken from PlaNet. Each latent s-t depends on the last latent, the last action and, when there is one, the new image.
| Part | Maps | Used when |
|---|---|---|
| Representation model | last latent, last action, new image o-t → latent s-t | On the real robot, where images arrive. It sees. |
| Transition model | last latent, last action → latent s-t | In a dream. It has no image, so it predicts. |
| Reward model | latent s-t → reward r̂-t | Both. It scores each latent. |
| Decoder | latent s-t → image ô-t | Training only. |
The representation model works like the Kalman filter: predict, then correct with the new measurement. The transition model is the predict half with the camera cut off.
Each Step moves the real ball and runs the model once. Switch on Imagine to cut the camera: only the transition model runs, and its guess drifts from the real ball.
Illustrative toy: the latent is the ball's position and speed. The transition model's speed is 10% too low, the kind of small error a learned model has. With the camera on, each image corrects it.
How do the parts learn? Dreamer trains them together on logged real sequences with three terms. The decoder rebuilds the image ô-t from the latent; compare it with the real image o-t. The reward model predicts r̂-t; compare it with the real reward r-t. And the transition model's guess, the prior, must sit close to the representation model's answer, the posterior. Both are bell curves over the latent.
L equals the squared length of o-hat-t minus o-t, plus r-hat-t minus r-t squared, plus beta times the K-L of q against p.
Decode the latent and score the image error. Predict the reward and score the reward error. Measure how far the dream's guess p sits from the camera's answer q, times beta. Add the three to get L, and lower it.
With made-up numbers: image error 0.20, reward error 0.04. The posterior has mean 0.60 and spread 0.10; the prior has mean 0.50 and spread 0.20. For two bell curves, KL = ln(0.20 / 0.10) + (0.10² + 0.10²) / (2 × 0.20²) − 0.5 = 0.693 + 0.25 − 0.5 = 0.443. With beta 1, L = 0.20 + 0.04 + 0.443 = 0.683.
The paper writes the first two terms as log-probabilities; squared error is the bell-curve case, up to constants. The third term is what makes dreaming work: it teaches the transition model to guess what the camera would have shown.
| Version | What changed |
|---|---|
| Dreamer (2020) | The RSSM above, plus an actor and critic trained in imagination (Chapter 5). |
| DreamerV2 (2021) | Discrete (categorical) latents and a balanced KL term. It keeps the decoder. |
| DreamerV3 (2023) | One fixed set of settings across many domains, with rescaled rewards and values. |
The symptom is a model that predicts one step well and blurs after a few dreamed steps. To measure it, run the transition model open loop for k steps and track the image error and the KL at each step. The fix is the KL term itself, weighted with care, and training on multi-step sequences.
Chapter 4
Start from latents the robot visited in the real world, roll the transition model forward, and count the free transitions
Once the transition and reward models work, the agent can practise without the robot. It takes a latent it visited on the real robot, picks actions with its policy, and rolls the transition model forward. Each rolled-out sequence is an imagined trajectory. The agent trains on these.
Take a toy with a 2-number latent and a linear transition model. Call the latent at step t s-t, the action a-t, and the two learned matrices A and B.
s-t-plus-one equals A times s-t, plus B times a-t.
Multiply the latent s-t by A. Add B times the action a-t. That is the next dreamed latent. Repeat, and never touch the camera or the motors.
With made-up numbers: s-0 = (1.0, 0.0) and a-0 = −1. A s-0 = (1.0, 0.0) and B a-0 = (0, −0.5), so s-1 = (1.0, −0.5). A reward head r̂(s) = −(first number)² scores it −1.0. One matrix product made one labelled transition, at zero robot steps.
Blue dots are latents from real robot steps. Each pink path is a dream from one of them under the policy. Set how many starts and how long each dream runs.
Illustrative toy using the A and B above and the policy a = −0.6 s-1 − 1.2 s-2 plus a little noise. The goal is the origin, where the reward is 0.
Dreamer repeats three phases until the policy is good.
The lab fits A and B from 200 real transitions by least squares, then dreams 15 steps with the fit.
The symptom is a policy that finds actions where the model is wrong and exploits them: the dream pays, the robot does not. To measure it, compare the imagined return of the policy with its real return. The fix is short dreams started from real latents, fresh data every round, and an uncertainty-aware model (ensembles).
Chapter 5
End each short dream with a learned value, and train the actor by backpropagating through the dream
A 15-step dream that only sums its rewards is short-sighted. It ignores everything after step 15. Dreamer fixes this with an actor-critic that lives in the latent space.
| Part | Maps | Job |
|---|---|---|
| Actor π(s) | latent → action | Picks the action at each dreamed latent. This is what runs on the robot. |
| Critic v(s) | latent → one number | Estimates all the discounted reward still to come from that latent. |
Call the dream length H, the dreamed reward at step t r̂-t, the discount γ (gamma), and the last dreamed latent s-H. The training target for the start latent is the dreamed rewards plus the critic's value at the end.
G-H equals the sum over t from zero to H minus one of gamma to the t times r-hat-t, plus gamma to the H times v of s-H.
Add the H dreamed rewards r-hat-t, each shrunk by gamma once per step of delay. Then ask the critic v for the value of the last latent s-H, shrink it by gamma to the H, and add it. That sum is G-H.
With the lab's made-up chain: gamma 0.9, H 5, rewards 1.0, 0.8, 0.6, 0.5, 0.4. Their discounted sum is 2.833. The critic says v(s-5) = 3.176, and 0.9 to the 5 is 0.590, so the tail adds 1.876. G-5 = 4.709, which equals the full 20-step return.
Pink bars are the discounted dreamed rewards. The yellow block is the critic's tail. The dashed line is the true return. Shorten the dream and switch the value on and off.
The 20-step reward chain and gamma 0.9 match the Code Lab below. The correct critic is the exact return of the chain, so any H recovers the truth.
Dreamer's other move is the gradient. The transition model, reward model, actor and critic are all differentiable networks. So Dreamer backpropagates G through the dream into the actor's weights. Nudge the action at step 3, and the gradient says how the latent at step 4, the reward and the value move. These are analytic value gradients. A model-free policy gradient must estimate the same direction from noisy samples.
The critic learns from the same targets: v(s-0) should match G. Dreamer averages the targets for every cut point H, a λ-return, which lowers the noise from any one cut.
The symptom is an actor that chases a tail that is not there: the dream looks great because the critic is too high. To measure it, compare v(s) with the real discounted return from the same states. The fix is to train the critic on many dreamed targets, with a slowly updated copy of the critic as the target.
Chapter 6
Sample plans in the learned model, weight them by score, take one action and plan again
Dreamer trains a policy and runs it. TD-MPC (Hansen, Wang and Su, 2022) takes the other route from Lectures 13 and 14: model predictive control, a fresh short plan at every step. It plans in a learned latent space and adds a learned value for the part the plan cannot see.
Its model has an encoder, latent dynamics, a reward head, a value Q and a policy that suggests plans. It has no decoder. It learns from three losses. It predicts the reward. It fits Q by temporal-difference (TD) learning. And it makes each predicted latent match the encoder's latent of the next real image.
Step 4 is MPPI (model predictive path integral), the sampler from Lecture 14. Call plan i's score J-i and a temperature τ (tau) that sets how greedy the weighting is.
w-i equals e to the J-i over tau, divided by the sum over j of e to the J-j over tau.
Divide each plan's score J-i by tau and take e to that power. Divide by the total over all plans. That is w-i. The new plan is the weighted average of the sampled plans. A small tau keeps only the best; a large tau averages them all.
With made-up numbers: three plans score 2.0, 1.5 and 0.5, and tau is 0.5. Then e to the 4, 3 and 1 are 54.6, 20.1 and 2.7, which add to 77.4. The weights are 0.705, 0.259 and 0.035.
The goal pays only inside its ring, and the ring is beyond a 5-step plan. Purple lines show sampled plans, brighter for more weight. Press Run, then switch the value off and run again.
Illustrative toy: 48 plans of 5 steps, 3 MPPI rounds per step, tau 0.02. The model here is exact; the value is the exact discounted value of the goal, standing in for a learned Q.
| Dreamer | TD-MPC | |
|---|---|---|
| How it acts | Runs the trained actor: one network pass. | Solves a short plan at every step. |
| Past the horizon | Critic v at the end of each dream. | Value Q at the end of each plan. |
| Optimiser | Gradients through the dream. | Sampling (MPPI), no gradient in the planner. |
| Cost when acting | Low. | High: many dreamed plans per step. |
The symptom is a planner too slow for the control rate, or one that jitters between plans. To measure it, time one planning step against the step period, and log the first action over time. The fix is to warm-start from the last plan, shifted by one step, and to seed samples from the learned policy.
Chapter 7
Feed the model its own output, watch a small error grow, and pick the horizon that keeps it small
In Lecture 1, a copied policy made one mistake, left its data, and stayed lost. A learned model does the same inside a dream. The lecture writes the planning cost with the model nested inside itself, where c-t is the cost at step t:
At step 3 the model eats its own output twice. Each pass adds a fresh error, and each pass also stretches the error it got. Call the gap between dream and reality after k steps e-k, the fresh error per step ε (epsilon), and the stretch factor L.
e-k-plus-one equals L times e-k, plus epsilon.
Take the gap e-k you carried in. The model stretches it by L. Then it adds its own fresh error epsilon. That is the gap after the next step. Start from zero and repeat.
With made-up numbers: epsilon 0.01 and H 15 steps. If L = 1, the gap grows by 0.01 a step: 0.15 at step 15. If L = 1.1, the gap at step 15 is 0.01 × (1.1 to the 15 − 1) / 0.1 = 0.318. A slope only 10% steeper more than doubles the gap.
Each bar is the gap e-k after k dreamed steps. The yellow line marks the dream length you trust. Move the stretch factor and watch the late bars turn red.
Illustrative recursion. A bar turns red when the gap passes 0.2, a made-up tolerance. Below L = 1 the model shrinks gaps, and the error levels off at epsilon / (1 − L).
The lecture names four remedies. Each gives the error fewer steps, or a gentler L.
For the full bound and the ensemble fix, see Model-Based RL and Uncertainty-Aware Model-Based RL.
The symptom is a model with tiny one-step error whose 25-step dreams are useless. To measure it, plot the open-loop gap against k on held-out real sequences. The fix is to cut the dream where the gap passes your tolerance and hand the rest to a value.
Chapter 8
Set the model's accuracy, the dream length and the value, then watch the dream and the real path part
This is the hero's full version. A robot must reach a goal ring across a warm current. It plans in a learned model, sends the first action, and plans again. The pink line is its best dream. The dashed line is what that same plan does in the real world.
Press Run. Then break it three ways: lower the accuracy, lengthen the dream, switch the value off.
Illustrative toy, the same world as the hero. "Dream end off by" is the distance between the dream's last state and the real outcome of the same plan, averaged over the run.
Try these, in order.
The robot still arrives in case 2, because it plans again after every action: feedback hides the bad far end of each dream. A policy trained on those dreams, as in Dreamer, gets no such rescue. That gap between a confident dream and a real arrival is the craft of deep MBRL.
Chapter 9
Keep the terms, the numbers, a checklist and the core loop in code
Dreamer and TD-MPC share one idea: learn a small latent world model from images, then act inside it. They differ only in how they act. Dreamer trains an actor by dreaming. TD-MPC plans in the dream at every step.
| Term | One line | Number to remember |
|---|---|---|
| World model | A learned model of what happens next and how good it is. | 1,000,000 steps: 13.9 h real, 20 min at 40 dreams per step |
| Model loss | L(θ) = average squared gap of next-state guesses. | 0.00043 for the three-step toy |
| Latent state | A short learned vector in place of the image. | 12,288 to 32: 384 times smaller |
| RSSM | Representation model sees; transition model dreams. | KL 0.443, L 0.683 in the worked step |
| Imagination | s-t+1 = A s-t + B a-t, rolled from real latents. | (1.0, 0.0) to (1.0, −0.5), reward −1.0 |
| Bootstrapped return | Dreamed rewards plus gamma to the H times v(s-H). | 2.833 + 1.876 = 4.709 |
| MPPI weights | w-i = exp(J-i / tau), normalised. | 0.705, 0.259, 0.035 |
| Compounding | e-k+1 = L e-k + epsilon. | 0.15 at L 1, 0.318 at L 1.1, 15 steps |
pythondef dream_target(s0, actor, dyn, rew, critic, H=15, gamma=0.99): s, G = s0, 0.0 # s0: a latent from a real robot step for t in range(H): a = actor(s) # the policy acts in the dream s = dyn(s, a) # transition model: no image, no robot G += gamma ** t * rew(s) # dreamed reward return G + gamma ** H * critic(s) # the value covers what the dream cannot see # Dreamer: maximise dream_target by backprop into the actor. # TD-MPC: score sampled action sequences the same way, take the MPPI average, act once.
This lesson follows Lecture 15, "Deep Model-Based RL", of CMU 16-831 Introduction to Robot Learning, Spring 2024, taught by Guanya Shi. The slides are on the lectures page. The methods are from Hafner et al., Dream to Control (2020), with DreamerV2 and DreamerV3, and Hansen, Wang and Su, Temporal Difference Learning for Model Predictive Control (2022). The lecture closes by asking whether large action-conditioned world models are a path forward for robots.
Now press Present or Teach and explain from memory: the latent space, the two halves of the RSSM, the value at a dream's end, how TD-MPC acts, and why dreams stay short. Then go back to the three dreamers, drop the accuracy to 0.3, and say why the short dream with a value still arrives.