Simple Algorithm
Reinforcement Learning · Fixed Point · 26 min

Planning with Learned Values: TD-MPC

Short-horizon planning with a learned value at the end.

Plan five steps ahead and let a learned value function judge everything after: that is the bargain of TD-MPC and its relatives. This lesson proves what a terminal value buys a short-horizon planner, shows that the classical bound is attained by planners that replan at every step (and why a widely quoted tighter bound needs a planner that commits to its plans), adds model error and asks what the bound says about choosing the horizon, and then dissects TD-MPC and TD-MPC2: what they learn, how they plan, and what their ablations show.

What you’ll learn

  • Prove that HH-step lookahead with an exact model and a terminal value within ε\varepsilon of V⋆V^\star loses at most 2γHε/(1−γ)2\gamma^H\varepsilon/(1-\gamma), and construct an example in which a replanning controller attains the bound.
  • Extend the bound to a learned model, and explain why under uniform error bounds it never favors an intermediate horizon.
  • Describe POLO and LOOP, including LOOP's actor-divergence problem.
  • Specify TD-MPC and TD-MPC2 (latent model, losses, MPPI planner with a policy prior) and interpret their ablations.

What a terminal value buys

A terminal value closes the gap that MPC leaves at its horizon. An HH-step lookahead policy solves, at each state, max⁡E[∑k<Hγkr(Sk,Ak)+γHV^(SH)]\max\E\big[\sum_{k<H}\gamma^kr(S_k,A_k)+\gamma^H\hat V(S_H)\big] over HH-step plans (closed-loop plans in a stochastic MDP), executes the first action and replans. With V^=0\hat V=0 it is the myopic MPC of Optimal Control & Model Predictive Control; with V^=V⋆\hat V=V^\star even H=1H=1 is optimal, because acting greedily with respect to V⋆V^\star is optimal.

