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) – 變換前的規範
- 返回:
轉換後的預期規範