RandomCropTensorDict¶
- class torchrl.envs.transforms.RandomCropTensorDict(sub_seq_len: int, sample_dim: int = - 1, mask_key: NestedKey | None = None)[源代码]¶
用于 ReplayBuffer 和模块的轨迹子采样器。
沿着输入 tensordict 的最后一个维度收集指定长度的子序列。这可以用于从从 ReplayBuffer 采样的轨迹中获取裁剪的轨迹。
此转换主要设计用于与回放缓冲区和模块一起使用。目前,它不能用作环境转换。如果您希望此行为,请随时通过 issue 请求。
- 参数:
sub_seq_len (int) – 要采样的子轨迹的长度
sample_dim (int, optional) – 应该发生裁剪的维度。倾向于使用负维度,以使转换对具有不同批量维度的 tensordict 具有鲁棒性。默认为 -1(TorchRL 中的默认时间维度)。
mask_key (NestedKey) – 如果提供,则表示采样时要查找的掩码键。如果提供,则只返回有效元素。假定掩码是一个布尔张量,前面是 True 值,后面是 False 值,而不是混合在一起。
RandomCropTensorDict
不会检查这一点,因此不正确的掩码可能导致错误而未被发现。默认值:None(无掩码键)。
- forward(tensordict: TensorDictBase) TensorDictBase [源代码]¶
读取输入 tensordict,并对选定的键应用转换。
默认情况下,此方法
直接调用
_apply_transform()
。不调用
_step()
或_call()
。
此方法不会在任何时候在 env.step 中调用。但是,它会在
sample()
中调用。注意
forward
也可以使用dispatch
将参数名称转换为键,并使用常规关键字参数。示例
>>> class TransformThatMeasuresBytes(Transform): ... '''Measures the number of bytes in the tensordict, and writes it under `"bytes"`.''' ... def __init__(self): ... super().__init__(in_keys=[], out_keys=["bytes"]) ... ... def forward(self, tensordict: TensorDictBase) -> TensorDictBase: ... bytes_in_td = tensordict.bytes() ... tensordict["bytes"] = bytes ... return tensordict >>> t = TransformThatMeasuresBytes() >>> env = env.append_transform(t) # works within envs >>> t(TensorDict(a=0)) # Works offline too.