Compose¶
- class torchrl.envs.transforms.Compose(*transforms: Transform)[源代码]¶
组合一系列转换。
Transform
或 ``callable``s 均可接受。示例
>>> env = GymEnv("Pendulum-v0") >>> transforms = [RewardScaling(1.0, 1.0), RewardClipping(-2.0, 2.0)] >>> transforms = Compose(*transforms) >>> transformed_env = TransformedEnv(env, transforms)
- append(transform: Transform | Callable[[TensorDictBase], TensorDictBase]) None [源代码]¶
将转换附加到链中。
Transform
或 callable 均可接受。
- 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.
- insert(index: int, transform: Transform | Callable[[TensorDictBase], TensorDictBase]) None [源代码]¶
将转换插入到链中的指定位置。
Transform
或 callable 均可接受。
- pop(index: int | None = None) Transform [源代码]¶
从链中弹出转换。
- 参数:
index (int, optional) – 要弹出的转换的索引。如果为 None,则弹出最后一个转换。
- 返回:
弹出的转换。
- to(*args, **kwargs)[源代码]¶
移动和/或转换参数和缓冲区。
这可以这样调用
- to(device=None, dtype=None, non_blocking=False)[源代码]
- to(dtype, non_blocking=False)[源代码]
- to(tensor, non_blocking=False)[源代码]
- to(memory_format=torch.channels_last)[源代码]
其签名类似于
torch.Tensor.to()
,但仅接受浮点或复数dtype
。此外,此方法只会将浮点或复数参数和缓冲区转换为dtype
(如果给定)。如果提供了device
,则整数参数和缓冲区将被移动到device
,但dtype
保持不变。当设置non_blocking
时,它会尝试异步(相对于主机)转换/移动,例如将具有固定内存的 CPU Tensor 移动到 CUDA 设备。有关示例,请参阅下文。
注意
此方法就地修改模块。
- 参数:
device (
torch.device
) – 此模块中参数和缓冲区的目标设备dtype (
torch.dtype
) – 此模块中参数和缓冲区的目标浮点或复数 dtypetensor (torch.Tensor) – 其 dtype 和设备是此模块所有参数和缓冲区的目标 dtype 和设备
memory_format (
torch.memory_format
) – 此模块中 4D 参数和缓冲区的目标内存格式(仅关键字参数)
- 返回:
self
- 返回类型:
模块
示例
>>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> linear = nn.Linear(2, 2) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]]) >>> linear.to(torch.double) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1913, -0.3420], [-0.5113, -0.2325]], dtype=torch.float64) >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1) >>> gpu1 = torch.device("cuda:1") >>> linear.to(gpu1, dtype=torch.half, non_blocking=True) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1') >>> cpu = torch.device("cpu") >>> linear.to(cpu) Linear(in_features=2, out_features=2, bias=True) >>> linear.weight Parameter containing: tensor([[ 0.1914, -0.3420], [-0.5112, -0.2324]], dtype=torch.float16) >>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble) >>> linear.weight Parameter containing: tensor([[ 0.3741+0.j, 0.2382+0.j], [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128) >>> linear(torch.ones(3, 2, dtype=torch.cdouble)) tensor([[0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j], [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
- transform_action_spec(action_spec: TensorSpec) TensorSpec [源代码]¶
转换动作规范,使结果规范与变换映射匹配。
- 参数:
action_spec (TensorSpec) – 变换前的规范
- 返回:
转换后的预期规范
- transform_input_spec(input_spec: TensorSpec) TensorSpec [源代码]¶
转换输入规范,使结果规范与转换映射匹配。
- 参数:
input_spec (TensorSpec) – 转换前的规范
- 返回:
转换后的预期规范
- transform_observation_spec(observation_spec: TensorSpec) TensorSpec [源代码]¶
转换观察规范,使结果规范与转换映射匹配。
- 参数:
observation_spec (TensorSpec) – 转换前的规范
- 返回:
转换后的预期规范
- transform_output_spec(output_spec: TensorSpec) TensorSpec [源代码]¶
转换输出规范,使结果规范与转换映射匹配。
此方法通常应保持不变。更改应通过
transform_observation_spec()
、transform_reward_spec()
和transform_full_done_spec()
实现。:param output_spec: 转换之前的 spec :type output_spec: TensorSpec- 返回:
转换后的预期规范
- transform_reward_spec(reward_spec: TensorSpec) TensorSpec [源代码]¶
转换奖励的 spec,使其与变换映射匹配。
- 参数:
reward_spec (TensorSpec) – 变换前的 spec
- 返回:
转换后的预期规范
- transform_state_spec(state_spec: TensorSpec) TensorSpec [源代码]¶
转换状态规范,使结果规范与变换映射匹配。
- 参数:
state_spec (TensorSpec) – 变换前的规范
- 返回:
转换后的预期规范