PrioritizedSliceSampler¶
- class torchrl.data.replay_buffers.PrioritizedSliceSampler(max_capacity: int, alpha: float, beta: float, eps: float = 1e-08, dtype: torch.dtype = torch.float32, reduction: str = 'max', *, num_slices: int | None = None, slice_len: int | None = None, end_key: NestedKey | None = None, traj_key: NestedKey | None = None, ends: torch.Tensor | None = None, trajectories: torch.Tensor | None = None, cache_values: bool = False, truncated_key: NestedKey | None = ('next', 'truncated'), strict_length: bool = True, compile: bool | dict = False, span: bool | int | tuple[bool | int, bool | int] = False, max_priority_within_buffer: bool = False)[源代码]¶
给定起始和停止信号,使用优先采样沿第一个维度对数据切片进行采样。
此类结合了轨迹采样和优先经验回放(PER),正如“Schaul, T.; Quan, J.; Antonoglou, I.; and Silver, D. 2015. Prioritized experience replay.”中所述 (https://arxiv.org/abs/1511.05952)
核心思想:此采样器不是统一采样轨迹切片,而是根据轨迹起始点处转换的重要性来优先考虑它们。这使得学习能够聚焦于轨迹中最具信息量的部分。
工作原理:1. 每个转换根据其 TD 误差分配优先级:\(p_i = |\\delta_i| + \\epsilon\) 2. 轨迹起始点的采样概率为:\(P(i) = \frac{p_i^\alpha}{\\sum_j p_j^\alpha}\) 3. 重要性采样权重用于纠正偏差:\(w_i = (N \\cdot P(i))^{-\beta}\) 4. 从采样到的起始点提取完整的轨迹切片
有关更多信息,请参阅
SliceSampler
和PrioritizedSampler
。警告
PrioritizedSliceSampler 将查看单个转换的优先级,并据此对起始点进行采样。这意味着低优先级的转换可能会出现在样本中,如果它们遵循了高优先级的转换;而高优先级但接近轨迹末尾的转换,如果不能用作起始点,则可能永远不会被采样。目前,用户有责任通过
update_priority()
聚合轨迹项的优先级。- 参数:
max_capacity (int) – 缓冲区的最大容量。
alpha (
float
) – 指数 \(\alpha\) 决定了优先级的程度。 - \(\alpha = 0\):统一采样轨迹起始点 - \(\alpha = 1\):基于起始点的 TD 误差幅度进行完全优先采样 - 典型值:0.4-0.7 用于平衡优先级 - 较高的 \(\alpha\) 意味着对高误差轨迹区域的优先采样更积极beta (
float
) – 重要性采样负指数 \(\beta\)。 - \(\beta\) 控制对优先级引入的偏差的校正 - \(\beta = 0\):无校正(偏向高优先级轨迹区域) - \(\beta = 1\):完全校正(无偏差但可能不稳定) - 典型值:从 0.4-0.6 开始,在训练过程中退火至 1.0 - 训练早期较低的 \(\beta\) 可提供稳定性,后期较高的 \(\beta\) 可减少偏差eps (
float
, optional) – 添加到优先级的微小常数,以确保没有转换具有零优先级。这可以防止轨迹区域永远不被采样。默认为 1e-8。reduction (str, optional) – 多维 tensordicts(即存储的轨迹)的归约方法。可以是“max”、“min”、“median”或“mean”之一。
参数指南: - :math:`alpha` (alpha): 控制对高误差轨迹区域的优先程度
0.4-0.7:学习速度和稳定性之间的良好平衡
1.0:最大优先级(可能不稳定)
0.0:统一采样(无优先级优势)
- :math:`beta` (beta): 控制重要性采样校正
从 0.4-0.6 开始训练以获得稳定性
在训练过程中退火至 1.0 以减少偏差
较低的值 = 更稳定但有偏差
较高的值 = 偏差较小但可能不稳定
- :math:`\epsilon`: 防止优先级为零的小常数
1e-8:良好的默认值
太小:可能导致数值问题
太大:降低优先级效果
- 关键字参数:
num_slices (int) – 要采样的切片数量。批大小必须大于或等于
num_slices
参数。与slice_len
互斥。slice_len (int) – 要采样的切片的长度。批大小必须大于或等于
slice_len
参数且可被其整除。与num_slices
互斥。end_key (NestedKey, optional) – 指示轨迹(或回合)结束的键。默认为
("next", "done")
。traj_key (NestedKey, optional) – 指示轨迹的键。默认为
"episode"
(在 TorchRL 的数据集中常用)。ends (torch.Tensor, optional) – 一个包含运行结束信号的一维布尔张量。当
end_key
或traj_key
获取成本高昂,或当此信号易于获得时使用。必须与cache_values=True
一起使用,并且不能与end_key
或traj_key
结合使用。trajectories (torch.Tensor, optional) – 一个包含运行 ID 的一维整数张量。当
end_key
或traj_key
获取成本高昂,或当此信号易于获得时使用。必须与cache_values=True
一起使用,并且不能与end_key
或traj_key
结合使用。cache_values (bool, optional) –
与静态数据集一起使用。将缓存轨迹的起始和结束信号。即使在调用
extend
时轨迹索引发生变化,也可以安全地使用此选项,因为此操作将清除缓存。警告
cache_values=True
在采样器与由另一个缓冲区扩展的存储一起使用时将不起作用。例如>>> buffer0 = ReplayBuffer(storage=storage, ... sampler=SliceSampler(num_slices=8, cache_values=True), ... writer=ImmutableWriter()) >>> buffer1 = ReplayBuffer(storage=storage, ... sampler=other_sampler) >>> # Wrong! Does not erase the buffer from the sampler of buffer0 >>> buffer1.extend(data)
警告
cache_values=True
如果缓冲区在进程之间共享,并且一个进程负责写入,另一个进程负责采样,则将无法按预期工作,因为清除缓存只能在本地完成。truncated_key (NestedKey, optional) – 如果不为
None
,此参数指示在哪里将截断信号写入输出数据。这用于向值估计器指示提供的轨迹在哪里中断。默认为("next", "truncated")
。此功能仅适用于TensorDictReplayBuffer
实例(否则,截断键将返回在sample()
方法返回的信息字典中)。strict_length (bool, optional) – 如果为
False
,则允许长度小于 slice_len(或 batch_size // num_slices)的轨迹出现在批次中。如果为True
,则会过滤掉长度不足的轨迹。请注意,这可能导致有效 batch_size 短于要求的!轨迹可以使用split_trajectories()
进行分割。默认为True
。compile (bool or dict of kwargs, optional) – 如果为
True
,则sample()
方法的瓶颈将使用compile()
进行编译。也可以通过此参数将关键字参数传递给 torch.compile。默认为False
。span (bool, int, Tuple[bool | int, bool | int], optional) – 如果提供,则采样轨迹将跨越左侧和/或右侧。这意味着可能提供的元素少于所需元素。布尔值表示每个轨迹至少采样一个元素。整数 i 表示每个采样轨迹至少收集 slice_len - i 个样本。使用元组可以精细控制左侧(存储轨迹的开头)和右侧(存储轨迹的结尾)的跨度。
max_priority_within_buffer (bool, optional) – 如果为
True
,则在缓冲区内跟踪最大优先级。如果为False
,则最大优先级跟踪的是自采样器实例化以来的最大值。默认为False
。
示例
>>> import torch >>> from torchrl.data.replay_buffers import TensorDictReplayBuffer, LazyMemmapStorage, PrioritizedSliceSampler >>> from tensordict import TensorDict >>> sampler = PrioritizedSliceSampler(max_capacity=9, num_slices=3, alpha=0.7, beta=0.9) >>> rb = TensorDictReplayBuffer(storage=LazyMemmapStorage(9), sampler=sampler, batch_size=6) >>> data = TensorDict( ... { ... "observation": torch.randn(9,16), ... "action": torch.randn(9, 1), ... "episode": torch.tensor([0,0,0,1,1,1,2,2,2], dtype=torch.long), ... "steps": torch.tensor([0,1,2,0,1,2,0,1,2], dtype=torch.long), ... ("next", "observation"): torch.randn(9,16), ... ("next", "reward"): torch.randn(9,1), ... ("next", "done"): torch.tensor([0,0,1,0,0,1,0,0,1], dtype=torch.bool).unsqueeze(1), ... }, ... batch_size=[9], ... ) >>> rb.extend(data) >>> sample, info = rb.sample(return_info=True) >>> print("episode", sample["episode"].tolist()) episode [2, 2, 2, 2, 1, 1] >>> print("steps", sample["steps"].tolist()) steps [1, 2, 0, 1, 1, 2] >>> print("weight", info["_weight"].tolist()) weight [1.0, 1.0, 1.0, 1.0, 1.0, 1.0] >>> priority = torch.tensor([0,3,3,0,0,0,1,1,1]) >>> rb.update_priority(torch.arange(0,9,1), priority=priority) >>> sample, info = rb.sample(return_info=True) >>> print("episode", sample["episode"].tolist()) episode [2, 2, 2, 2, 2, 2] >>> print("steps", sample["steps"].tolist()) steps [1, 2, 0, 1, 0, 1] >>> print("weight", info["_weight"].tolist()) weight [9.120110917137936e-06, 9.120110917137936e-06, 9.120110917137936e-06, 9.120110917137936e-06, 9.120110917137936e-06, 9.120110917137936e-06]
- update_priority(index: int | torch.Tensor, priority: float | torch.Tensor, *, storage: TensorStorage | None = None) None ¶
更新由索引指向的数据的优先级。
- 参数:
index (int or torch.Tensor) – 要更新优先级的索引。
priority (Number or torch.Tensor) – 索引元素的新的优先级。
- 关键字参数:
storage (Storage, optional) – 用于将 N 维索引大小映射到一维的 sum_tree 和 min_tree 大小的存储。仅在
index.ndim > 2
时需要。