信誉好的天津网站建设,淘客单网站,wordpress中设置ajax分页,龙岩做网站设计公司前面的博客介绍了ray.rllib中算法的配置和构建#xff0c;也包含了算法训练的代码。 但是rllib中实现算法训练的方式不止一种#xff0c;本博客对此进行介绍。很多教程使用 PPOTrainer 进行训练#xff0c;但是 PPOTrainer 在最近的 ray 版本中已经取消了。 环境配置#x… 前面的博客介绍了ray.rllib中算法的配置和构建也包含了算法训练的代码。 但是rllib中实现算法训练的方式不止一种本博客对此进行介绍。很多教程使用 PPOTrainer 进行训练但是 PPOTrainer 在最近的 ray 版本中已经取消了。 环境配置 torch2.5.1 ray2.10.0 ray[rllib]2.10.0 ray[tune]2.10.0 ray[serve]2.10.0 numpy1.23.0 python3.9.18 方式1 algo.train() rllib 中的 Algorithm 类自带了.train() 函数实现算法训练前面几个博客教程均是采用的这种方式。这里仅再提供一下示例 不再赘述
import os
from ray.rllib.algorithms.ppo import PPO,PPOConfig
from ray.tune.logger import pretty_print## 配置算法
storage_path F:/codes/RLlib_study/ray_results/build_method_3
config PPOConfig()
config config.rollouts(num_rollout_workers2)
config config.resources(num_gpus0,num_cpus_per_worker1,num_gpus_per_worker0)
config config.environment(envCartPole-v1,env_config{})
config.output storage_path ## 设置过程文件的存储路径## 构建算法
algo config.build()## 训练
for i in range(3):result algo.train() print(fepisode_{i})
方式2tune.Tuner() 以上方式只能固定训练超参数不能对训练超参数寻优。ray中还有一个模块 tune, 专门用于算法训练过程中超参数调参。 在使用tune.Tuner()执行rllib算法训练时 可以默认为tune背后自动执行了以下操作 algo PPOConfig().build() ## 构建算法 result algo.train() ## 算法训练 print(pretty_print(result)) ## 每完成一次algo.train 打印一次阶段性训练结果 algo.save_checkpoint() ## 保存训练模型 并且遍历了多个超参数组合多次进行训练。直到达到停止训练的条件自己配置。 基于tune的rllib训练示例如下(代码篇幅比较大是因为添加的功能模块和注释比较多后面的介绍主要以 方式一为主所以对于这种方式这里介绍的多一些)
import ray
from ray.rllib.algorithms.ppo import PPO,PPOConfig
from ray import train, tune
import torch
import os
import shutil
from ray.tune.logger import pretty_print
import gymnasium as gym ray.init()#### 配置算法 ####
config PPOConfig()
config config.training(lrtune.grid_search([0.01, 0.001]))
config config.environment(envCartPole-v1)#### 配置 tune ###### 准备 tune 的 stop_condition多个条件之间是”或“的关系。有一个满足即停止训练。
## episode_reward_mean关键字将在 ”ray-2.40”版中中被抛弃
## 届时需要用 env_runners/episode_return_mean 替代 episode_reward_mean
stop_condition {episode_reward_mean:10, ## 这里设置的结束条件很宽松所以能够快速结束训练。training_iteration:3}## 准备 tune 的过程文件存储路径
storage_path F:/codes/RLlib_study/ray_results
os.makedirs(storage_path, exist_okTrue)## 准备 tune 的 checkpoint_config
## tune 默认保存每个 algo.train() 训练得到的 checkpoint.
## 通过以下配置可以对此进行自定义修改
checkpoint_config train.CheckpointConfig(num_to_keepNone, ## 保存几个checkpoint, None 表示保存所有checkpointcheckpoint_at_endTrue) ## 是否在训练结束后保存 checkpoint. ## 配置 tuner
tuner tune.Tuner(PPO, ## 需要是一个 rllib 的 Algorithm 类 从 ray.rllib.algorithms 导入 也可以是自定义的后面介绍 run_config train.RunConfig(stop stop_condition, checkpoint_config checkpoint_config, ## 用于设置保存哪个checkpoint. storage_path storage_path, ## 如果不设置 默认存储路径是 “~/ray-results” 或 “C:/用户/xxx/ray_results”),param_spaceconfig, ## 这里定义了参数搜索调优空间是一个 PPOConfig 对象
)## 执行训练
results tuner.fit() ## tuner 返回一个Result表格对象该对象允许进一步分析训练结果并检索经过训练的智能体的checkpoint。
print(训练结束)## 获取最佳训练结果
best_result results.get_best_result(metricepisode_reward_mean, modemax)
## 以 episode_reward_mean 为选择指标 从results里面选择checkpoint, 选择模式是“max”
## episode_reward_mean关键字将在ray-2.40版中中被抛弃届时需要用 env_runners/episode_return_mean 替代 episode_reward_mean## 从最佳训练结果中提取对应的 checkpoint 并保存
checkpoint_save_dir F:/codes/RLlib_study/ray_results/best_checkpoints
os.makedirs(checkpoint_save_dir, exist_okTrue)best_checkpoint best_result.checkpoint
if best_checkpoint:with best_checkpoint.as_directory() as checkpoint_dir:print(f最佳模型路径位于:{checkpoint_dir})## 把最佳模型转存到指定位置。 shutil.rmtree(checkpoint_save_dir)shutil.copytree(checkpoint_dir,checkpoint_save_dir)print(f保存最佳模型到:{checkpoint_save_dir})## 加载保存的最佳模型
checkpoint_dir F:/codes/RLlib_study/ray_results/best_checkpoints
algo PPO.from_checkpoint(checkpoint_dir)
print(f 加载最佳模型 {checkpoint_dir})## evaluate 模型
env_name CartPole-v1
env gym.make(env_name)## 模型推断: method-1
step 0
episode_reward 0
terminated truncated False obs,info env.reset()
while not terminated and not truncated:action algo.compute_single_action(obs)obs, reward, terminated, truncated, info env.step(action)episode_reward rewardstep 1print(fstep {step}, reward {reward}, action {action}, obs {obs}, episode_reward {episode_reward})
文章转载自: http://www.morning.hqwtm.cn.gov.cn.hqwtm.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.gnkbf.cn.gov.cn.gnkbf.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.qwhbk.cn.gov.cn.qwhbk.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.pqhgn.cn.gov.cn.pqhgn.cn http://www.morning.wqrdx.cn.gov.cn.wqrdx.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.yrhsg.cn.gov.cn.yrhsg.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.lczxm.cn.gov.cn.lczxm.cn http://www.morning.dnls.cn.gov.cn.dnls.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.weiwt.com.gov.cn.weiwt.com http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.mxcgf.cn.gov.cn.mxcgf.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.dyxlj.cn.gov.cn.dyxlj.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.zbnkt.cn.gov.cn.zbnkt.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn