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: Union[Transform, Callable[[TensorDictBase], TensorDictBase]]) None [源代码]¶
在链中追加一个转换。
Transform
或 callable 可以被接受。
- insert(index: int, transform: Union[Transform, Callable[[TensorDictBase], TensorDictBase]]) None [源代码]¶
在链中的所需索引处插入一个转换。
Transform
或 callable 可以被接受。
- 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 张量移动到 CUDA 设备。请参阅下面的示例。
注意
此方法就地修改模块。
- 参数:
device (
torch.device
) – 此模块中参数和缓冲区的所需设备dtype (
torch.dtype
) – 此模块中参数和缓冲区的所需浮点型或复数 dtypetensor (torch.Tensor) – 张量,其 dtype 和设备是此模块中所有参数和缓冲区的所需 dtype 和设备
memory_format (
torch.memory_format
) – 此模块中 4D 参数和缓冲区的所需内存格式(仅关键字参数)
- 返回值:
self
- 返回类型:
Module
示例
>>> # 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_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()
和transformfull_done_spec()
来实现。 :param output_spec: 转换前的规格 :type output_spec: TensorSpec- 返回值:
转换后期望的规格
- transform_reward_spec(reward_spec: TensorSpec) TensorSpec [源代码]¶
转换奖励规格,使结果规格与转换映射匹配。
- 参数:
reward_spec (TensorSpec) – 转换前的规格
- 返回值:
转换后期望的规格