当前位置: 首页 > news >正文

CPT 强化学习完整实现(PyTorch 版 - Actor-Critic + CPT)

✅ CPT 强化学习完整实现(PyTorch 版 - Actor-Critic + CPT)

以下是生产级友好的实现,适合连续/离散控制任务,结合Cumulative Prospect Theory修改优势函数(Advantage)。

推荐配置(默认使用)

  • 环境LunarLander-v2(连续动作空间,难度适中,能明显看出 CPT 的风险偏好差异)
  • 框架PyTorch+ Gymnasium
  • 算法:Actor-Critic(A2C风格) + CPT 价值重塑
  • 深度:带经验回放 + 参考点自适应

完整代码(可直接运行)

importgymnasiumasgymimporttorchimporttorch.nnasnnimporttorch.optimasoptimimportnumpyasnpfromcollectionsimportdequeimportrandom# ====================== CPT 核心函数 ======================classCPT:def__init__(self,alpha=0.88,beta=0.88,lambda_loss=2.25,gamma_gain=0.61,gamma_loss=0.69,reference=0.0):self.alpha=alpha self.beta=beta self.lambda_loss=lambda_loss self.gamma_gain=gamma_gain self.gamma_loss=gamma_loss self.reference=referencedefvalue(self,x):"""价值函数 v(x)"""x=torch.tensor(x,dtype=torch.float32)returntorch.where(x>=0,x**self.alpha,-self.lambda_loss*(-x)**self.beta)defprobability_weight(self,p):"""概率权重函数"""p=torch.tensor(p,dtype=torch.float32).clamp(1e-6,1-1e-6)w_gain=p**self.gamma_gain/(p**self.gamma_gain+(1-p)**self.gamma_gain)**(1/self.gamma_gain)w_loss=p**self.gamma_loss/(p**self.gamma_loss+(1-p)**self.gamma_loss)**(1/self.gamma_loss)returntorch.where(p>=0.5,w_gain,w_loss)# 简化defcompute_cpt_advantage(self,rewards,gamma=0.99):"""从 trajectory 计算 CPT 优势"""returns=[]R=0forrinreversed(rewards):R=r+gamma*R returns.insert(0,R)returns=torch.tensor(returns)cpt_returns=self.value(returns-self.reference)# 标准化cpt_advantages=(cpt_returns-cpt_returns.mean())/(cpt_returns.std()+1e-8)returncpt_advantages# ====================== Actor-Critic 网络 ======================classActorCritic(nn.Module):def__init__(self,state_dim,action_dim,hidden_dim=128):super().__init__()self.shared=nn.Sequential(nn.Linear(state_dim,hidden_dim),nn.ReLU(),nn.Linear(hidden_dim,hidden_dim),nn.ReLU())# Actor (均值 + 标准差)self.actor_mean=nn.Linear(hidden_dim,action_dim)self.actor_logstd=nn.Parameter(torch.zeros(action_dim))# Criticself.critic=nn.Linear(hidden_dim,1)defforward(self,x):x=self.shared(x)mean=self.actor_mean(x)std=torch.exp(self.actor_logstd)value=self.critic(x)returnmean,std,value# ====================== CPT Agent ======================classCPTActorCriticAgent:def__init__(self,state_dim,action_dim,lr=1e-3,gamma=0.99,device='cpu'):self.device=device self.gamma=gamma self.cpt=CPT()self.model=ActorCritic(state_dim,action_dim).to(device)self.optimizer=optim.Adam(self.model.parameters(),lr=lr)self.memory=deque(maxlen=2048)# (state, action, reward, next_state, done)defselect_action(self,state):state=torch.FloatTensor(state).unsqueeze(0).to(self.device)mean,std,_=self.model(state)dist=torch.distributions.Normal(mean,std)action=dist.sample()action=torch.clamp(action,-1.0,1.0)# LunarLander 动作范围returnaction.squeeze().cpu().numpy()defupdate(self,batch_size=128):iflen(self.memory)<batch_size:return0.0batch=random.sample(self.memory,batch_size)states,actions,rewards,next_states,dones=zip(*batch)states=torch.FloatTensor(np.array(states)).to(self.device)actions=torch.FloatTensor(np.array(actions)).to(self.device)rewards=torch.FloatTensor(rewards).to(self.device)next_states=torch.FloatTensor(np.array(next_states)).to(self.device)dones=torch.FloatTensor(dones).to(self.device)# CPT Advantageadvantages=self.cpt.compute_cpt_advantage(rewards.tolist())# Forwardmeans,stds,values=self.model(states)dist=torch.distributions.Normal(means,stds.exp())log_probs=dist.log_prob(actions).sum(dim=-1)# Lossactor_loss=-(log_probs*advantages.detach()).mean()critic_loss=(values.squeeze()-advantages).pow(2).mean()loss=actor_loss+0.5*critic_loss self.optimizer.zero_grad()loss.backward()torch.nn.utils.clip_grad_norm_(self.model.parameters(),0.5)self.optimizer.step()returnloss.item()defstore(self,state,action,reward,next_state,done):self.memory.append((state,action,reward,next_state,done))# ====================== 训练主函数 ======================deftrain_cpt_rl(episodes=800,render=False):env=gym.make("LunarLanderContinuous-v2",render_mode="human"ifrenderelseNone)state_dim=env.observation_space.shape[0]# 8action_dim=env.action_space.shape[0]# 2agent=CPTActorCriticAgent(state_dim,action_dim,device='cuda'iftorch.cuda.is_available()else'cpu')best_reward=-float('inf')forepisodeinrange(episodes):state,_=env.reset()episode_rewards=[]done=Falsetotal_reward=0whilenotdone:action=agent.select_action(state)next_state,reward,terminated,truncated,_=env.step(action)done=terminatedortruncated agent.store(state,action,reward,next_state,done)state=next_state total_reward+=reward episode_rewards.append(reward)iflen(agent.memory)>256:loss=agent.update()# 自适应参照点(可选)iftotal_reward>best_reward:best_reward=total_reward agent.cpt.reference=np.mean(episode_rewards)*0.3ifepisode%50==0:print(f"Episode{episode:4d}| Reward:{total_reward:8.2f}| "f"Best:{best_reward:.2f}| Ref Point:{agent.cpt.reference:.2f}")env.close()returnagentif__name__=="__main__":agent=train_cpt_rl(episodes=1000)

