MultiStepActorWrapper¶
- class torchrl.modules.tensordict_module.MultiStepActorWrapper(*args, **kwargs)[源]¶
一个多动作 actor 的封装器。
此类允许在环境中执行宏(macros)。actor 的动作(action)条目必须包含一个额外的时间维度才能被使用。它必须紧邻输入 tensordict 的最后一个维度(即在
tensordict.ndim
处)。如果未提供动作(action)条目键,将使用简单的启发式方法从 actor 自动检索(任何以字符串
"action"
结尾的嵌套键)。输入 tensordict 中还必须存在一个
"is_init"
条目,用于跟踪当前收集应何时因遇到“完成”状态而中断。与action_keys
不同,此键必须是唯一的。- 参数:
actor (TensorDictModuleBase) – 一个 actor。
n_steps (int) – actor 一次输出的动作数量(前瞻窗口)。
- 关键字参数:
action_keys (list of NestedKeys, 可选) – 来自环境的动作(action)键。可以从
env.action_keys
中检索。默认为actor
的所有以字符串"action"
结尾的out_keys
。init_key (NestedKey, 可选) – 指示环境何时经历重置的条目键。默认为
"is_init"
,这是来自InitTracker
变换的out_key
。
示例
>>> import torch.nn >>> from torchrl.modules.tensordict_module.actors import MultiStepActorWrapper, Actor >>> from torchrl.envs import CatFrames, GymEnv, TransformedEnv, SerialEnv, InitTracker, Compose >>> from tensordict.nn import TensorDictSequential as Seq, TensorDictModule as Mod >>> >>> time_steps = 6 >>> n_obs = 4 >>> n_action = 2 >>> batch = 5 >>> >>> # Transforms a CatFrames in a stack of frames >>> def reshape_cat(data: torch.Tensor): ... return data.unflatten(-1, (time_steps, n_obs)) >>> # an actor that reads `time_steps` frames and outputs one action per frame >>> # (actions are conditioned on the observation of `time_steps` in the past) >>> actor_base = Seq( ... Mod(reshape_cat, in_keys=["obs_cat"], out_keys=["obs_cat_reshape"]), ... Mod(torch.nn.Linear(n_obs, n_action), in_keys=["obs_cat_reshape"], out_keys=["action"]) ... ) >>> # Wrap the actor to dispatch the actions >>> actor = MultiStepActorWrapper(actor_base, n_steps=time_steps) >>> >>> env = TransformedEnv( ... SerialEnv(batch, lambda: GymEnv("CartPole-v1")), ... Compose( ... InitTracker(), ... CatFrames(N=time_steps, in_keys=["observation"], out_keys=["obs_cat"], dim=-1) ... ) ... ) >>> >>> print(env.rollout(100, policy=actor, break_when_any_done=False)) TensorDict( fields={ action: Tensor(shape=torch.Size([5, 100, 2]), device=cpu, dtype=torch.float32, is_shared=False), action_orig: Tensor(shape=torch.Size([5, 100, 6, 2]), device=cpu, dtype=torch.float32, is_shared=False), counter: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.int32, is_shared=False), done: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False), is_init: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False), next: TensorDict( fields={ done: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False), is_init: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False), obs_cat: Tensor(shape=torch.Size([5, 100, 24]), device=cpu, dtype=torch.float32, is_shared=False), observation: Tensor(shape=torch.Size([5, 100, 4]), device=cpu, dtype=torch.float32, is_shared=False), reward: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.float32, is_shared=False), terminated: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False), truncated: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False)}, batch_size=torch.Size([5, 100]), device=cpu, is_shared=False), obs_cat: Tensor(shape=torch.Size([5, 100, 24]), device=cpu, dtype=torch.float32, is_shared=False), observation: Tensor(shape=torch.Size([5, 100, 4]), device=cpu, dtype=torch.float32, is_shared=False), terminated: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False), truncated: Tensor(shape=torch.Size([5, 100, 1]), device=cpu, dtype=torch.bool, is_shared=False)}, batch_size=torch.Size([5, 100]), device=cpu, is_shared=False)
- forward(tensordict: TensorDictBase) TensorDictBase [源]¶
定义每次调用时执行的计算。
应由所有子类重写。
注意
尽管前向传播(forward pass)的实现需要在该函数内定义,但之后应调用
Module
实例而非此函数,因为前者会负责运行注册的 hook,而后者会静默忽略它们。
- property init_key: NestedKey¶
批处理中给定元素的初始步骤指示器。