深度强化学习(DRL)算法 2 —— PPO 之 Clipped Surrogate Objective 篇

本文介绍了深度强化学习中的PPO算法,它解决了REINFORCE算法中策略分离的问题,通过ClippedSurrogateObjective和CreditAssignment实现off-policy学习,并降低了Monte-Carlo评估带来的大方差。
摘要由CSDN通过智能技术生成

在之前的文章里介绍了 深度强化学习(DRL)算法 1 —— REINFORCE,提出了两个缺点,其中缺点一,在后续提出的 DRL 算法 PPO 解决了,今天我们就来看看大名鼎鼎的 PPO 算法。

回顾

在 REINFORCE 算法里,用于产生 τ \tau τ 的策略和用来学习的策略(on-policy)是同一个,导致 τ \tau τ 不能复用,使得训练效率很低。那么直觉上,是不是把这两个策略分开来,一个用来产生 τ \tau τ ,一个用来学习(off-policy),是不是就可以了呢?答案是,对的!PPO 也是这么做的,接下来,我们来看看算法描述(建议先阅读 深度强化学习(DRL)算法 1 —— REINFORCE,文章接下来用文1指代这篇文章)。

算法描述

根据文1,最大期望回报表达如下:

R ˉ θ = E τ ∼ p θ ( τ ) [ R ( τ ) ] = ∑ τ p θ ( τ ) R ( τ ) \bar{R}_{\theta} = E_{\tau\sim p_{\theta}(\tau)}[R(\tau)] = \sum_{\tau}p_{\theta}(\tau)R(\tau) Rˉθ=Eτpθ(τ)[R(τ)]=τpθ(τ)R(τ)

现在我们希望有一个新的策略 q,用来产生 τ \tau τ ,原有策略 p 用来学习,那么我们可以对 R ˉ θ \bar{R}_{\theta} Rˉθ 做如下修改:

R ˉ θ = ∑ τ q θ ′ ( τ ) p θ ( τ ) q θ ′ ( τ ) R ( τ ) = E τ ∼ q θ ′ ( τ ) [ p θ ( τ ) q θ ′ ( τ ) R ( τ ) ] \bar{R}_{\theta} = \sum_{\tau}q_{\theta'}(\tau)\frac{p_{\theta}(\tau)}{q_{\theta'}(\tau)}R(\tau) = E_{\tau\sim q_{\theta'}(\tau)}[\frac{p_{\theta}(\tau)}{q_{\theta'}(\tau)}R(\tau)] Rˉθ=τqθ(τ)qθ(τ)pθ(τ)R(τ)=Eτqθ(τ)[qθ(τ)pθ(τ)R(τ)]
(Importance Sampling)
为了写起来方便,我们用 g 来表示文1里的
∇ R ˉ ( θ ) \nabla \bar{R}(\theta) Rˉ(θ)
,那么新的 g 如下:

