强化学习_06_pytorch-PPO实践(Pendulum-v1)

【深度强化学习】(6) PPO 模型解析,附Pytorch完整代码 近端策略优化算法PPO(proximal policy optimization),具备 Policy Gradient、TRPO 的部分优点,采样数据和使用随机梯度上升方法优化代替目标函数之间交替进行,但 PPO 提出新目标函数,可以实现小批量更新。PPO 算法可依据 Actor 网络的更新方式细化为含有自适应 KL-散度(KL Penalty)的 PPO-Penalty 和含有 Clippped Surrogate Objective 函数的 PPO-Clip。 阅读详情
AI摘要

本文介绍了PPO算法,它是TRPO的一种更简单的实现方式,主要讨论了PPO的两种形式——PPO-惩罚和PPO-截断,重点讲述了PPO-Clip的原理,即通过目标函数的限制保持新旧策略的稳定性。此外,还提供了PPO在PyTorch中的实践代码,展示了如何构建策略网络并在倒立钟摆问题中应用PPO-Clip算法。,

一、PPO简介

TRPO(Trust Range Policy Optimate)算法每一步更新都需要大量的运算,于是便有其改进版本PPO在2017年被提出。PPO 基于 TRPO 的思想,但是其算法实现更加简单。TRPO 使用泰勒展开近似、共轭梯度、线性搜索等方法直接求解。PPO 的优化目标与 TRPO 相同,但 PPO 用了一些相对简单的方法来求解。具体来说, PPO 有两种形式,一是PPO-惩罚,二是PPO-截断,我们接下来对这两种形式进行介绍。

二、PPO两种形式

2.1 PPO-Penalty

用拉格朗日乘数法直接将KL散度的限制放入目标函数,变成一个无约束的优化问题。同时还需要更新KL散度的系数。
a r g m a x θ E a − v π θ k E a − π θ k ( ⋅ ∣ s ) [ π θ ( a ∣ s ) π θ k ( a ∣ s ) A π θ k ( s , a ) − β D K L [ π θ k ( ⋅ ∣ s ) , π θ ( ⋅ ∣ s ) ] ] arg max_{\theta} E_{a- v^{\pi_{\theta_k}}}E_{a-\pi_{\theta_k}}( \cdot|s)[\frac{\pi_\theta(a|s)}{\pi_{\theta_k}(a|s)}A^{\pi_{\theta_k}}(s, a) - \beta D_{KL}[\pi_{\theta_k}(\cdot|s), \pi_{\theta}(\cdot|s)]] argmaxθ​Ea−vπθk​​​Ea−πθk​​​(⋅∣s)[πθk​​(a∣s)πθ​(a∣s)​Aπθk​​(s,a)−βDKL​[πθk​​(⋅∣s),πθ​(⋅∣s)]]
令 d k = D K L v π θ k [ π θ k ( ⋅ ∣ s ) , π θ ( ⋅ ∣ s ) ] d_k=D^{v^{\pi_{\theta_k}}}_{KL}[\pi_{\theta_k}(\cdot|s), \pi_{\theta}(\cdot|s)] dk​=DKLvπθk​​​[πθk​​(⋅∣s),πθ​(⋅∣s)]

  1. 如果 d k < δ / 1.5 d_k < \delta /1.5 dk​<δ/1.5, 那么 β k + 1 = β k / 2 \beta_{k+1} = \beta_k/2 βk+1​=βk​/2
  2. 如果
