强化学习-08--PPO

一、ppo伪代码

在这里插入图片描述
在这里插入图片描述

二、ppo算法整个过程的理解

适用于连续动作空间的一种算法!

(1)首先,存储(s、a、r),计算折扣奖励以及优势函数

buffer_s.append(s) buffer_a.append(a) buffer_r.append®,即可将强化学习的经验存储起来,当存储量达到一个batch后,计算buffer里面的折扣奖励及优势函数,然后将buffer_s、buffer_a、buffer_r清零置空。下一步将更新actor网络和critic网络。

折扣奖励公式为:

R t = ∑ t ′ > t t ′ = n − 1 γ t ′ − t r t ′ + γ n − t V ϕ ( s n ) R_{t}=\sum_{t^{'}>t}^{t^{'}=n-1}{\gamma^{t^{'}-t}r_{t^{'}}}+\gamma^{n-t}V_{\phi}(s_{n}) Rt=t>tt=n1γttrt+γntVϕ(sn)
= r t + γ ∗ r t + 1 + γ 2 ∗ r t + 2 + . . . + γ n − t − 1 ∗ r n − 1 + γ n − t ∗ V ϕ ( s n ) =r_{t}+\gamma*r_{t+1}+\gamma^{2}*r_{t+2}+...+\gamma^{n-t-1}*r_{n-1}+\gamma^{n-t}*V_{\phi}(s_{n}) =rt+γrt+1+γ2rt+2+...+γnt1rn1+γntVϕ(sn)
其中 V ϕ ( s n ) V_{\phi}(s_{n}) Vϕ(sn)通过critic网络得到,输入状态 sn 得到值函数 V ϕ ( s t ) V_{\phi}(s_{t}) Vϕ(st)

优势函数为:
A t = R t − V ϕ ( s t ) A_{t}=R_{t}-V_{\phi}(s_{t}) At=RtVϕ(st)
其中 V ϕ ( s t ) V_{\phi}(s_{t}) Vϕ(st)通过critic网络得到,输入状态 st 得到值函数 V ϕ ( s t ) V_{\phi}(s_{t}) Vϕ(st)

(2)更新actor网络
actor网络有一个新actor网路和旧actor网络,其中新actor网络在更新的时候是一直都在更新,而旧actor网络是在新actor网络更新一定次数后,由新actor网络的参数赋值。

新actor网络更新:(有两种更新方式)

1)一种为: a l o s s = − ∑ t − 1 T π n e w ( a t ∣ s t ) π o l d ( a t ∣ s t ) A t − λ ∗ K L ( π o l d ∣ π n e w ) aloss=-\sum_{t-1}^{T}{\frac{\pi_{new}(a_{t}|s_{t})}{\pi_{old}(a_{t}|s_{t})}A_{t}}-\lambda*KL(\pi_{old}|\pi_{new}) aloss=t1Tπold(atst)πnew(atst)AtλKL(πoldπnew)
K L ( π o l d ∣ π n e w ) > 4 K L t e a r g e t KL(\pi_{old}|\pi_{new})>4KL_{tearget} KL(πoldπnew)>4KLtearget时,break,退出更新循环,进入下一次迭代episode。

2)第二种为: a l o s s = − ∑ a b m i n ( π n e w ( a t ∣ s t ) π o l d ( a t ∣ s t ) A t , c l i p ( π n e w ( a t ∣ s t ) π o l d ( a t ∣ s t ) , 1 − ϵ , 1 + ϵ ) A t ) aloss=-\sum_{a}^{b}{min( \frac{\pi_{new}(a_{t}|s_{t})}{\pi_{old}(a_{t}|s_{t})}A_{t},clip(\frac{\pi_{new}(a_{t}|s_{t})}{\pi_{old}(a_{t}|s_{t})} ,1-\epsilon,1+\epsilon)A_{t} }) aloss=abmin(πold(atst)πnew(atst)At,clip(πold(atst)πnew(atst),1ϵ,1+ϵ)At)
根据得到的损失函数,进行梯度更新(进行训练,使得损失函数越来越小)。

(3)更新critic网络
c l o s s = ∑ t = 1 T [ R t − V ϕ ( s t ) ] 2 closs=\sum_{t=1}^{T}[R_{t}-V_{\phi}(s_{t})]^2 closs=t=1T[RtVϕ(st)]2
根据损失函数,进行梯度更新

(4)要根据不同的KL散度的范围对参数 λ 进行更改:
K L ( π o l d ∣ π n e w ) > β h i g h K L t a r g e t 时, λ = α ∗ λ ( α > 1 ) KL(\pi_{old}|\pi_{new})>\beta_{high}KL_{target}时,\lambda=\alpha*\lambda(\alpha>1) KL(πoldπnew)>βhighKLtarget时,λ=αλ(α>1)
K L ( π o l d ∣ π n e w ) < β l o w K L t a r g e t 时, λ = λ / α ( α > 1 ) KL(\pi_{old}|\pi_{new})<\beta_{low}KL_{target}时,\lambda=\lambda/\alpha(\alpha>1) KL(πoldπnew)<βlowKLtarget时,λ=λ/α(α>1)

这就是一次episode的全部运行过程,结束后进入到下一个episode,继续训练。

三、on-policy与off-policy

on-policy:我们想要训练的agent与环境进行交互的agent是同一个agent。

off-policy:想要训练的agent与环境进行交互的不是同一个agent。

(李宏毅老师的DRL课程讲的是off-policy,论文里写的都是on-policy)
在强化学习中,为什么TRPO和PPO算法属于On-Policy的算法?

在这里插入图片描述
从策略 θ′ 中训练,将数据保存在buffer中,从buffer中抽取随机抽取数据更新策略 θ ,与环境交互的agent与学习更新的agent不是同一个,故认为是off-policy。

policy gradient:on-policy。

四、细节

ppo使用了important sampling方法,p(x)与q(x)应该相差不大。

五、ppo代码

代码

六、与A3C区别

A3C将Gradient 给Global net

ppo将采集到的数据给Global ppo

参考资料

代码
在强化学习中,为什么TRPO和PPO算法属于On-Policy的算法?

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值