Compose¶
- class torchrl.envs.transforms.Compose(transforms: list[torchrl.envs.transforms.transforms.Transform])[源代码]¶
组合一个转换链。
接受
Transform
或 ``callable``。该类可以通过几种方式实例化
- 参数:
示例
>>> env = GymEnv("Pendulum-v0") >>> >>> # Method 1: Using positional arguments >>> transforms = Compose(RewardScaling(1.0, 1.0), RewardClipping(-2.0, 2.0)) >>> transformed_env = TransformedEnv(env, transforms) >>> >>> # Method 2: Using a list with positional argument >>> transform_list = [RewardScaling(1.0, 1.0), RewardClipping(-2.0, 2.0)] >>> transforms = Compose(transform_list) >>> transformed_env = TransformedEnv(env, transforms) >>> >>> # Method 3: Using keyword argument >>> transforms = Compose(transforms=[RewardScaling(1.0, 1.0), RewardClipping(-2.0, 2.0)]) >>> transformed_env = TransformedEnv(env, transforms)
- append(transform: Transform | Callable[[TensorDictBase], TensorDictBase]) None [源代码]¶
在链中追加一个转换。
接受
Transform
或可调用对象。
- 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
或可调用对象。
- 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
(如果给定),但dtype
保持不变。当设置non_blocking
时,它会尝试与主机异步转换/移动(如果可能),例如,将具有固定内存的 CPU Tensor 移动到 CUDA 设备。有关示例,请参阅下文。
注意
此方法就地修改模块。
- 参数:
device (
torch.device
) – the desired device of the parameters and buffers in this module – 此模块中参数和缓冲区的目标设备。dtype (
torch.dtype
) – the desired floating point or complex dtype of the parameters and buffers in this module – 此模块中参数和缓冲区的目标浮点数或复数 dtype。tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module – 其 dtype 和 device 是此模块中所有参数和缓冲区的目标 dtype 和 device 的 Tensor。
memory_format (
torch.memory_format
) – the desired memory format for 4D parameters and buffers in this module (keyword only argument) – 此模块中 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) – 变换前的规范
- 返回:
转换后的预期规范