DoubleToFloat¶
- class torchrl.envs.transforms.DoubleToFloat(in_keys: Sequence[NestedKey] | None = None, out_keys: Sequence[NestedKey] | None = None, in_keys_inv: Sequence[NestedKey] | None = None, out_keys_inv: Sequence[NestedKey] | None = None)[source]¶
将选定键的数据类型转换为另一种类型。
根据在构造期间是否提供了
in_keys
或in_keys_inv
,类的行为将发生变化如果提供了键,则仅转换这些条目,并将这些条目从
float64
转换为float32
条目;如果未提供键,并且对象位于转换的环境注册表中,则数据类型设置为
float64
的输入和输出规范将分别用作 in_keys_inv / in_keys。如果未提供键,并且对象在没有环境的情况下使用,则
forward
/inverse
传递将扫描输入 tensordict 中的所有 float64 值,并将它们映射到 float32 张量。对于大型数据结构,这会影响性能,因为此扫描并非免费的。要转换的键不会被缓存。请注意,在这种情况下,无法传递 out_keys(或 out_keys_inv),因为无法准确预测处理键的顺序。
- 参数:
in_keys (嵌套键序列, 可选) – 在公开给外部对象和函数之前,要转换为浮点数的双精度键列表。
out_keys (嵌套键序列, 可选) – 目标键列表。如果未提供,则默认为
in_keys
。in_keys_inv (嵌套键序列, 可选) – 要在传递给包含的 base_env 或存储之前转换为双精度的浮点键列表。
out_keys_inv (嵌套键序列, 可选) – 反向转换的目标键列表。如果未提供,则默认为
in_keys_inv
。
示例
>>> td = TensorDict( ... {'obs': torch.ones(1, dtype=torch.double), ... 'not_transformed': torch.ones(1, dtype=torch.double), ... }, []) >>> transform = DoubleToFloat(in_keys=["obs"]) >>> _ = transform(td) >>> print(td.get("obs").dtype) torch.float32 >>> print(td.get("not_transformed").dtype) torch.float64
在“自动”模式下,所有 float64 条目都会被转换
示例
>>> td = TensorDict( ... {'obs': torch.ones(1, dtype=torch.double), ... 'not_transformed': torch.ones(1, dtype=torch.double), ... }, []) >>> transform = DoubleToFloat() >>> _ = transform(td) >>> print(td.get("obs").dtype) torch.float32 >>> print(td.get("not_transformed").dtype) torch.float32
在不指定转换键的情况下构建环境时,也会采用相同的行为
示例
>>> class MyEnv(EnvBase): ... def __init__(self): ... super().__init__() ... self.observation_spec = CompositeSpec(obs=UnboundedContinuousTensorSpec((), dtype=torch.float64)) ... self.action_spec = UnboundedContinuousTensorSpec((), dtype=torch.float64) ... self.reward_spec = UnboundedContinuousTensorSpec((1,), dtype=torch.float64) ... self.done_spec = UnboundedContinuousTensorSpec((1,), dtype=torch.bool) ... def _reset(self, data=None): ... return TensorDict({"done": torch.zeros((1,), dtype=torch.bool), **self.observation_spec.rand()}, []) ... def _step(self, data): ... assert data["action"].dtype == torch.float64 ... reward = self.reward_spec.rand() ... done = torch.zeros((1,), dtype=torch.bool) ... obs = self.observation_spec.rand() ... assert reward.dtype == torch.float64 ... assert obs["obs"].dtype == torch.float64 ... return obs.empty().set("next", obs.update({"reward": reward, "done": done})) ... def _set_seed(self, seed): ... pass >>> env = TransformedEnv(MyEnv(), DoubleToFloat()) >>> assert env.action_spec.dtype == torch.float32 >>> assert env.observation_spec["obs"].dtype == torch.float32 >>> assert env.reward_spec.dtype == torch.float32, env.reward_spec.dtype >>> print(env.rollout(2)) TensorDict( fields={ action: Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.float32, is_shared=False), done: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.bool, is_shared=False), next: TensorDict( fields={ done: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.bool, is_shared=False), obs: Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.float32, is_shared=False), reward: Tensor(shape=torch.Size([2, 1]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([2]), device=cpu, is_shared=False), obs: Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.float32, is_shared=False)}, batch_size=torch.Size([2]), device=cpu, is_shared=False) >>> assert env.transform.in_keys == ["obs", "reward"] >>> assert env.transform.in_keys_inv == ["action"]