g = ∇ θ ∑ τ q θ ′ ( τ ) p θ ( τ ) q θ ′ ( τ ) R ( τ )    = 1 m ∑ i = 1 m R ( τ ( i ) ) ∑ t = 1 T p θ ( τ ( i ) ) q θ ′ ( τ ( i ) ) ∇ θ l o g   p θ ( a t ( i ) ∣ s t ( i ) )    = 1 m ∑ i = 1 m R ( τ ( i ) ) ∑ t = 1 T p θ ( τ ( i ) ) q θ ′ ( τ ( i ) ) ∇ θ p θ ( a t ( i ) ∣ s t ( i ) ) p θ ( a t ( i ) ∣ s t ( i ) )    = 1 m ∑ i = 1 m R ( τ ( i ) ) ∑ t = 1 T ∏ t = 0 T p θ ( a t ( i ) ∣ s t ( i ) ) p ( s t + 1 ( i ) ∣ s t ( i ) , a t ( i ) ) ∏ t = 0 T q θ ′ ( a t ( i ) ∣ s t ( i ) ) q ( s t + 1 ( i ) ∣ s t ( i ) , a t ( i ) ) ∇ θ p θ ( a t ( i ) ∣ s t ( i ) ) p θ ( a t ( i ) ∣ s t ( i ) )    ≈ 1 m ∑ i = 1 m R ( τ ( i ) ) ∑ t = 1 T ∇ θ p θ ( a t ( i ) ∣ s t ( i ) ) q θ ′ ( a t ( i ) ∣ s t ( i ) ) g = \nabla_{\theta} \sum_{\tau}q_{\theta'}(\tau)\frac{p_{\theta}(\tau)}{q_{\theta'}(\tau)}R(\tau) \\ \ \ = \frac{1}{m}\sum_{i=1}^{m}R(\tau^{(i)})\sum_{t=1}^{T}\frac{p_{\theta} (\tau^{(i)})}{q_{\theta'}(\tau^{(i)})}\nabla_{\theta}log\ p_\theta(a_{t}^{(i)}|s_{t}^{(i)}) \\ \ \ = \frac{1}{m}\sum_{i=1}^{m}R(\tau^{(i)})\sum_{t=1}^{T}\frac{p_{\theta}(\tau^{(i)})}{q_{\theta'}(\tau^{(i)})}\frac{\nabla_{\theta}p_\theta(a_{t}^{(i)}|s_{t}^{(i)})}{p_\theta(a_{t}^{(i)}|s_{t}^{(i)})} \\ \ \ = \frac{1}{m}\sum_{i=1}^{m}R(\tau^{(i)})\sum_{t=1}^{T} \frac{\prod_{t=0}^{T}p_{\theta}(a_{t}^{(i)}|s_{t}^{(i)})p(s_{t+1}^{(i)}|s_{t}^{(i)}, a_{t}^{(i)})} {\prod_{t=0}^{T}q_{\theta'}(a_{t}^{(i)}|s_{t}^{(i)})q(s_{t+1}^{(i)}|s_{t}^{(i)}, a_{t}^{(i)})} \frac{\nabla_{\theta}p_\theta(a_{t}^{(i)}|s_{t}^{(i)})}{p_\theta(a_{t}^{(i)}|s_{t}^{(i)})} \\ \ \ \approx \frac{1}{m}\sum_{i=1}^{m}R(\tau^{(i)})\sum_{t=1}^{T} \frac{\nabla_{\theta}p_\theta(a_{t}^{(i)}|s_{t}^{(i)})}{q_\theta'(a_{t}^{(i)}|s_{t}^{(i)})} g=θτqθ(τ)qθ(τ)pθ(τ)R(τ)  =m1i=1mR(τ(i))t=1Tqθ(τ(i))pθ(τ(i))θlog pθ(at(i)st(i))  =m1i=1mR(τ(i))t=1Tqθ(τ(i))pθ(τ(i))pθ(at(i)st(i))θpθ(at(i)st(i))  =m1i=1mR(τ(i))t=1Tt=0Tqθ(at(i)st(i))q(st+1(i)st(i),at(i))t=0Tpθ(at(i)st(i))p(st+1(i)st(i),at(i))pθ(at(i)st(i))θpθ(at(i)st(i))  m1i=1mR(τ(i))t=1Tqθ(at(i)st(i))θpθ(at(i)st(i))

这里对 g 取了近似,那么上面的
R ˉ θ \bar{R}_{\theta} Rˉθ
也可以近似为:

R ˉ θ ≈ E τ ∼ q θ ′ ( τ ) [ p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) R ( τ ) ] \bar{R}_{\theta} \approx E_{\tau\sim q_{\theta'}(\tau)}[\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}R(\tau)] RˉθEτqθ(τ)[qθ(atst)pθ(atst)R(τ)]

这就是论文里说的 Surrogate Objective,那这里是不是直接求导,使得新的期望回报值最大就行了吗?
当然不是!注意上面 g 近似成立的前提是 p 和 q 的值差距不大,如果差距太大的话,等式就不成立了,求导也没有意义,所以我们需要对 Surrogate Objective 进行截断,让 p 和 q 的值不要差距太大。
所以有了 Clipped Surrogate Objective:

E τ ∼ q θ ′ ( τ ) [ m i n ( p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) R ( τ ) , c l i p ( p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) , 1 − ϵ , 1 + ϵ ) R ( τ ) ) ] E_{\tau\sim q_{\theta'}(\tau)}[min(\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}R(\tau), clip(\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}, 1-\epsilon, 1 + \epsilon)R(\tau))] Eτqθ(τ)[min(qθ(atst)pθ(atst)R(τ),clip(qθ(atst)pθ(atst),1ϵ,1+ϵ)R(τ))]

clip 代表把 p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) \frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})} qθ(atst)pθ(atst) 限制在区间 (1- ϵ \epsilon ϵ, 1 + ϵ \epsilon ϵ)。
那么当 R > 0 时,Surrogate Objective 等同于:

E τ ∼ q θ ′ ( τ ) [ m i n ( p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) R ( τ ) , ( 1 + ϵ ) R ( τ ) ) ] E_{\tau\sim q_{\theta'}(\tau)}[min(\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}R(\tau), (1 + \epsilon)R(\tau))] Eτqθ(τ)[min(qθ(atst)pθ(atst)R(τ),(1+ϵ)R(τ))]

这就确保会增大 p θ ( a t ∣ s t ) p_\theta(a_{t}|s_{t}) pθ(atst) 但最大不会让 p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) \frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})} qθ(atst)pθ(atst) 大于 1 + ϵ \epsilon ϵ
当 R < 0,Surrogate Objective 等同于:

E τ ∼ q θ ′ ( τ ) [ m i n ( p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) R ( τ ) , ( 1 − ϵ ) R ( τ ) ) ] E_{\tau\sim q_{\theta'}(\tau)}[min(\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}R(\tau), (1-\epsilon)R(\tau))] Eτqθ(τ)[min(qθ(atst)pθ(atst)R(τ),(1ϵ)R(τ))]

这就确保会减小 p θ ( a t ∣ s t ) p_\theta(a_{t}|s_{t}) pθ(atst) 但最小不会让 p θ ( a t ∣ s t ) q θ ′ ( a t ∣ s t ) \frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})} qθ(atst)pθ(atst) 小于 1 - ϵ \epsilon ϵ
从而满足了让 p 和 q 的值不要差距太大的约束条件。
再把 R 替换成文一里提到的 Credit Assignment 的形式,我们对 REINFORCE 的算法的优化暂时到这里。

缺点

总结一下,我们用 Clipped Surrogate Objective 和 Credit Assignment 让 REINFORCE 可以做 off-policy,并且让 R 的表示更合理,但是我们再次看一下 R 现在的表示方法,虽然更合理了,但是还是有可以优化的地方。

R t ( τ ) = ∑ t = t ( a ) T γ t − t ( a ) r t , γ ∈ [ 0 , 1 ] R_{t}(\tau) = \sum_{t=t(a)}^{T}\gamma^{t-t(a)}r_{t},\gamma \in [0,1] Rt(τ)=t=t(a)Tγtt(a)rt,γ[0,1]

虽然对每个 a 都有相应的 R,但是回到最开始 a 是怎么来的,每一步的 a 是采样来的,所以这种 Monte-Carlo 式的评估 R 的方式会引入大的方差。

改进

下篇文章会给出方差大的解决办法。

参考

  1. Proximal Policy Optimization Algorithms
  2. 深度解读:Policy Gradient,PPO及PPG
  3. 李宏毅深度强化学习(国语)课程(2018)
  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿姆姆姆姆姆姆姆

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

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

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

打赏作者

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

抵扣说明:

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

余额充值