Search

Search papers and lessons

Model-Based Reinforcement Learning

Level 3 · 9 min read

Model-based reinforcement learning fits a model of how the world responds to actions, then uses that model to plan or to generate practice, so it needs fewer real interactions.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Learning basics
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Model-based reinforcement learning first learns a model of the environment: given a state and an action, predict the next state and the reward. The agent then improves its behaviour by using that model. It can plan ahead inside the model, or it can generate imagined experience to train a policy. Real interaction is spent on learning the model, and the model is reused many times.

The payoff is sample efficiency. A robot that can afford only a few thousand real steps can still plan with millions of imagined ones, as long as the model is accurate enough.

Where you'll see it: in robot control where real trials are slow or costly, in planning from video with PlaNet and Dreamer, and in game agents such as MuZero, which plans with a learned model.

Intuition

Practising in your head

A chess player does not learn only by playing games. Before moving a piece, she imagines the reply, then the reply to that, and rejects lines that lose. Each imagined move costs nothing, so she can look at far more lines than she could play. Her picture of the board and its rules is a model.

Model-free reinforcement learning skips the picture. It learns what to do directly from each real outcome, which is reliable but hungry for data. Model-based reinforcement learning spends real experience to learn the rules, then uses the rules to save experience.

The two uses split the field. In planning, the agent searches through imagined futures at decision time, the way model predictive control does. In Dyna-style learning, the agent replays imagined transitions to update its value estimates between real steps. Dyna, proposed by Richard Sutton in the early 1990s, is the simplest version of the second idea.

flowchart LR
  A[Agent acts in the real environment] --> B[Store real transitions]
  B --> C[Fit a model to predict next state and reward]
  C --> D[Plan or imagine rollouts inside the model]
  D --> E[Improve the policy or value estimates]
  E --> A

The loop is the whole idea. Real data improves the model, the model produces cheap data that improves the policy, and the better policy collects more useful real data.

The math

Setting