DerivationProof
  1. V⋆(s)−Vπ(s)=Eπ[∑t≥0γt(V⋆(St)−Q⋆(St,π(St))) ∣ S0=s]V^\star(s)-V^\pi(s)=\E_\pi\Big[\sum_{t\ge0}\gamma^t\Big(V^\star(S_t)-Q^\star\big(S_t,\pi(S_t)\big)\Big)\ \Big|\ S_0=s\Big]

    (a) The performance-difference lemma, with the optimal policy as the reference, writes the loss as the discounted one-step shortfalls along π\pi's own trajectory:

  2. V⋆(s)−Q⋆(s,π(s))≤[Q~(s,a⋆)+e]−[Q~(s,π(s))−e]≤2eV^\star(s)-Q^\star(s,\pi(s))\le\big[\tilde Q(s,a^\star)+e\big]-\big[\tilde Q(s,\pi(s))-e\big]\le2e

    Each shortfall is at most 2e2e. With a⋆a^\star optimal in ss, and Q~(s,π(s))≥Q~(s,a⋆)\tilde Q(s,\pi(s))\ge\tilde Q(s,a^\star) because π\pi is greedy:

  3. Summing γt⋅2e\gamma^t\cdot2e gives (a). (b) The lookahead policy is greedy with respect to Q~(s,a)=r(s,a)+γ E[(T⋆)H−1V^(S′)]\tilde Q(s,a)=r(s,a)+\gamma\,\E\big[(\mathcal T^\star)^{H-1}\hat V(S')\big]. Since T⋆\mathcal T^\star is a γ\gamma-contraction with fixed point V⋆V^\star, ∥(T⋆)H−1V^−V⋆∥∞≤γH−1ε\norm{(\mathcal T^\star)^{H-1}\hat V-V^\star}_\infty\le\gamma^{H-1}\varepsilon, hence ∥Q~−Q⋆∥∞≤γHε\norm{\tilde Q-Q^\star}_\infty\le\gamma^H\varepsilon; apply (a) with e=γHεe=\gamma^H\varepsilon. ■\blacksquare

The terminal value's error is discounted by γH\gamma^H, but the factor 1/(1−γ)1/(1-\gamma) remains: a replanning controller makes a fresh decision at every step, and each decision can be off by 2γHε2\gamma^H\varepsilon.

The bound is tight: procrastination

POLO (Lowrey et al., 2019, Lemma 2) and LOOP (Sikchi, Zhou & Held, 2021, Theorem 1) state bounds with 1/(1−γH)1/(1-\gamma^H) in place of 1/(1−γ)1/(1-\gamma), which would make lookahead far more forgiving. Both proofs pay the value error once per HH steps, which is right for a controller that carries out each optimized HH-step plan before planning again. LOOP says so: its appendix proves the theorem for such a controller and sets the receding-horizon case aside for simplicity. POLO states its lemma for the MPC policy that executes only the first action and replans, but its proof (Appendix C) compares the HH-step trajectory that this policy generates with the optimized plan, as if the policy followed the plan. For plan-committing controllers both bounds hold, and the example respects them: the committing controller loses only (1−γ)V(1-\gamma)V. For receding-horizon MPC, which is what POLO, LOOP and TD-MPC actually run, the example shows that the factor 1/(1−γ)1/(1-\gamma) cannot be improved, so POLO's lemma holds only with the committing qualification; its message that lookahead discounts value errors by γH\gamma^H survives. Replanning protects against model error and noise, but against value error alone it can do worse than committing.

Predict before you look

In the procrastination example with γ=0.9\gamma=0.9, H=3H=3 and ε=1\varepsilon=1, what does the replanning controller lose as VV approaches the tie value 2γHε/(1−γ)2\gamma^H\varepsilon/(1-\gamma)?

Show the answer

14.58

2⋅0.93/0.1=2⋅0.729⋅10=14.582\cdot0.9^3/0.1=2\cdot0.729\cdot10=14.58: the full bound. A controller that commits to its plan loses (1−γ)V≈1.46(1-\gamma)V\approx1.46, within POLO's 2γHε/(1−γH)≈5.382\gamma^H\varepsilon/(1-\gamma^H)\approx5.38.

Adding a learned model: the horizon trade-off

DerivationProof
  1. ∥T^⋆W−T⋆W∥∞≤max⁡s,a∣r^−r+γ(P^−P)⊤W∣≤εr+γ εPRmax⁡1−γ=δ\norm{\hat{\mathcal T}^\star W-\mathcal T^\star W}_\infty\le\max_{s,a}\Big|\hat r-r+\gamma(\hat P-P)^\top W\Big|\le\varepsilon_r+\gamma\,\varepsilon_P\frac{R_{\max}}{1-\gamma}=\delta

    Let Wk=(T⋆)kV^W_k=(\mathcal T^\star)^k\hat V in the environment and W^k=(T^⋆)kV^\hat W_k=(\hat{\mathcal T}^\star)^k\hat V in the model. Both operators map [0,Rmax⁡/(1−γ)][0,R_{\max}/(1-\gamma)] into itself, so every WkW_k has span at most Rmax⁡/(1−γ)R_{\max}/(1-\gamma), and centering gives ∣(P^−P)⊤W∣≤εPspan⁡(W)\abs{(\hat P-P)^\top W}\le\varepsilon_P\operatorname{span}(W), as in Model Errors & Value-Equivalent Models. Applied to the same function, the two operators therefore differ by at most δ\delta:

  2. ∥W^k−Wk∥∞≤∥T^⋆W^k−1−T^⋆Wk−1∥∞+∥T^⋆Wk−1−T⋆Wk−1∥∞≤γ∥W^k−1−Wk−1∥∞+δ≤δ 1−γk1−γ\norm{\hat W_k-W_k}_\infty\le\norm{\hat{\mathcal T}^\star\hat W_{k-1}-\hat{\mathcal T}^\star W_{k-1}}_\infty+\norm{\hat{\mathcal T}^\star W_{k-1}-\mathcal T^\star W_{k-1}}_\infty\le\gamma\norm{\hat W_{k-1}-W_{k-1}}_\infty+\delta\le\delta\,\frac{1-\gamma^k}{1-\gamma}

    Split W^k−Wk\hat W_k-W_k into a contraction term and a one-step model error, and unroll from W^0=W0=V^\hat W_0=W_0=\hat V:

  3. The lookahead Q-values add one more model step: ∣Q~−QH∣≤δ+γ δ1−γH−11−γ=δ1−γH1−γ\abs{\tilde Q-Q_H}\le\delta+\gamma\,\delta\frac{1-\gamma^{H-1}}{1-\gamma}=\delta\frac{1-\gamma^H}{1-\gamma}, where QHQ_H is the exact-model lookahead, which is within γHε\gamma^H\varepsilon of Q⋆Q^\star as before. The triangle inequality and part (a) of the previous theorem finish the proof. ■\blacksquare

Read the bound as a function of the horizon: eH=δ1−γ+γH(ε−δ1−γ)e_H=\frac{\delta}{1-\gamma}+\gamma^H\big(\varepsilon-\frac{\delta}{1-\gamma}\big) is affine in γH\gamma^H. If the value function is worse than the model's whole-horizon error, ε>δ/(1−γ)\varepsilon>\delta/(1-\gamma), the bound falls with HH and favors planning as far as possible; otherwise it rises and favors H=1H=1. This bound never favors an intermediate horizon.

The intermediate horizons used in practice, such as TD-MPC's H=5H=5, must therefore be justified by what uniform bounds ignore: model errors that grow faster along a rollout as it leaves the data (the compounding of Model Errors & Value-Equivalent Models), value errors that are small on the data but large elsewhere, and a planner that improves on an imperfect policy network. Charging model error through the drift of the rollout's state distribution, as LOOP's bound does, does not by itself change the verdict: when the terminal value's range is the full Rmax⁡/(1−γ)R_{\max}/(1-\gamma), the model term of LOOP's bound works out to 2γRmax⁡εm/(1−γ)22\gamma R_{\max}\varepsilon_m/(1-\gamma)^2 for every HH, so that bound too only falls as HH grows.

Predict before you look

TD-MPC plans H=5H=5 steps ahead with γ=0.99\gamma=0.99. By what factor does that lookahead shrink the value-error term, that is, what is γ5\gamma^5?

Show the answer

0.951

0.995≈0.9510.99^5\approx0.951: five steps of lookahead remove only about 5% of a value error, since the effective horizon is 100 steps. The worst-case bound alone cannot explain why planning helps TD-MPC; the experiments below point elsewhere.

POLO, LOOP and TD-MPC

POLO (Lowrey, Rajeswaran, Kakade, Todorov & Mordatch, 2019; "plan online, learn offline") assumed the dynamics are known, as in a simulator, and learned only a terminal value: MPPI planned with it at every step, the value was fitted offline to targets computed at the states visited, and an ensemble of value functions with randomized priors supplied optimism for exploration. It solved tasks such as a simulated humanoid getting up from the floor.

LOOP (Sikchi, Zhou & Held, 2021) combined a learned ensemble of dynamics models with a terminal value from an off-policy actor–critic. It identified actor divergence: the data are collected by the planner, while the critic's targets are computed with the actor, two different policies, which can destabilize value learning. Its fix, Actor Regularized Control, keeps the planner's distribution over action sequences close to one derived from the actor through a KL constraint, which leads to an MPPI-like closed-form update.

TD-MPC (Hansen, Wang & Su, 2022) learns everything, in a latent space and without a decoder. Its task-oriented latent dynamics model has an encoder z=hθ(s)\mathbf z=h_\theta(\mathbf s), latent dynamics z′=dθ(z,a)\mathbf z'=d_\theta(\mathbf z,\mathbf a), a reward head RθR_\theta, a value QθQ_\theta and a policy πθ\pi_\theta, all trained on short replayed segments by unrolling the latent dynamics from the first encoded state.

L(θ)=∑i=tt+Hλi−t[c1∥Rθ(zi,ai)−ri∥2+c2∥Qθ(zi,ai)−(ri+γ Qθ−(zi+1,πθ(zi+1)))∥2+c3∥dθ(zi,ai)−hθ−(si+1)∥2]\mathcal L(\theta)=\sum_{i=t}^{t+H}\lambda^{i-t}\Big[c_1\norm{R_\theta(\mathbf z_i,\mathbf a_i)-r_i}^2+c_2\norm{Q_\theta(\mathbf z_i,\mathbf a_i)-\big(r_i+\gamma\,Q_{\theta^-}(\mathbf z_{i+1},\pi_\theta(\mathbf z_{i+1}))\big)}^2+c_3\norm{d_\theta(\mathbf z_i,\mathbf a_i)-h_{\theta^-}(\mathbf s_{i+1})}^2\Big]
TD-MPC's model loss: reward prediction, a TD loss for the value, and latent consistency with a target encoder hθ−h_{\theta^-} whose parameters are an exponential moving average of the online ones. zi+1=dθ(zi,ai)\mathbf z_{i+1}=d_\theta(\mathbf z_i,\mathbf a_i) is the unrolled latent, and λ<1\lambda<1 weights nearer steps more. The policy is trained separately to maximize Qθ(z,πθ(z))Q_\theta(\mathbf z,\pi_\theta(\mathbf z)), as in DDPG. (The paper writes states as s\mathbf s and indexes each reward by the step that produces it.)

To act, TD-MPC runs MPPI in the latent space with horizon H=5H=5. Each candidate action sequence is scored by its predicted rewards plus the terminal value, ϕ=∑t=0H−1γtRθ(zt,at)+γHQθ(zH,aH)\phi=\sum_{t=0}^{H-1}\gamma^tR_\theta(\mathbf z_t,\mathbf a_t)+\gamma^HQ_\theta(\mathbf z_H,\mathbf a_H). It samples 512 sequences from a Gaussian plus about 5% more from the policy πθ\pi_\theta, weights the top 64 by eτϕe^{\tau\phi} to refit the Gaussian, repeats for 6 iterations (8 for the Dog tasks, 12 for Humanoid), and warm-starts from the previous step's plan. Early in training the horizon grows from 1 to HH, because the model is still poor.

python
import torch

@torch.no_grad()
def plan(z0, dyn, rew, q, pi, act_dim, mean=None, H=5, iters=6, n=512, n_pi=25, k=64, temp=0.5, gamma=0.99):
    """Latent MPPI with a policy prior. z0: (d,) encoded state. Returns the action and the plan's mean."""
    mean = torch.zeros(H, act_dim) if mean is None else mean       # warm start: last plan shifted by one
    std = torch.full((H, act_dim), 2.0)
    for _ in range(iters):
        acts = (mean + std * torch.randn(n, H, act_dim)).clamp(-1, 1)
        z, prior = z0.expand(n_pi, -1), []
        for t in range(H):                                         # sequences proposed by the policy
            a = pi(z); prior.append(a); z = dyn(z, a)
        acts = torch.cat([acts, torch.stack(prior, dim=1)])
        z, G = z0.expand(len(acts), -1), torch.zeros(len(acts))
        for t in range(H):                                         # predicted rewards along the rollout ...
            G += gamma ** t * rew(z, acts[:, t]); z = dyn(z, acts[:, t])
        G += gamma ** H * q(z, pi(z))                              # ... plus the learned terminal value
        top = G.topk(k).indices
        w = torch.softmax(temp * G[top], dim=0)                    # weights proportional to exp(temp * G)
        mean = (w[:, None, None] * acts[top]).sum(0)
        std = (w[:, None, None] * (acts[top] - mean) ** 2).sum(0).sqrt().clamp_min(0.05)
    return mean[0], mean

A simplified sketch of TD-MPC's planner, not its exact code: the paper also adds exploration noise to the executed action, lowers the spread's floor over training and grows the horizon early on. The policy's proposals keep the search anchored near behavior that the value function has been trained on.

TD-MPC2, and what the experiments show

TD-MPC2 (Hansen, Su & Wang, 2024) kept the recipe and changed what makes it robust and scalable. The consistency target is the stop-gradient encoding sg⁡(h(s′))\operatorname{sg}(h(\mathbf s')); rewards and values are learned by discrete regression, a cross-entropy over bins in a log-transformed space, as in DreamerV3; the policy prior is a maximum-entropy policy; an ensemble of 5 Q-functions supplies targets from the minimum of two randomly chosen members; every network uses LayerNorm and Mish activations; and the latent state is normalized by SimNorm, which splits z\mathbf z into groups of V=8V=8 entries and applies a softmax to each, z∘=[g1,…,gL]\mathbf z^\circ=[\mathbf g_1,\dots,\mathbf g_L] with gi=softmax⁡(zi:i+V/τ)\mathbf g_i=\softmax(\mathbf z_{i:i+V}/\tau), biasing the latent toward sparsity. One set of hyperparameters served 104 continuous-control tasks across 4 domains, and a single 317-million-parameter agent learned 80 tasks spanning several domains, embodiments and action spaces.

ClaimEvidenceCaveat
Planning beats the learned policy aloneIn TD-MPC the policy network was inferior to planning on complex tasks, with a larger gap for high-dimensional action spaces such as the Quadruped tasksPlanning costs an optimization at every step
Latent consistency is a good model targetOver 15 DMControl tasks, reconstruction and contrastive losses improved on no regularization, and latent consistency more consistentlyResults on state-based control tasks
Sample efficiencyTD-MPC learned Humanoid and Dog locomotion from states in about 1 million environment steps and reported better sample efficiency and final performance than prior methods on DMControl and Meta-WorldComparisons depend on implementation details and compute
Stability and scaleTD-MPC2's authors found SimNorm essential to stable training, and larger models scored higher in the multi-task settingReported by the method's authors

Key takeaways

  • A replanning HH-step lookahead with an exact model and a terminal value within ε\varepsilon of V⋆V^\star loses at most 2γHε/(1−γ)2\gamma^H\varepsilon/(1-\gamma): lookahead discounts the value's error by γH\gamma^H but keeps the 1/(1−γ)1/(1-\gamma).
  • The bound is attained: in the procrastination chain, value errors make "act next step" always look better than "act now". The 1/(1−γH)1/(1-\gamma^H) bounds of POLO and LOOP hold for controllers that commit to their plans.
  • With a model whose one-step error is δ\delta, the lookahead error is γHε+(1−γH)δ/(1−γ)\gamma^H\varepsilon+(1-\gamma^H)\delta/(1-\gamma), affine in γH\gamma^H, so uniform bounds never favor an intermediate horizon; practical short horizons rest on errors that grow along rollouts and on improving an imperfect policy.
  • TD-MPC learns a decoder-free latent model with reward, TD and latent-consistency losses and plans with MPPI over 5 steps plus a learned Q, seeded by a policy prior; TD-MPC2 adds SimNorm, discrete regression, a Q ensemble and a maximum-entropy prior to cover 104 tasks with one configuration and to train a 317M-parameter multi-task agent.
  • Planning helped most where the policy network was weakest, consistent with decision-time planning acting as policy improvement rather than as a correction of the value.

Check yourself

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

Question 1

With an exact model and a terminal value within ε\varepsilon of V⋆V^\star, what is the worst-case loss of an HH-step lookahead controller that replans at every step?

  1. 2γHε/(1−γH)2\gamma^H\varepsilon/(1-\gamma^H)
  2. 2γε/(1−γ)2\gamma\varepsilon/(1-\gamma), independent of HH
  3. Zero for H≥2H\ge2
  4. 2γHε/(1−γ)2\gamma^H\varepsilon/(1-\gamma), and the procrastination example attains it
Show the answer

D. 2γHε/(1−γ)2\gamma^H\varepsilon/(1-\gamma), and the procrastination example attains it

Lookahead shrinks the error of the decisions' Q-values to γHε\gamma^H\varepsilon, and a fresh decision every step can lose 2γHε2\gamma^H\varepsilon each time, adding up to 2γHε/(1−γ)2\gamma^H\varepsilon/(1-\gamma). The procrastination chain shows the bound cannot be improved.

Question 2

Why does the replanning controller never take the good action in the procrastination example?

  1. The good action's true value is lower than waiting
  2. At every step the value error makes "wait one step, then act" look better than "act now", and each replanning postpones again
  3. The model gets the transitions wrong
  4. The horizon is too long
Show the answer

B. At every step the value error makes "wait one step, then act" look better than "act now", and each replanning postpones again

The plan "wait, then act" ends its horizon in a state whose value is overestimated, while "act now" ends in one whose value is underestimated. The planner always intends to act next step, and replanning never lets it.

Question 3

Under uniform error bounds, the lookahead error is eH=γHε+(1−γH)δ/(1−γ)e_H=\gamma^H\varepsilon+(1-\gamma^H)\delta/(1-\gamma). What does this imply about choosing the horizon?

  1. eHe_H is affine in γH\gamma^H, so it is smallest at H=1H=1 or as H→∞H\to\infty, never at an intermediate horizon
  2. The best horizon is always H=5H=5
  3. Longer horizons always help
  4. The horizon does not matter
Show the answer

A. eHe_H is affine in γH\gamma^H, so it is smallest at H=1H=1 or as H→∞H\to\infty, never at an intermediate horizon

eH=δ1−γ+γH(ε−δ1−γ)e_H=\frac{\delta}{1-\gamma}+\gamma^H\big(\varepsilon-\frac{\delta}{1-\gamma}\big) moves monotonically with HH, in the direction set by whether the value or the model is the worse source of error. Intermediate horizons need error models that uniform bounds do not capture.

Question 4

What target does TD-MPC's latent-consistency loss use for the predicted latent dθ(zi,ai)d_\theta(\mathbf z_i,\mathbf a_i)?

  1. The next observation's pixels
  2. The reward of the step
  3. The encoding of the next real state by a target encoder, a moving average of the online encoder
  4. The policy's next action
Show the answer

C. The encoding of the next real state by a target encoder, a moving average of the online encoder

TD-MPC never decodes observations. The predicted latent is pulled toward hθ−(si+1)h_{\theta^-}(\mathbf s_{i+1}), and the slowly moving target encoder, like a stop-gradient, keeps this self-prediction from collapsing.

Further reading

  • S. P. Singh & R. C. Yee, "An upper bound on the loss from approximate optimal-value functions," Machine Learning 16, 227–233 (1994).
  • D. P. Bertsekas, Reinforcement Learning and Optimal Control (Athena Scientific, 2019).
  • K. Lowrey, A. Rajeswaran, S. Kakade, E. Todorov & I. Mordatch, "Plan online, learn offline: efficient learning and exploration via model-based control," ICLR (2019).
  • H. Sikchi, W. Zhou & D. Held, "Learning off-policy with online planning," Conference on Robot Learning (2021).
  • N. Hansen, X. Wang & H. Su, "Temporal difference learning for model predictive control," ICML (2022).
  • N. Hansen, H. Su & X. Wang, "TD-MPC2: scalable, robust world models for continuous control," ICLR (2024).