使用说明

  1. 安装依赖
pipinstallgymnasium[box2d]torch numpy
  1. 运行:直接执行即可看到 LunarLander 训练过程。

  2. CPT 关键影响

    • 损失厌恶(λ=2.25)→ Agent 更倾向于避免大风险坠毁,即使牺牲部分期望得分。
    • 可通过调整lambda_loss观察行为变化(越大越保守)。

需要我调整吗?

  • 切换到PPO版本(更稳定)
  • 使用Discrete 环境(CartPole / FrozenLake)
  • 增加经验回放 + Prioritized Replay
  • 多环境并行训练(VectorEnv)
  • CPT 在 DQN / SAC 中的实现
http://www.jsqmd.com/news/879886/

相关文章:

  • 2026年装修季必看!专业明装暖气怎么选的实用攻略来了 - 资讯纵览
  • 从救援受阻事故案例,看无感定位技术普及的迫切意义
  • m4s-converter终极指南:3步解锁B站缓存视频的离线观看自由
  • 如何免费解锁Wand专业版功能:Wand-Enhancer完整使用指南
  • 六音音源修复版:三步解决洛雪音乐播放失效问题
  • DeepSeek计费策略终极对比:RPM限制、上下文长度溢价、多模态附加费,一文讲透
  • 在Windows 10上从零开始:手把手教你安装和运行TELEMAC-MASCARET V8P4水动力模型
  • BooruDatasetTagManager:如何用AI智能标注工具将图像数据集处理效率提升10倍
  • Claude Code用户如何通过Taotoken解决API不稳定与Token不足问题
  • 2026 北京包包回收实测:上门回收估价 vs 线下实体店,哪个更划算 - 奢侈品回收测评
  • 对比直接使用原厂API,Taotoken在计费透明性上给我们的感受
  • 等保2.0三级Linux服务器合规基线重建实战指南
  • 终极指南:让老旧Mac免费升级最新macOS系统的完整方案
  • 新沂沙发翻新换皮换布面靠谱商家优选推荐|匠阁沙发翻新、御匠沙发翻新、锦修沙发翻新三大品牌、全品类沙发翻新换皮换布一站式服务 - 卓一科技
  • 纯视觉破界空间感知 自研体系领跑视频孪生领域
  • 5分钟搞定Sunshine游戏串流:从安装到畅玩的完整指南
  • 2026年5月有实力的电磁阀厂家推荐钢特阀门科技有限公司,优化产品结构提升流体调控效能 - 品牌鉴赏师
  • Python Anaconda,为什么要创建虚拟环境,Pycharm使用
  • 因果推断与双机器学习在LED制造返工决策中的实战应用
  • Gemini企业社会责任实践白皮书(2024独家解密版):覆盖AI伦理、碳足迹追踪与社区赋能的3层合规架构
  • 夏季前挡膜怎么选?固驰蓝闪幻蝶车窗膜给出不止隔热的答案
  • 3分钟快速找回Navicat数据库连接密码:开源解密工具完整教程
  • DeepSeek模型版本选择实战手册(2024最新版):从推理延迟、显存占用到LoRA兼容性全拆解
  • 抖音无水印视频解析工具:3分钟搭建你的个人视频素材库
  • 量子机器学习模型鲁棒性验证:VeriQR工具原理与应用实战
  • AI构建的Python学习路线
  • 3个场景告诉你:为什么你需要PowerToys Text Extractor
  • 告别笔记本续航焦虑:手把手教你用NVMe电源管理给SSD“降频省电”
  • 3分钟掌握Heightmapper:免费创建专业3D地形高度图的终极指南
  • StraightLine调度器:异构资源下的机器学习模型智能部署实践