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.