Take a Markov decision process (S,A,P,R,γ)(\mathcal{S}, \mathcal{A}, P, R, \gamma) with unknown transition probabilities P(s′∣s,a)P(s' \mid s, a) and unknown reward function R(s,a)R(s, a). The discount factor γ∈[0,1)\gamma \in [0, 1) says how much a reward kk steps away is worth, relative to a reward now: γk\gamma^k.

The agent wants a policy π\pi that maximises the expected discounted return

J(π)=E ⁣[∑t=0∞γtrt],J(\pi) = \mathbb{E}\!\left[\sum_{t=0}^{\infty} \gamma^t r_t\right],

where the expectation is over the trajectory s0,a0,r0,s1,…s_0, a_0, r_0, s_1, \dots generated by following π\pi. Model-free methods estimate JJ or its gradient from real trajectories. Model-based methods build an approximation P^\hat{P} and R^\hat{R} and work with those instead.

Learning the model

The model is an ordinary supervised learning problem. The inputs are state-action pairs (st,at)(s_t, a_t). The targets are the observed next state st+1s_{t+1} and reward rtr_t. If the model is probabilistic, we maximise the likelihood of the observed transitions:

θ^=arg⁡max⁡θ∑tlog⁡P^θ(st+1∣st,at).\hat{\theta} = \arg\max_{\theta} \sum_{t} \log \hat{P}_\theta(s_{t+1} \mid s_t, a_t).

This is maximum likelihood again. For a discrete problem the answer is just counts: P^(s′∣s,a)\hat{P}(s' \mid s, a) equals the number of times s′s' followed (s,a)(s, a), divided by the number of times (s,a)(s, a) was tried. For a continuous state, the model is a neural network that outputs a mean and variance, or a mixture density network when the next state can have several plausible values.

Planning inside the model

With a model in hand, the agent can estimate the return of a plan without touching the environment. Take a rollout of HH steps from state ss under a sequence of actions:

G^H=∑k=0H−1γkr^k+γHV(s^H),\hat{G}_H = \sum_{k=0}^{H-1} \gamma^k \hat{r}_k + \gamma^H V(\hat{s}_H),

where s^0=s\hat{s}_0 = s, each s^k+1\hat{s}_{k+1} and r^k\hat{r}_k come from the model, and VV is a value function that estimates the return after the horizon. Setting H=0H = 0 leaves only V(s)V(s), with no planning at all; larger HH trusts the model for more steps. A planner, such as a random search, the cross-entropy method, or tree search, chooses the action sequence with the highest estimated return and executes its first action.

Error compounds

Here is the central difficulty. Suppose each predicted reward is off by at most δ\delta, and the errors add up along the rollout. The accumulated error after a horizon HH is then at most

εH=δ∑k=0H−1γk=δ 1−γH1−γ.\varepsilon_H = \delta \sum_{k=0}^{H-1} \gamma^k = \delta\,\frac{1 - \gamma^H}{1 - \gamma}.

The error grows with HH and levels off at δ/(1−γ)\delta / (1 - \gamma). Long imagined rollouts can therefore be dominated by model error, even when each single step looks accurate. This is the reason most practical systems use short horizons, or re-plan often from real observations.

051015202530012345rollout horizon Haccumulated error
  • per-step error 0.5, gamma 0.9
If each model step adds error 0.5, the accumulated error grows with the horizon and levels off near 5 for gamma = 0.9. The curve is the worst case where errors add with no cancellation.

Model-free and model-based in one line

A useful way to see the tradeoff: model-free learning estimates JJ from real samples at a cost of one real step per sample. Model-based learning spends real steps on the model and then gets roughly KK imagined samples per real step, for some K≥1K \ge 1. The gain is large when KK is large and the model error stays small over the horizon used.

Worked examples

Example 1: estimating a transition from counts

An agent in state AA takes action "go" ten times and records where it lands: 6 times in BB, 3 times in CC, and once back in AA.

Example
Counting transitions
  1. Total tries of (A,go)(A, \text{go}): n=10n = 10.
  2. Estimated probabilities: P^(B∣A,go)=6/10=0.6\hat{P}(B \mid A, \text{go}) = 6/10 = 0.6, P^(C∣A,go)=3/10=0.3\hat{P}(C \mid A, \text{go}) = 3/10 = 0.3, and P^(A∣A,go)=1/10=0.1\hat{P}(A \mid A, \text{go}) = 1/10 = 0.1.
  3. Check: 0.6+0.3+0.1=1.00.6 + 0.3 + 0.1 = 1.0, so the estimate is a valid distribution.
  4. Interpretation: ten real steps give a usable model of this one state-action pair. The estimate is noisy. With only ten tries, the standard error of P^(B∣A,go)\hat{P}(B \mid A, \text{go}) is about 0.6×0.4/10≈0.155\sqrt{0.6 \times 0.4 / 10} \approx 0.155, so the model is far from certain. A planner that trusts it blindly may favour the wrong branch, which is why later sections matter.

Example 2: a planning rollout with a model error

Two steps from state s0s_0, with discount γ=0.9\gamma = 0.9 and the value of the state after the horizon taken as 00.

Example
Model return versus true return
  1. Plan: take action a1a_1, then action a2a_2. The model predicts reward r^0=1\hat{r}_0 = 1 for a1a_1, then r^1=2\hat{r}_1 = 2 for a2a_2.
  2. Model return: G^2=1+0.9×2=2.8\hat{G}_2 = 1 + 0.9 \times 2 = 2.8.
  3. The real environment gives r1=1.5r_1 = 1.5 for a2a_2, because the model overestimated that reward by 0.50.5. The true return of the same plan is G2=1+0.9×1.5=2.35G_2 = 1 + 0.9 \times 1.5 = 2.35.
  4. Error: 2.8−2.35=0.45=γ×0.52.8 - 2.35 = 0.45 = \gamma \times 0.5. The planner believes this plan is better by 0.450.45 than it really is.
  5. Interpretation: a planner that picks the plan with the highest model return will be biased toward the plan whose model is most optimistic. This is called model exploitation, and it is the main reason the method can fail when the model is trained on too little data.

Tradeoffs and failure modes

situationwhat goes wrongbetter choice
Real steps are scarce or expensiveModel-free methods need too many of themModel-based learning, with a short planning horizon
Long horizon with an imperfect modelError compounds, as the plot showsShort rollouts, frequent re-planning, or a latent model with multi-step training
Planner searches hard against the modelExploits model errors, finds plans that only the model likesEnsembles to measure disagreement, or penalise uncertain transitions
Rich observations such as imagesPredicting every pixel wastes capacity and misses what mattersLearn a compact latent state, as in PlaNet and Dreamer
Environment is cheap to simulateModel adds complexity and a new source of biasModel-free methods such as [[proximal-policy-optimization
Model is a deterministic regression on noisy dataAverages over futures and predicts a blurred stateProbabilistic model that outputs a distribution

The field's standard fix for the exploitation problem is to keep the model honest about what it does not know. An ensemble of models gives a spread of predictions, and a planner can prefer plans where the models agree.

History

Model-based ideas go back to optimal control, where engineers had a physics model of the plant long before learning was involved. Within reinforcement learning, Sutton's Dyna architecture in the early 1990s combined learning the model with updating value estimates from imagined transitions.

In the 2010s, the probabilistic Gaussian-process model in PILCO showed that a small number of real trials could learn a control policy on a physical system. Neural network models then scaled the idea to images. PlaNet (2018) planned in a learned latent space with the cross-entropy method, and Dreamer (2019) trained a policy entirely on rollouts imagined in its latent model. MuZero later showed that a model trained only to predict quantities relevant to planning, rather than the full observation, could reach strong game play.

Check yourself

QuestionWhat is the main reason a model-based agent can use fewer real steps than a model-free one?Show answer

Each real transition is used twice: once to fit the model and again indirectly through the model's imagined rollouts. The imagined rollouts cost computation rather than real interaction, so the agent gets many more learning samples per real step, provided the model is accurate enough.

QuestionAn agent sees the state-action pair (s,a)(s, a) go to s′s' in 4 of 10 tries and to s′′s'' in the other 6. What does the count-based model predict for P^(s′∣s,a)\hat{P}(s' \mid s, a)?Show answer

P^(s′∣s,a)=4/10=0.4\hat{P}(s' \mid s, a) = 4/10 = 0.4. The complementary probability P^(s′′∣s,a)=0.6\hat{P}(s'' \mid s, a) = 0.6 makes the distribution sum to 1.

QuestionWith γ=0.9\gamma = 0.9 and a per-step reward error of δ=0.5\delta = 0.5, what is the bound on the accumulated error for a horizon H=3H = 3? Why does it stop growing as HH increases?Show answer

ε3=0.5×(1+0.9+0.81)=0.5×2.71=1.355\varepsilon_3 = 0.5 \times (1 + 0.9 + 0.81) = 0.5 \times 2.71 = 1.355. It stops growing because the discount γk\gamma^k shrinks each later error term. The infinite sum converges to δ/(1−γ)=5\delta / (1 - \gamma) = 5, so later steps add less and less.

QuestionYou plan with a learned model and the chosen plan performs much worse in the real environment than the model predicted. Name the likely cause and one change that would reduce it.Show answer

The planner exploited an error in the model: it selected a plan whose predicted return was high only because the model was wrong there. A useful change is to measure the model's uncertainty, for example with an ensemble, and penalise or avoid plans that lead into regions where the models disagree. Shorter horizons and more real data collected near the chosen plan also help.

Sources