在之前的文章里介绍了 深度强化学习(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=1∑mR(τ(i))t=1∑Tqθ′(τ(i))pθ(τ(i))∇θlog pθ(at(i)∣st(i)) =m1i=1∑mR(τ(i))t=1∑Tqθ′(τ(i))pθ(τ(i))pθ(at(i)∣st(i))∇θpθ(at(i)∣st(i)) =m1i=1∑mR(τ(i))t=1∑T∏t=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=1∑mR(τ(i))t=1∑Tqθ′(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θ′(at∣st)pθ(at∣st)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θ′(at∣st)pθ(at∣st)R(τ),clip(qθ′(at∣st)pθ(at∣st),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θ′(at∣st)pθ(at∣st) 限制在区间 (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θ′(at∣st)pθ(at∣st)R(τ),(1+ϵ)R(τ))]
这就确保会增大
p
θ
(
a
t
∣
s
t
)
p_\theta(a_{t}|s_{t})
pθ(at∣st) 但最大不会让
p
θ
(
a
t
∣
s
t
)
q
θ
′
(
a
t
∣
s
t
)
\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}
qθ′(at∣st)pθ(at∣st) 大于 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θ′(at∣st)pθ(at∣st)R(τ),(1−ϵ)R(τ))]
这就确保会减小
p
θ
(
a
t
∣
s
t
)
p_\theta(a_{t}|s_{t})
pθ(at∣st) 但最小不会让
p
θ
(
a
t
∣
s
t
)
q
θ
′
(
a
t
∣
s
t
)
\frac{p_\theta(a_{t}|s_{t})}{q_\theta'(a_{t}|s_{t})}
qθ′(at∣st)pθ(at∣st) 小于 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γt−t(a)rt,γ∈[0,1]
虽然对每个 a 都有相应的 R,但是回到最开始 a 是怎么来的,每一步的 a 是采样来的,所以这种 Monte-Carlo 式的评估 R 的方式会引入大的方差。
改进
下篇文章会给出方差大的解决办法。