Shortcuts

CEMPlanner

class torchrl.modules.CEMPlanner(*args, **kwargs)[source]

CEMPlanner Module.

Reference: The cross-entropy method for optimization, Botev et al. 2013

This module will perform a CEM planning step when given a TensorDict containing initial states. The CEM planning step is performed by sampling actions from a Gaussian distribution with zero mean and unit variance. The sampled actions are then used to perform a rollout in the environment. The cumulative rewards obtained with the rollout is then ranked. We select the top-k episodes and use their actions to update the mean and standard deviation of the actions distribution. The CEM planning step is repeated for a specified number of steps.

A call to the module returns the actions that empirically maximised the returns given a planning horizon

Parameters:
  • env (EnvBase) – The environment to perform the planning step on (can be ModelBasedEnv or EnvBase).

  • planning_horizon (int) – The length of the simulated trajectories

  • optim_steps (int) – The number of optimization steps used by the MPC planner

  • num_candidates (int) – The number of candidates to sample from the Gaussian distributions.

  • top_k (int) – The number of top candidates to use to update the mean and standard deviation of the Gaussian distribution.

  • reward_key (str, optional) – The key in the TensorDict to use to retrieve the reward. Defaults to “reward”.

  • action_key (str, optional) – The key in the TensorDict to use to store the action. Defaults to “action”

Examples

>>> from tensordict import TensorDict
>>> from torchrl.data import Composite, Unbounded
>>> from torchrl.envs.model_based import ModelBasedEnvBase
>>> from torchrl.modules import SafeModule
>>> class MyMBEnv(ModelBasedEnvBase):
...     def __init__(self, world_model, device="cpu", dtype=None, batch_size=None):
...         super().__init__(world_model, device=device, dtype=dtype, batch_size=batch_size)
...         self.state_spec = Composite(
...             hidden_observation=Unbounded((4,))
...         )
...         self.observation_spec = Composite(
...             hidden_observation=Unbounded((4,))
...         )
...         self.action_spec = Unbounded((1,))
...         self.reward_spec = Unbounded((1,))
...
...     def _reset(self, tensordict: TensorDict) -> TensorDict:
...         tensordict = TensorDict(
...             {},
...             batch_size=self.batch_size,
...             device=self.device,
...         )
...         tensordict = tensordict.update(
...             self.full_state_spec.rand())
...         tensordict = tensordict.update(
...             self.full_action_spec.rand())
...         tensordict = tensordict.update(
...             self.full_observation_spec.rand())
...         return tensordict
...
>>> from torchrl.modules import MLP, WorldModelWrapper
>>> import torch.nn as nn
>>> world_model = WorldModelWrapper(
...     SafeModule(
...         MLP(out_features=4, activation_class=nn.ReLU, activate_last_layer=True, depth=0),
...         in_keys=["hidden_observation", "action"],
...         out_keys=["hidden_observation"],
...     ),
...     SafeModule(
...         nn.Linear(4, 1),
...         in_keys=["hidden_observation"],
...         out_keys=["reward"],
...     ),
... )
>>> env = MyMBEnv(world_model)
>>> # Build a planner and use it as actor
>>> planner = CEMPlanner(env, 10, 11, 7, 3)
>>> env.rollout(5, planner)
TensorDict(
    fields={
        action: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.float32, is_shared=False),
        done: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False),
        hidden_observation: Tensor(shape=torch.Size([5, 4]), device=cpu, dtype=torch.float32, is_shared=False),
        next: TensorDict(
            fields={
                done: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False),
                hidden_observation: Tensor(shape=torch.Size([5, 4]), device=cpu, dtype=torch.float32, is_shared=False),
                reward: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.float32, is_shared=False),
                terminated: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
            batch_size=torch.Size([5]),
            device=cpu,
            is_shared=False),
        terminated: Tensor(shape=torch.Size([5, 1]), device=cpu, dtype=torch.bool, is_shared=False)},
    batch_size=torch.Size([5]),
    device=cpu,
    is_shared=False)
planning(tensordict: TensorDictBase) Tensor[source]

Performs the MPC planning step.

Parameters:

td (TensorDict) – The TensorDict to perform the planning step on.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources