LineariseRewards¶
- class torchrl.envs.transforms.LineariseRewards(in_keys: Sequence[NestedKey], out_keys: Sequence[NestedKey] | None = None, *, weights: Sequence[float] | Tensor | None = None)[源]¶
通过加权求和将多目标奖励信号转换为单目标奖励信号。
- 参数:
in_keys (List[NestedKey]) – 查找多目标奖励的键列表。
out_keys (List[NestedKey], optional) – 应写入单目标奖励的键列表。默认为
in_keys
。weights (List[float], Tensor, optional) – 指示在求和时如何对每个奖励进行加权。默认为 [1.0, 1.0, …]。
警告
如果传入的 in_keys 序列长度严格大于一(例如,多智能体设置中每个智能体一组),则相同的权重将应用于每个条目。如果需要对每个组进行不同的奖励聚合,请连续使用多个
LineariseRewards
。示例
>>> import mo_gymnasium as mo_gym >>> from torchrl.envs import MOGymWrapper >>> mo_env = MOGymWrapper(mo_gym.make("deep-sea-treasure-v0")) >>> mo_env.reward_spec BoundedContinuous( shape=torch.Size([2]), space=ContinuousBox( low=Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.float32, contiguous=True), high=Tensor(shape=torch.Size([2]), device=cpu, dtype=torch.float32, contiguous=True)), ...) >>> so_env = TransformedEnv(mo_env, LineariseRewards(in_keys=("reward",))) >>> so_env.reward_spec BoundedContinuous( shape=torch.Size([1]), space=ContinuousBox( low=Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.float32, contiguous=True), high=Tensor(shape=torch.Size([1]), device=cpu, dtype=torch.float32, contiguous=True)), ...) >>> td = so_env.rollout(5) >>> td["next", "reward"].shape torch.Size([5, 1])
- transform_reward_spec(reward_spec: TensorSpec) TensorSpec [源]¶
转换奖励规范,使结果规范与转换映射匹配。
- 参数:
reward_spec (TensorSpec) – 转换前的规范
- 返回:
转换后预期的规范