快捷方式

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 可以被接受。

forward(tensordict: TensorDictBase) TensorDictBase[源代码]

读取输入 tensordict,并对选定的键应用转换。

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) – 此模块中参数和缓冲区的所需浮点型或复数 dtype

  • tensor (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_env_batch_size(batch_size: torch.batch_size)[源代码]

转换父环境的批次大小。

transform_env_device(device: device)[源代码]

转换父环境的设备。

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) – 转换前的规格

返回值:

转换后期望的规格

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源