Lab 12. Model-Based RL with Learned Ensembles

Work in progress: this part of the notes is still being revised.

Overview

This lab puts chapter 23 to work. You will learn a model of a pendulum’s dynamics from a few hundred transitions, plan through it with the cross-entropy method as PETS does, and then use the same kind of model to generate short rollouts for a soft actor–critic as MBPO does. Along the way you will test two of the chapter’s claims against small experiments: that modeling uncertainty keeps a planner out of trouble, and that a model’s rollouts, rather than many updates per real sample, are what make MBPO data-efficient.

Part 1 — Probabilistic ensembles and model predictive control

  1. The model. Write an ensemble of \(E\) networks trained together (batched matrix multiplications make this as cheap as one wider network). Each member maps \((\cos\theta,\sin\theta,\dot\theta/8,u/2)\) through two hidden layers of 64 SiLU units to the mean and log-variance of a Gaussian over the change in the observation, with the speed component scaled back up by 8. Bound the log-variance softly between learned limits, as PETS does: \(\log\sigma^2\leftarrow v_{\max}-\operatorname{softplus}(v_{\max}-\log\sigma^2)\) and \(\log\sigma^2\leftarrow v_{\min}+\operatorname{softplus}(\log\sigma^2-v_{\min})\), starting from \(v_{\max}=0\) and \(v_{\min}=-10\).
  2. Training. Minimize the Gaussian negative log-likelihood \((\mu-s')^2e^{-\log\sigma^2}+\log\sigma^2\), averaged over components, plus \(0.01(v_{\max}-v_{\min})\) to keep the limits tight. Give each member its own bootstrap minibatch of 256 drawn with replacement from all the data, and after every episode take 400 Adam steps with step size \(10^{-3}\). For comparison, write a deterministic variant: one network that outputs a mean only, trained by squared error.
  3. Planning. At every step, choose the action by the cross-entropy method over sequences of \(H=15\) torques: sample 200 sequences from a Gaussian, clip them to the torque limits, score each, refit the Gaussian to the best 20, and repeat 4 times, then execute the first action of the final mean. Start from the previous step’s plan shifted by one step, with standard deviation 1.5, and keep at least 0.05. Score a sequence by propagating one particle per member, each sampling its next observation from its own member’s Gaussian at every step (the “TS∞” scheme of exercise 23.4), and averaging the particles’ predicted returns.
  4. Learning loop. Run one episode of uniformly random torques, then 14 episodes of planning, refitting the model to all the data after each. Record each episode’s return, for the ensemble of 5 and for the deterministic network, with 3 seeds.

Part 2 — Branched model rollouts for a model-free learner

  1. Start from your SAC of Lab 11, with networks of two layers of 64 units, Adam with step size \(3\times10^{-4}\), minibatches of 256, \(\gamma=0.99\), \(\tau=0.005\), a target entropy of \(-1\), and 500 initial random steps. Run it for 4,000 environment steps with one update per step and with 10.
  2. MBPO. From step 500, fit a probabilistic ensemble of 5 to the real data every 250 steps (300 Adam steps). At every environment step, start 50 rollouts from states drawn from the real replay memory, and run each for \(k\) steps under the current stochastic policy, choosing a random member for each rollout at each step and computing the rewards with the known reward function. Grow \(k\) linearly from 1 to 5 over the first 3,000 steps, and store the model transitions in a separate memory of 50,000.
  3. Train SAC with 10 updates per step on minibatches of 243 model and 13 real transitions, 95% model data as in MBPO.
  4. Every 1,000 steps, evaluate the deterministic policy over 5 episodes. Use 3 seeds for each of the three agents.

Expected results

output
=== Part 1: learning the pendulum with a learned model and MPC (15 episodes of 200 steps, 3 seeds) ===
  return of each episode (the first with random torques), mean of seeds; a return above about -200 means
  the pendulum is swung up and held
                                      episode:  1       2       3       5       8      10      15
  probabilistic ensemble of 5 (PETS)        -1346    -721    -810    -255    -168    -284    -163   seeds at episode 15:  -124  -124  -240
  one deterministic network                 -1346    -669    -128    -166    -124    -123    -123   seeds at episode 15:  -129  -115  -125

=== Part 2: SAC, SAC with 10 updates per step, and MBPO, 4,000 environment steps, 3 seeds ===
  average return of the deterministic policy over 5 episodes, mean of seeds
                                        steps: 1,000   2,000   3,000   4,000   seeds at 4,000
  SAC, 1 update per step                        -1570   -1659    -867   -1300   -1287 -1339 -1273
  SAC, 10 updates per step                       -904    -142    -150    -143    -142  -144  -142
  MBPO (SAC, 10 updates, model data)            -1489    -303    -165    -141    -141  -140  -141

Things to notice:

Going further

  1. Uncertainty where it matters. Make the pendulum stochastic by adding Gaussian noise to the applied torque, or by drawing the pendulum’s mass anew in each episode, and repeat part 1. Does the probabilistic ensemble now beat the deterministic network, and does each of its two ingredients, the variance head and the ensemble, matter on its own?
  2. Watching a planner exploit its model. Train a deterministic network on the random episode only, then plan with a longer horizon (\(H=50\)) and more iterations of the cross-entropy method. Compare the planner’s predicted returns with the returns obtained, and repeat with the ensemble. Relate the gap to exercise 23.1.
  3. Rollout length and the real-data fraction. Run MBPO with \(k\) fixed at 1, 5, and 15, and with a 50% fraction of real data. Which settings hurt, and how does this match the bound of exercise 23.3?
  4. A harder body. Compare MBPO with SAC at 10 updates per step on HalfCheetah-v5 for 30,000 steps, with networks of 200 units and an ensemble of 7. Does the model help once the dynamics are harder to fit and the real data scarcer?
  5. A value at the end of the horizon. Add SAC’s learned value of the final predicted state to the cross-entropy method’s score, as TD-MPC does, and shorten the horizon to 5. How short can the horizon become before planning stops helping?