BatchSubSampler¶
- class torchrl.trainers.BatchSubSampler(batch_size: int, sub_traj_len: int = 0, min_sub_traj_len: int = 0)[原始碼]¶
用於線上 RL sota-implementations 的資料子取樣器。
此類對剛從環境中收集到的整批資料的一部分進行子取樣。
- 引數:
batch_size (int) – 要收集的子批次大小。提供的批次大小必須等於輸出 tensordict 中的專案總數,這將具有大小 [batch_size // sub_traj_len, sub_traj_len]。
sub_traj_len (int, optional) – 線上環境中子取樣軌跡的長度。預設為 -1(即,取軌跡的完整長度)
min_sub_traj_len (int, optional) –
sub_traj_len的最小值,以防批次中的某些元素包含少量步驟。預設為 -1(即,無最小值)
示例
>>> td = TensorDict( ... { ... key1: torch.stack([torch.arange(0, 10), torch.arange(10, 20)], 0), ... key2: torch.stack([torch.arange(0, 10), torch.arange(10, 20)], 0), ... }, ... [2, 10], ... ) >>> trainer.register_op( ... "process_optim_batch", ... BatchSubSampler(batch_size=batch_size, sub_traj_len=sub_traj_len), ... ) >>> td_out = trainer._process_optim_batch_hook(td) >>> assert td_out.shape == torch.Size([batch_size // sub_traj_len, sub_traj_len])
- register(trainer: Trainer, name: str = 'batch_subsampler')[原始碼]¶
Registers the hook in the trainer at a default location.
- 引數:
trainer (Trainer) – the trainer where the hook must be registered.
name (str) – the name of the hook.
注意
To register the hook at another location than the default, use
register_op().