RL策略梯度方法之(十): Trust region policy optimization (TRPO)

    科技2022-07-14  134

    本专栏按照 https://lilianweng.github.io/lil-log/2018/04/08/policy-gradient-algorithms.html 顺序进行总结 。


    文章目录

    原理解析整体概述算法推理过程 算法实现总体流程代码实现


    T R P O \color{red}TRPO TRPO :[ paper | code ]


    原理解析

    整体概述

    为了提高训练的稳定性,我们应该避免在一步中进行大幅度的策略参数更新。Trust region policy optimization (TRPO) 通过在每次迭代时对策略更新的大小施加KL散度约束来实现这一思想。

    考虑这样一个情况:用来收集轨迹的策略 β \beta β 和用于优化更新的策略 π \pi π 是不同的。在off-policy 模型中的目标函数 目标函数度量了 状态访问分布和动作的总体优势 ,而训练数据分布和真实的策略状态分布之间的不匹配通过重要采样估计器进行补偿。

    J ( θ ) = ∑ s ∈ S ρ π θ old ∑ a ∈ A ( π θ ( a ∣ s ) A ^ θ old ( s , a ) ) = ∑ s ∈ S ρ π θ old ∑ a ∈ A ( β ( a ∣ s ) π θ ( a ∣ s ) β ( a ∣ s ) A ^ θ old ( s , a ) ) ; 重要性采样 = E s ∼ ρ π θ old , a ∼ β [ π θ ( a ∣ s ) β ( a ∣ s ) A ^ θ old ( s , a ) ] \begin{aligned} J(\theta) &= \sum_{s \in \mathcal{S}} \rho^{\pi_{\theta_\text{old}}} \sum_{a \in \mathcal{A}} \big( \pi_\theta(a \vert s) \hat{A}_{\theta_\text{old}}(s, a) \big) & \\ &= \sum_{s \in \mathcal{S}} \rho^{\pi_{\theta_\text{old}}} \sum_{a \in \mathcal{A}} \big( \beta(a \vert s) \frac{\pi_\theta(a \vert s)}{\beta(a \vert s)} \hat{A}_{\theta_\text{old}}(s, a) \big) & \scriptstyle{\text{; 重要性采样}} \\ &= \mathbb{E}_{s \sim \rho^{\pi_{\theta_\text{old}}}, a \sim \beta} \big[ \frac{\pi_\theta(a \vert s)}{\beta(a \vert s)} \hat{A}_{\theta_\text{old}}(s, a) \big] & \end{aligned} J(θ)=sSρπθoldaA(πθ(as)A^θold(s,a))=sSρπθoldaA(β(as)β(as)πθ(as)A^θold(s,a))=Esρπθold,aβ[β(as)πθ(as)A^θold(s,a)]重要性采样

    θ o l d \theta_{old} θold 已知,是更新前的策略参数; ρ π θ old \rho^{\pi_{\theta_\text{old}}} ρπθold 定义方式与上述相同; β ( a ∣ s ) \beta(a \vert s) β(as) 是收集轨迹的行为策略;注意:在此我们是用了 A ^ ( . ) \hat{A}(.) A^(.)而不是 A ( . ) {A}(.) A(.),因为真实回报往往不可知。

    在训练策略时,理论上收集数据的策略和我们想要优化的策略是一样的。然而,当rollout workers和优化器异步并行运行时,行为策略可能会过时。TRPO 考虑了这个轻微的差异,它将行为策略记做: π θ old ( a ∣ s ) \pi_{\theta_\text{old}}(a \vert s) πθold(as),因此目标函数变为:

    J ( θ ) = E s ∼ ρ π θ old , a ∼ π θ old [ π θ ( a ∣ s ) π θ old ( a ∣ s ) A ^ θ old ( s , a ) ] J(\theta) = \mathbb{E}_{s \sim \rho^{\pi_{\theta_\text{old}}}, a \sim \pi_{\theta_\text{old}}} \big[ \frac{\pi_\theta(a \vert s)}{\pi_{\theta_\text{old}}(a \vert s)} \hat{A}_{\theta_\text{old}}(s, a) \big] J(θ)=Esρπθold,aπθold[πθold(as)πθ(as)A^θold(s,a)]

    TRPO 目的是最大化目标函数 J ( θ ) J(\theta) J(θ),满足置信域约束:即:使得新老策略的距离的KL散度足够小,小于等于参数 δ δ δ

    E s ∼ ρ π θ old [ D KL ( π θ old ( . ∣ s ) ∥ π θ ( . ∣ s ) ] ≤ δ \mathbb{E}_{s \sim \rho^{\pi_{\theta_\text{old}}}} [D_\text{KL}(\pi_{\theta_\text{old}}(.\vert s) \| \pi_\theta(.\vert s)] \leq \delta Esρπθold[DKL(πθold(.s)πθ(.s)]δ

    这样的话,当这一硬约束得到满足时,新旧策略就不会有太大的分歧。尽管如此,TRPO仍然可以保证策略迭代的单调改进。


    简而言之,可以用下面这个式子表示TRPO的思想:

    算法推理过程

    详见我的另外一篇博客:第三篇 直接策略搜索——基于置信域策略优化的强化学习方法

    或者见另一篇,写得更规范:强化学习进阶 第七讲 TRPO

    算法实现

    总体流程

    代码实现

    Processed: 0.020, SQL: 8