Shortcuts

ding.agent

a2c

请参考 ding/bonus/a2c.py 获取更多详细信息。

A2CAgent

class ding.bonus.a2c.A2CAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[源代码]
Overview:

用于训练、评估和部署强化学习算法优势演员评论家(A2C)的代理类。 有关RL代理系统设计的更多信息,请参阅 <https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[source]
Overview:

初始化A2C算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): A2C算法的模型,应该是类 ding.model.VAC 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): A2C算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/A2C/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLanderContinuous-v2 registered in gym, and we want to train an agent with A2C algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = A2CAgent(env_id='LunarLanderContinuous-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLanderContinuous-v2'}, 'policy': ...... }
>>> agent = A2CAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLanderContinuous-v2')
>>> agent = A2CAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = VAC(**cfg.policy.model)
>>> agent = A2CAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = A2CAgent(cfg=cfg, policy_state_dict='LunarLanderContinuous-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用A2C算法对代理进行评估,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: A2CAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (A2CAgent): 拥有最佳模型的代理。

Examples:
>>> agent = A2CAgent(env_id='LunarLanderContinuous-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用A2C算法收集数据,共进行n_episode个回合,使用env_num个收集环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境交互使用A2C算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。评估结果将被返回。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int = 4, evaluator_env_num: int = 4, n_iter_log_show: int = 500, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[来源]
Overview:

使用A2C算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

c51

请参考 ding/bonus/c51.py 获取更多详细信息。

C51代理

class ding.bonus.c51.C51Agent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[source]
Overview:

用于训练、评估和部署强化学习算法C51的代理类。 有关RL代理系统设计的更多信息,请参阅 <https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[源代码]
Overview:

初始化C51算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): C51算法的模型,应该是类 ding.model.C51DQN 的一个实例。如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): C51算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/C51/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLander-v2 registered in gym, and we want to train an agent with C51 algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = C51Agent(env_id='LunarLander-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLander-v2'}, 'policy': ...... }
>>> agent = C51Agent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLander-v2')
>>> agent = C51Agent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = C51DQN(**cfg.policy.model)
>>> agent = C51Agent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = C51Agent(cfg=cfg, policy_state_dict='LunarLander-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用C51算法评估代理,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: C51Agent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (C51Agent): 拥有最佳模型的代理。

Examples:
>>> agent = C51Agent(env_id='LunarLander-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用C51算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[源代码]
Overview:

通过与环境交互部署使用C51算法的代理,在此期间如果enable_save_replay为True,则可以保存回放视频。将返回评估结果。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[来源]
Overview:

使用C51算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

ddpg

请参考 ding/bonus/ddpg.py 获取更多详细信息。

DDPG代理

class ding.bonus.ddpg.DDPGAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[source]
Overview:

用于训练、评估和部署强化学习算法深度确定性策略梯度(DDPG)的代理类。 有关RL代理系统设计的更多信息,请参阅 <https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[源代码]
Overview:

为DDPG算法初始化代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): DDPG算法的模型,应该是类 ding.model.ContinuousQAC 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): DDPG算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/DDPG/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLanderContinuous-v2 registered in gym, and we want to train an agent with DDPG algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = DDPGAgent(env_id='LunarLanderContinuous-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLanderContinuous-v2'}, 'policy': ...... }
>>> agent = DDPGAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLanderContinuous-v2')
>>> agent = DDPGAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = ContinuousQAC(**cfg.policy.model)
>>> agent = DDPGAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = DDPGAgent(cfg=cfg, policy_state_dict='LunarLanderContinuous-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用DDPG算法评估代理,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: DDPGAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (DDPGAgent): 拥有最佳模型的代理。

Examples:
>>> agent = DDPGAgent(env_id='LunarLanderContinuous-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用DDPG算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境交互使用DDPG算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。将返回评估结果。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_log_show: int = 500, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[source]
Overview:

使用DDPG算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

dqn

请参考 ding/bonus/dqn.py 获取更多详细信息。

DQNAgent

class ding.bonus.dqn.DQNAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[source]
Overview:

用于训练、评估和部署强化学习算法深度Q学习(DQN)的代理类。 有关RL代理系统设计的更多信息,请参阅 <https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[source]
Overview:

初始化DQN算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): DQN算法的模型,应该是类 ding.model.DQN 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): DQN算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/DQN/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLander-v2 registered in gym, and we want to train an agent with DQN algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = DQNAgent(env_id='LunarLander-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLander-v2'}, 'policy': ...... }
>>> agent = DQNAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLander-v2')
>>> agent = DQNAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = DQN(**cfg.policy.model)
>>> agent = DQNAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = DQNAgent(cfg=cfg, policy_state_dict='LunarLander-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用DQN算法对代理进行评估,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: DQNAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (DQNAgent): 拥有最佳模型的代理。

Examples:
>>> agent = DQNAgent(env_id='LunarLander-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[source]
Overview:

使用DQN算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境的交互使用DQN算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。评估结果将被返回。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[来源]
Overview:

使用DQN算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

pg

请参考 ding/bonus/pg.py 获取更多详细信息。

PG代理

class ding.bonus.pg.PGAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[源代码]
Overview:

用于训练、评估和部署强化学习算法策略梯度(PG)的代理类。 有关RL代理系统设计的更多信息,请参阅 <https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[源代码]
Overview:

初始化PG算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): PG算法的模型,应该是类 ding.model.PG 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): PG算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/PG/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLanderContinuous-v2 registered in gym, and we want to train an agent with PG algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = PGAgent(env_id='LunarLanderContinuous-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLanderContinuous-v2'}, 'policy': ...... }
>>> agent = PGAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLanderContinuous-v2')
>>> agent = PGAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = PG(**cfg.policy.model)
>>> agent = PGAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = PGAgent(cfg=cfg, policy_state_dict='LunarLanderContinuous-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[源代码]
Overview:

使用PG算法对代理进行评估,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: PGAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (PGAgent): 拥有最佳模型的代理。

Examples:
>>> agent = PGAgent(env_id='LunarLanderContinuous-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用PG算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境的交互使用PG算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。将返回评估结果。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[来源]
Overview:

使用PG算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

ppo_offpolicy

请参考 ding/bonus/ppo_offpolicy.py 了解更多详情。

PPOOffPolicyAgent

class ding.bonus.ppo_offpolicy.PPOOffPolicyAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[来源]
Overview:

用于训练、评估和部署强化学习算法Proximal Policy Optimization(PPO)的代理类,采用离策略风格。 有关RL代理系统设计的更多信息,请参阅<https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[source]
Overview:

初始化PPO(offpolicy)算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): PPO(offpolicy)算法的模型, 应该是类 ding.model.VAC 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): PPO (offpolicy) 算法的配置,它是一个字典。默认值为 None。如果未指定,将使用默认配置。默认配置可以在 ding/config/example/PPO (offpolicy)/gym_lunarlander_v2.py 中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLander-v2 registered in gym, and we want to train an agent with PPO (offpolicy) algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = PPOOffPolicyAgent(env_id='LunarLander-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLander-v2'}, 'policy': ...... }
>>> agent = PPOOffPolicyAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLander-v2')
>>> agent = PPOOffPolicyAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = VAC(**cfg.policy.model)
>>> agent = PPOOffPolicyAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = PPOOffPolicyAgent(cfg=cfg, policy_state_dict='LunarLander-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用PPO(offpolicy)算法评估代理,进行n_evaluator_episode个回合,并使用env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: PPOOffPolicyAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
Examples:
>>> agent = PPOOffPolicyAgent(env_id='LunarLander-v2')
>>> agent.train()
>>> agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[source]
Overview:

使用PPO(offpolicy)算法收集数据,共收集n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用PPO(offpolicy)算法部署代理,与环境进行交互,如果enable_save_replay为True,则可以保存回放视频。将返回评估结果。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[source]
Overview:

使用PPO(offpolicy)算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

ppof

请参考 ding/bonus/ppof.py 了解更多详情。

PPOF

class ding.bonus.ppof.PPOF(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[源代码]
Overview:

用于训练、评估和部署强化学习算法近端策略优化(PPO)的代理类。 有关RL代理系统设计的更多信息,请参阅<https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[源代码]
Overview:

初始化PPO算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg中指定env_id。如果指定了env_id,则cfg中的env_id将被忽略。env_id应该是支持的环境之一,可以在PPOF.supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_idcfg.env_idenv_idcfg.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): PPO算法的模型,应该是类 ding.model.PPOFModel 的一个实例。如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): PPO算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLander-v2 registered in gym, and we want to train an agent with PPO algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = PPOF(env_id='LunarLander-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLander-v2'}, 'policy': ...... }
>>> agent = PPOF(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLander-v2')
>>> agent = PPOF(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = VAC(**cfg.policy.model)
>>> agent = PPOF(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = PPOF(cfg=cfg, policy_state_dict='LunarLander-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用PPO算法对代理进行评估,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: PPOF
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (PPOF): 拥有最佳模型的代理。

Examples:
>>> agent = PPOF(env_id='LunarLander-v2')
>>> agent.train()
>>> agent = agent.best()

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用PPO算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境交互使用PPO算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。评估结果将被返回。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int = 4, evaluator_env_num: int = 4, n_iter_log_show: int = 500, n_iter_save_ckpt: int = 1000, context: str | None = None, reward_model: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[来源]
Overview:

使用PPO算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境的数量。默认为4。

  • evaluator_env_num (int): 评估器环境的数量。默认为4。

  • n_iter_log_show (int): 每次训练迭代的日志记录频率。默认为500。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • reward_model (str): 奖励模型名称。默认为 None。此参数目前尚不支持。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

sac

请参考 ding/bonus/sac.py 获取更多详细信息。

SAC代理

class ding.bonus.sac.SACAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[source]
Overview:

用于训练、评估和部署强化学习算法Soft Actor-Critic(SAC)的代理类。 有关RL代理系统设计的更多信息,请参阅<https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[源代码]
Overview:

初始化SAC算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): SAC算法的模型,应该是类 ding.model.ContinuousQAC 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (Union[EasyDict, dict]): SAC算法的配置,它是一个字典。默认值为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/SAC/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLanderContinuous-v2 registered in gym, and we want to train an agent with SAC algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = SACAgent(env_id='LunarLanderContinuous-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLanderContinuous-v2'}, 'policy': ...... }
>>> agent = SACAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLanderContinuous-v2')
>>> agent = SACAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = ContinuousQAC(**cfg.policy.model)
>>> agent = SACAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = SACAgent(cfg=cfg, policy_state_dict='LunarLanderContinuous-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[源代码]
Overview:

使用SAC算法评估代理,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: SACAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (SACAgent): 拥有最佳模型的代理。

Examples:
>>> agent = SACAgent(env_id='LunarLanderContinuous-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用SAC算法收集n_episode个回合的数据,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在其中,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境交互使用SAC算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。将返回评估结果。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[source]
Overview:

使用SAC算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

SQL

请参考 ding/bonus/sql.py 获取更多详细信息。

SQL代理

class ding.bonus.sql.SQLAgent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[source]
Overview:

用于训练、评估和部署强化学习算法Soft Q-Learning(SQL)的代理类。 有关RL代理系统设计的更多信息,请参阅<https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[来源]
Overview:

初始化SQL算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): SQL算法的模型,应该是类 ding.model.DQN 的一个实例。 如果未指定,将根据配置生成默认模型。

  • cfg (:obj:Union[EasyDict, dict]): SQL算法的配置,它是一个字典。默认为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/SQL/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLander-v2 registered in gym, and we want to train an agent with SQL algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = SQLAgent(env_id='LunarLander-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLander-v2'}, 'policy': ...... }
>>> agent = SQLAgent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLander-v2')
>>> agent = SQLAgent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = DQN(**cfg.policy.model)
>>> agent = SQLAgent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = SQLAgent(cfg=cfg, policy_state_dict='LunarLander-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用SQL算法评估代理,评估n_evaluator_episode个回合,使用env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: SQLAgent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (SQLAgent): 拥有最佳模型的代理。

Examples:
>>> agent = SQLAgent(env_id='LunarLander-v2')
>>> agent.train()
>>> agent = agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用SQL算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境的交互部署带有SQL算法的代理,在此期间如果enable_save_replay为True,则可以保存回放视频。评估结果将被返回。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[source]
Overview:

使用SQL算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录和保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。

td3

请参考 ding/bonus/td3.py 获取更多详细信息。

TD3代理

class ding.bonus.td3.TD3Agent(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None)[source]
Overview:

用于训练、评估和部署强化学习算法 Twin Delayed Deep Deterministic Policy Gradient(TD3)的代理类。 有关RL代理系统设计的更多信息,请参阅 <https://di-engine-docs.readthedocs.io/en/latest/03_system/agent.html>。

Interface:

__init__, train, deploy, collect_data, batch_evaluate, best

__init__(env_id: str | None = None, env: BaseEnv | None = None, seed: int = 0, exp_name: str | None = None, model: Module | None = None, cfg: EasyDict | dict | None = None, policy_state_dict: str | None = None) None[来源]
Overview:

初始化TD3算法的代理。

Arguments:
  • env_id (str): 环境ID,这是在gym或gymnasium中注册的环境名称。如果未指定env_id,则必须在cfg.env中指定env_id。如果指定了env_id,则cfg.env中的env_id将被忽略。env_id应该是支持的环境之一,可以在supported_env_list中找到。

  • env (BaseEnv): 用于训练和评估的环境实例。如果未指定env,则必须指定env_id`cfg.env.env_idenv_idcfg.env.env_id将用于创建环境实例。如果指定了env,则env_idcfg.env.env_id将被忽略。

  • seed (int): 随机种子,在运行程序之前设置。默认为0。

  • exp_name (str): 此实验的名称,将用于创建保存日志数据的文件夹。默认为 None。如果未指定,文件夹名称将为 env_id-algorithm

  • 模型 (torch.nn.Module): TD3算法的模型,应该是类 ding.model.ContinuousQAC 的一个实例。 如果未指定,将根据配置生成一个默认模型。

  • cfg (:obj:Union[EasyDict, dict]): TD3算法的配置,它是一个字典。默认为None。如果未指定,将使用默认配置。默认配置可以在ding/config/example/TD3/gym_lunarlander_v2.py中找到。

  • policy_state_dict (str): 由PyTorch保存在本地文件中的策略状态字典的路径。如果指定,策略将从该文件加载。默认为None。

注意

An RL Agent Instance can be initialized in two basic ways. For example, we have an environment with id LunarLanderContinuous-v2 registered in gym, and we want to train an agent with TD3 algorithm with default configuration. Then we can initialize the agent in the following ways:
>>> agent = TD3Agent(env_id='LunarLanderContinuous-v2')
or, if we want can specify the env_id in the configuration:
>>> cfg = {'env': {'env_id': 'LunarLanderContinuous-v2'}, 'policy': ...... }
>>> agent = TD3Agent(cfg=cfg)

在初始化时还有其他参数可以指定代理。 例如,如果我们想要指定环境实例:

>>> env = CustomizedEnv('LunarLanderContinuous-v2')
>>> agent = TD3Agent(cfg=cfg, env=env)
or, if we want to specify the model:
>>> model = ContinuousQAC(**cfg.policy.model)
>>> agent = TD3Agent(cfg=cfg, model=model)
or, if we want to reload the policy from a saved policy state dict:
>>> agent = TD3Agent(cfg=cfg, policy_state_dict='LunarLanderContinuous-v2.pth.tar')

确保配置与保存的策略状态字典一致。

batch_evaluate(env_num: int = 4, n_evaluator_episode: int = 4, context: str | None = None, debug: bool = False) EvalReturn[source]
Overview:

使用TD3算法对代理进行评估,使用n_evaluator_episode个回合和env_num个评估环境。评估结果将被返回。 方法batch_evaluatedeploy之间的区别在于,batch_evaluate将创建多个评估环境来评估代理以获得平均性能,而deploy将只创建一个评估环境来评估代理并保存回放视频。

Arguments:
  • env_num (int): 评估器环境的数量。默认为4。

  • n_evaluator_episode (int): 评估的剧集数量。默认为4。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

property best: TD3Agent
Overview:

从检查点目录加载最佳模型,默认情况下该目录位于文件夹 exp_name/ckpt/eval.pth.tar 中。返回值是带有最佳模型的代理。

Returns:
  • (TD3Agent): 拥有最佳模型的代理。

Examples:
>>> agent = TD3Agent(env_id='LunarLanderContinuous-v2')
>>> agent.train()
>>> agent.best

注意

最佳模型是评估返回最高的模型。如果调用此方法,当前模型将被最佳模型替换。

collect_data(env_num: int = 8, save_data_path: str | None = None, n_sample: int | None = None, n_episode: int | None = None, context: str | None = None, debug: bool = False) None[来源]
Overview:

使用TD3算法收集数据,共进行n_episode个回合,使用env_num个收集器环境。如果指定了save_data_path,则收集的数据将保存在该路径下,否则将保存在exp_name/demo_data中。

Arguments:
  • env_num (int): 收集器环境的数量。默认为8。

  • save_data_path (str): 保存收集数据的路径。默认为 None。如果未指定,数据将保存在 exp_name/demo_data 中。

  • n_sample (int): 要收集的样本数量。默认为 None。如果未指定,则必须指定 n_episode

  • n_episode (int): 要收集的剧集数量。默认为 None。如果未指定,则必须指定 n_sample

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

deploy(enable_save_replay: bool = False, concatenate_all_replay: bool = False, replay_save_path: str | None = None, seed: int | List | None = None, debug: bool = False) EvalReturn[source]
Overview:

通过与环境的交互使用TD3算法部署代理,在此期间如果enable_save_replay为True,则可以保存回放视频。评估结果将被返回。

Arguments:
  • enable_save_replay (bool): 是否保存回放视频。默认为 False。

  • concatenate_all_replay (bool): 是否将所有回放视频合并为一个视频。默认值为 False。如果 enable_save_replay 为 False,此参数将被忽略。如果 enable_save_replay 为 True 且 concatenate_all_replay 为 False,每个回合的回放视频将分别保存。

  • replay_save_path (str): 保存回放视频的路径。默认为 None。如果未指定,视频将保存在 exp_name/videos 中。

  • seed (Union[int, List]): 随机种子,在运行程序之前设置。默认为 None。如果未指定,将使用 self.seed。如果 seed 是一个整数,代理将部署一次。如果 seed 是一个整数列表,代理将为列表中的每个种子部署一次。

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

Returns:
  • (EvalReturn): The evaluation result, of which the attributions are:
    • eval_value (np.float32): 评估返回的平均值。

    • eval_value_std (np.float32): 评估回报的标准差。

train(step: int = 10000000, collector_env_num: int | None = None, evaluator_env_num: int | None = None, n_iter_save_ckpt: int = 1000, context: str | None = None, debug: bool = False, wandb_sweep: bool = False) TrainingReturn[source]
Overview:

使用TD3算法训练代理,进行step次迭代,使用collector_env_num个收集器环境和evaluator_env_num个评估器环境。训练期间的信息将由wandb记录并保存。

Arguments:
  • 步骤 (int): 所有收集器环境的总训练环境步骤。默认为1e7。

  • collector_env_num (int): 收集器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • evaluator_env_num (int): 评估器环境编号。默认为 None。如果未指定,将根据配置进行设置。

  • n_iter_save_ckpt (int): 每次训练迭代保存检查点的频率。默认值为1000。

  • context (str): 环境管理器的多进程上下文。默认为 None。它可以指定为 spawn, forkforkserver

  • debug (bool): 是否在环境管理器中使用调试模式。默认为 False。如果设置为 True,将使用基础环境管理器以便于调试。否则,将使用子进程环境管理器。

  • wandb_sweep (bool): 是否使用wandb sweep,这是一个用于寻找最佳配置的超参数优化过程。默认为False。如果为True,wandb sweep的id将用作实验名称。

Returns:
  • (TrainingReturn): The training result, of which the attributions are:
    • wandb_url (str): 训练实验的权重和偏差(wandb)项目URL。