本专栏按照 https://lilianweng.github.io/lil-log/2018/04/08/policy-gradient-algorithms.html 顺序进行总结 。
S A C \color{red}SAC SAC :[ paper:Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor | code ]
原理解析
1. 主要思路
基于DDPG,主要是利用 off policy 的样本效率比较高 以及 最大熵 来增加探索,把actor critic放入算法中,结合了on policy的stable性质。将policy entropy 放入reward 中,共同maximize,鼓励agent在reward大区域内增加探索。
算法描述:将最大化的目标加入policy entropy,鼓励agent探索。
软演员-评论家算法(Soft Actor-Critic,SAC)将策略的熵度量纳入回报函数中用以鼓励探索:我们希望学习到一种尽可能随机行动的策略,同时仍然能够在任务中完成目标。它是一个遵循最大熵强化学习框架的离线演员-评论家模型。一个先例工作是软Q学习。
SAC算法中的三个关键部分如下:
- 包含独立的 策略网络 以及 值函数网络 的演员-评论家架构;
- 离线策略形式 使其能够复用历史收集的数据从而实现高采样有效性;
- 熵最大化以使得训练稳定并鼓励探索。
综上所述:策略的训练目标是同时最大化期望累积回报以及策略的熵度量:
J ( θ ) = ∑ t = 1 T E ( s t , a t ) ∼ ρ π θ [ r ( s t , a t ) + α H ( π θ ( . ∣ s t ) ) ] J(\theta)=\sum_{t=1}^{T} \mathbb{E}_{\left(s_{t}, a_{t}\right) \sim \rho_{\pi_{\theta}}}\left[r\left(s_{t}, a_{t}\right)+\alpha \mathcal{H}\left(\pi_{\theta}\left( . | s_{t}\right)\right)\right] J(θ)=t=1∑TE(st,at)∼ρπθ[r(st,at)+αH(πθ(.∣st))]
其中:
- H ( . ) \mathcal{H}( .) H(.) 表示熵度量,
- α α α 被称为热度(temperature)参数用以控制熵正则项的重要度。
熵最大化使得策略再训练过程中可以:
(1)进行更多的探索
(2)捕获近似最优策略的多种模式(例如,如果存在似乎同样好的多种选项,则策略应该为每个选项分配以相同的概率被选中)。
准确地说,SAC旨在学习三个函数:
- 由 θ θ θ 参数化的策略 π θ π_θ πθ。
- 由 w w w 参数化的软 Q Q Q 值函数 Q w Q_w Qw。
- 由 ψ ψ ψ 参数化的软状态-值函数 V ψ V_ψ Vψ ;理论上来说我们可以通过 Q Q Q 以及 π π π 来推导出 V V V,但是在实际情况下,显式对状态-值函数建模可以使得训练过程更加稳定。
软
Q
Q
Q 值以及软状态值 分别定义如下:
Q
(
s
t
,
a
t
)
=
r
(
s
t
,
a
t
)
+
γ
E
s
t
+
1
∼
ρ
π
(
s
)
[
V
(
s
t
+
1
)
]
; 根据贝尔曼方程
where
V
(
s
t
)
=
E
a
t
∼
π
[
Q
(
s
t
,
a
t
)
−
α
log
π
(
a
t
∣
s
t
)
]
; 软状态值函数
\begin{aligned} Q(s_t, a_t) &= r(s_t, a_t) + \gamma \mathbb{E}_{s_{t+1} \sim \rho_{\pi}(s)} [V(s_{t+1})] & \text{; 根据贝尔曼方程}\\ \text{where }V(s_t) &= \mathbb{E}_{a_t \sim \pi} [Q(s_t, a_t) - \alpha \log \pi(a_t \vert s_t)] & \text{; 软状态值函数} \end{aligned}
Q(st,at)where V(st)=r(st,at)+γEst+1∼ρπ(s)[V(st+1)]=Eat∼π[Q(st,at)−αlogπ(at∣st)]; 根据贝尔曼方程; 软状态值函数
Thus, Q ( s t , a t ) = r ( s t , a t ) + γ E ( s t + 1 , a t + 1 ) ∼ ρ π [ Q ( s t + 1 , a t + 1 ) − α log π ( a t + 1 ∣ s t + 1 ) ] \text{Thus, } ~~\textcolor{red}{Q(s_t, a_t)} = r(s_t, a_t) + \gamma \mathbb{E}_{(s_{t+1}, a_{t+1}) \sim \rho_{\pi}} [Q(s_{t+1}, a_{t+1}) - \alpha \log \pi(a_{t+1} \vert s_{t+1})] Thus, Q(st,at)=r(st,at)+γE(st+1,at+1)∼ρπ[Q(st+1,at+1)−αlogπ(at+1∣st+1)]
- ρ π ( s ) \rho_{\pi}(s) ρπ(s) 和 ρ π ( s , a ) \rho_{\pi}(s,a) ρπ(s,a) 分别表示由策略 π ( a ∣ s ) \pi(a\vert s) π(a∣s) 导出的状态分布的状态以及状态-动作边际分布;DPG算法部分有类似的定义。
软状态值函数通过最小化均方误差来训练:
J
V
(
ψ
)
=
E
s
t
∼
D
[
1
2
(
V
ψ
(
s
t
)
−
E
[
Q
w
(
s
t
,
a
t
)
−
log
π
θ
(
a
t
∣
s
t
)
]
)
2
]
其中梯度为:
∇
ψ
J
V
(
ψ
)
=
∇
ψ
V
ψ
(
s
t
)
(
V
ψ
(
s
t
)
−
Q
w
(
s
t
,
a
t
)
+
log
π
θ
(
a
t
∣
s
t
)
)
\begin{aligned} J_V(\psi) &= \mathbb{E}_{s_t \sim \mathcal{D}} [\frac{1}{2} \big(V_\psi(s_t) - \mathbb{E}[Q_w(s_t, a_t) - \log \pi_\theta(a_t \vert s_t)] \big)^2] \\ \text{其中梯度为: }\nabla_\psi J_V(\psi) &= \nabla_\psi V_\psi(s_t)\big( V_\psi(s_t) - Q_w(s_t, a_t) + \log \pi_\theta (a_t \vert s_t) \big) \end{aligned}
JV(ψ)其中梯度为: ∇ψJV(ψ)=Est∼D[21(Vψ(st)−E[Qw(st,at)−logπθ(at∣st)])2]=∇ψVψ(st)(Vψ(st)−Qw(st,at)+logπθ(at∣st))
其中 D \mathcal{D} D 代表经验回放缓冲。
软Q值函数通过最小化软贝尔曼残差来训练:
J
Q
(
w
)
=
E
(
s
t
,
a
t
)
∼
D
[
1
2
(
Q
w
(
s
t
,
a
t
)
−
(
r
(
s
t
,
a
t
)
+
γ
E
s
t
+
1
∼
ρ
π
(
s
)
[
V
ψ
ˉ
(
s
t
+
1
)
]
)
)
2
]
其中梯度为:
∇
w
J
Q
(
w
)
=
∇
w
Q
w
(
s
t
,
a
t
)
(
Q
w
(
s
t
,
a
t
)
−
r
(
s
t
,
a
t
)
−
γ
V
ψ
ˉ
(
s
t
+
1
)
)
\begin{aligned} J_Q(w) &= \mathbb{E}_{(s_t, a_t) \sim \mathcal{D}} [\frac{1}{2}\big( Q_w(s_t, a_t) - (r(s_t, a_t) + \gamma \mathbb{E}_{s_{t+1} \sim \rho_\pi(s)}[V_{\bar{\psi}}(s_{t+1})]) \big)^2] \\ \text{其中梯度为: } \nabla_w J_Q(w) &= \nabla_w Q_w(s_t, a_t) \big( Q_w(s_t, a_t) - r(s_t, a_t) - \gamma V_{\bar{\psi}}(s_{t+1})\big) \end{aligned}
JQ(w)其中梯度为: ∇wJQ(w)=E(st,at)∼D[21(Qw(st,at)−(r(st,at)+γEst+1∼ρπ(s)[Vψˉ(st+1)]))2]=∇wQw(st,at)(Qw(st,at)−r(st,at)−γVψˉ(st+1))
其中
ψ
ˉ
\bar{\psi}
ψˉ 代表目标值函数,它是个指数移动平均值(exponential moving average)或者只是采用一种“硬”方式进行周期更新。就像DQN中目标Q网络中的参数一样,为了使得训练过程更加稳定。
SAC通过最小化如下 KL散度来去更新策略:
π new = arg min π ′ ∈ Π D KL ( π ′ ( . ∣ s t ) ∥ exp ( Q π old ( s t , . ) ) Z π old ( s t ) ) = arg min π ′ ∈ Π D KL ( π ′ ( . ∣ s t ) ∥ exp ( Q π old ( s t , . ) − log Z π old ( s t ) ) ) objective for update: J π ( θ ) = ∇ θ D KL ( π θ ( . ∣ s t ) ∥ exp ( Q w ( s t , . ) − log Z w ( s t ) ) ) = E a t ∼ π [ − log ( exp ( Q w ( s t , a t ) − log Z w ( s t ) ) π θ ( a t ∣ s t ) ) ] = E a t ∼ π [ log π θ ( a t ∣ s t ) − Q w ( s t , a t ) + log Z w ( s t ) ] \begin{aligned} \pi_\text{new} &= \arg\min_{\pi' \in \Pi} D_\text{KL} \Big( \pi'(.\vert s_t) \| \frac{\exp(Q^{\pi_\text{old}}(s_t, .))}{Z^{\pi_\text{old}}(s_t)} \Big) \\[6pt] &= \arg\min_{\pi' \in \Pi} D_\text{KL} \big( \pi'(.\vert s_t) \| \exp(Q^{\pi_\text{old}}(s_t, .) - \log Z^{\pi_\text{old}}(s_t)) \big) \\[6pt] \text{objective for update: } J_\pi(\theta) &= \nabla_\theta D_\text{KL} \big( \pi_\theta(. \vert s_t) \| \exp(Q_w(s_t, .) - \log Z_w(s_t)) \big) \\[6pt] &= \mathbb{E}_{a_t\sim\pi} \Big[ - \log \big( \frac{\exp(Q_w(s_t, a_t) - \log Z_w(s_t))}{\pi_\theta(a_t \vert s_t)} \big) \Big] \\[6pt] &= \mathbb{E}_{a_t\sim\pi} [ \log \pi_\theta(a_t \vert s_t) - Q_w(s_t, a_t) + \log Z_w(s_t) ] \end{aligned} πnewobjective for update: Jπ(θ)=argπ′∈ΠminDKL(π′(.∣st)∥Zπold(st)exp(Qπold(st,.)))=argπ′∈ΠminDKL(π′(.∣st)∥exp(Qπold(st,.)−logZπold(st)))=∇θDKL(πθ(.∣st)∥exp(Qw(st,.)−logZw(st)))=Eat∼π[−log(πθ(at∣st)exp(Qw(st,at)−logZw(st)))]=Eat∼π[logπθ(at∣st)−Qw(st,at)+logZw(st)]
其中 ∏ \prod ∏ 是潜在策略的集合,我们可以将这些策略建模为容易处理的形式;例如, ∏ \prod ∏ 可以是高斯混合分布族,虽然建模时复杂度较高但是具有很强的表达能力并且易于处理。 Z π old ( s t ) Z^{\pi_\text{old}}(s_t) Zπold(st) 是用于正则化分布的配分函数。它一般是很难处理的但所幸对于梯度值没有影响。最小化 J π ( θ ) J_π(θ) Jπ(θ) 的方式依赖于 ∏ \prod ∏ 的选择。
这个更新保证了 Q π new ( s t , a t ) ≥ Q π old ( s t , a t ) Q^{\pi_\text{new}}(s_t, a_t) \geq Q^{\pi_\text{old}}(s_t, a_t) Qπnew(st,at)≥Qπold(st,at),请在原论文附录B.2的证明中 查阅这个引理。
2. 算法详细实现
详见博客说明,十分清晰明了:最前沿:深度解读Soft Actor-Critic 算法
算法实现
总体流程
一旦我们为软动作-值,软状态值和策略网络定义了目标函数和梯度,软演员-评论家算法就很简单了:
伪代码如下(在连续任务下):