1. 倒立摆: Pendulum-v1 是一个经典的强化学习环境,属于 OpenAI Gym 库的一部分。这个环境模拟了一个倒立摆的问题,目标是通过施加适当的扭矩使摆杆保持在竖直向上的位置。:控制摆杆使其保持在竖直向上的位置(即角度为 0 弧度)。theta[-π, π]theta_dot[-8, 8]cos(theta)sin(theta)[-2, 2]r−θ20.1⋅θdot2​0.001⋅torque2θθdot​torque最小的奖励是,当摆杆处于竖直向下位置(θπ),角速度最大(θdot​8。 阅读详情

相关推荐

gym-Pendulum v1 + DDPG(PyTorch)

pendulum

weixin_42108775的博客 5548

PPO2代码 pytorch框架

PPO2代码pytorch框架玩gym库的Pendulum环境。

fangchenglia的博客 8687

PyTorch实战:用PPO算法训练Pendulum-v1环境(附完整代码与调参技巧)

本文详细介绍了如何使用PyTorch实现PPO算法训练Pendulum-v1环境,包括网络架构设计、核心算法实现及调参技巧。通过完整的代码示例和工业级优化建议,帮助开发者快速掌握强化学习在连续控制任务中的应用,提升训练效率和模型性能。

weixin_33712987的博客 363

强化学习新手上路:用PPO-Clip轻松玩转Pendulum-v1(PyTorch保姆级教程)

本文提供了一份基于PyTorch的PPO-Clip算法实战教程,指导强化学习新手在Pendulum-v1环境中构建连续控制智能体。教程详细解析了PPO-Clip的核心机制,特别是其Clip机制如何限制策略更新幅度以确保训练稳定,并涵盖了从环境配置、策略网络设计、经验收集到完整训练框架搭建的全过程,是掌握连续动作空间强化学习的实用指南。

weixin_29062963的博客 274

从代码学习深度强化学习 - PPO PyTorch版

欢迎来到深度强化学习(DRL)的世界!在众多 DRL 算法中,Proximal Policy Optimization (PPO) 无疑是最受欢迎和广泛应用的算法之一。它由 OpenAI 在 2017 年提出,以其出色的性能、相对简单的实现和稳定的训练过程而著称,成为了许多研究和应用的基准算法。本篇博客旨在通过一个完整的 PyTorch 实现,带您从代码层面深入理解 PPO 算法。我们将不仅仅是看公式,更是要“动手”,一步步构建、训练和分析 PPO 智能体。为了全面掌握其应用,我们将分别在经典的。

weixin_43887510的博客 904

避坑指南:在Pendulum-v0上调试PPO时,我踩过的那些‘雷’(PyTorch实战)

本文分享了在Pendulum-v0环境中调试PPO算法时的实战经验与避坑指南。详细解析了连续动作空间的策略网络设计、GAE参数敏感性、Clip机制监控等关键问题,并提供了PyTorch实现中的优化技巧和工程细节,帮助开发者高效解决训练不稳定问题。

weixin_30329623的博客 453

从理论到实战:基于PyTorch的PPO算法在Gym连续控制任务Pendulum-v0中的调优与部署

本文详细介绍了基于PyTorch的PPO算法在Gym连续控制任务Pendulum-v0中的调优与部署实践。通过分析PPO算法的核心思想、网络架构设计、超参数调优及训练技巧,帮助开发者高效解决连续动作空间控制问题,提升强化学习模型的性能与稳定性。

weixin_33738555的博客 311

从理论到实践:使用PyTorch与PPO算法攻克Gym连续控制难题Pendulum-v0

本文详细介绍了如何使用PyTorch和PPO算法解决Gym中的连续控制问题Pendulum-v0。通过Actor-Critic架构设计、动作采样技巧和PPO-Clip核心实现,帮助开发者掌握强化学习在连续控制任务中的应用。文章还分享了超参数调优和训练监控的实战经验,最终实现摆杆稳定直立的控制效果。

weixin_29204121的博客 197

Pendulum

AngryBirdsStage2.5 《愤怒的小鸟》第2.5阶段具有类继承和图像

Vue3 视频播放器完整指南 – @videojs-player/vue 从入门到精通

全面解析 Vue3 生态中最强大的视频播放解决方案 @videojs-player/vue。从基础安装到高级功能,涵盖自动播放、直播流、字幕支持等核心特性。提供完整的代码示例和最佳实践,助你快速构建专业的视频播放应用。

前端开发-前端技术分享 1430

从开源到落地:SimpleBGC 三轴稳像平台全栈技术解析(上)

SimpleBGC是一款开源的稳定云台控制系统,相比专业设备具有显著的价格优势(仅为1/5-1/10),采用模块化设计支持全场景适配。系统包含硬件设计、软件代码和算法三大核心:硬件采用STM32主控和IMU传感器,支持6步换向和FOC电机驱动;软件采用三层架构(驱动层、算法层、应用层),实现姿态解算和PID控制;算法方面提供互补滤波(基础版)和卡尔曼滤波(高端版),可达到±0.5°至±0.1°的稳定精度。该系统适用于无人机航拍、工业检测等多种应用场景,特别适合DIY开发者和中小企业进行定制化开发。

bother3000的博客 3697

让AI镜头“活”起来:22个核心运镜提示词,告别视频画面呆板

介绍了22种影视级镜头语言的AI提示词用法。内容涵盖推轨/后拉镜头等距离变化技巧、横摇/俯仰等方向控制手法、环绕/升降等空间位移方式,以及POV视角等特殊构图。

weixin_43489601的博客 6299

2025海外代理IP深度横评:Bright Data、IPFoxy、SmartProxy等六大服务商实战对比

本文深度横评2025年六大海外代理IP服务商(Bright Data、IPFoxy、SmartProxy、IPIPGO、Kookeey及青果网络),基于实战测试数据,从IP池质量、速度稳定性、性价比等维度进行对比。针对跨境电商、数据爬虫、社交媒体管理等不同业务场景,提供精准的选购指南与避坑建议,帮助企业用户根据自身需求选择最合适的代理IP解决方案。

weixin_29196891的博客 440

瑞萨单片机-硬件I2C从设备

瑞萨单片机上实现I2C从机,根据主机发来的数据,从机返回或者保存数据2、R5F1115AxFL最多支持3个从设备地址(即可以作为三个从设备)3、从接收模式:直接将数据保存到RAM中,接收完再处理4、从发送模式:按照I2C读取传感器数据经验,主机读取从设备时,从设备需要立刻返回数据的,但是程序要如何实现一收到指令就返回特定的数据?(时钟拉伸)这里先跳过,我们先实现返回固定的数据。

qq_27893147的博客 3220

交通流预测爬坑记(一):交通流数据集,原始数据

常用的交通流数据集,持续更新~~

K_first的博客 4万+

无人机——电机篇(一)

1. 电机的定义 电机俗称“马达”,是无人机的动力来源,无人机通过改变电机的转速来改变无人机的飞行状态。即改变每个电机的速度,使得无人机能够盘旋空中,上升或下降,或向各个方向移动。2. 电机的分类 电机分为:“无刷电机”和“有刷电机” - 无刷电机:是采用半导体开关器件来实现电子换向,具有可靠性高、低干扰、低噪声低、寿命长等优点。一般应用在稍大型的飞行器,载重大,可以有更广泛的用途。 - 有刷电机:是内含电刷装置的将电能转换成机械能(电动机)或将机械能转换成电能(发电机)的旋转电机。有刷电机是所有电机的基

weixin_46066938的博客 1万+

DXF转G代码是圆弧的方向如何确定?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1192

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

上一篇: 强化学习_06_pytorch-doubleDQN实践(Pendulum-v1)
下一篇: 强化学习_06_pytorch-DDPG实践(Pendulum-v1)
Scc_hy
博客等级 码龄9年 432粉丝 176原创
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Scc_hy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址