强化学习的前世今生(二)
接上篇强化学习的前世今生(一),本文主要介绍强化学习中的蒙特卡洛算法,TD算法,策略梯度算法以及Actor-Critic算法
2 蒙特卡洛和TD
2.1 蒙特卡洛方法
在强化学习中,蒙特卡洛(Monte Carl0)方法通过多次实验采样轨迹,根据这些轨迹来估计价值函数。例如,假设需要算状态价值函数 V π ( s t ) V^{\pi}(s_t) Vπ(st),那么可以从状态 s t s_t st出发,依据策略 π \pi π采样 N N N条轨迹,对多条轨迹的回报求均值即可得到状态价值函数的估计
V π ( s t ) = 1 N ∑ i = 1 N G t i (2.1) V^{\pi}(s_t)=\frac{1}{N}\sum_{i=1}^{N}G_t^{i}\tag{2.1} Vπ(st)=N1i=1∑NGti(2.1)
根据大数定理,当采样次数 N N N足够多时,状态价值函数的估计值将接近真值。
在实际应用中,蒙特卡洛方法可以在每次采样完一条轨迹后立即更新状态价值函数 V π ( s ) V^{\pi}(s) Vπ(s),而不必等到所有轨迹都采样完后再计算。状态价值函数的更新规则如下
V π ( s t ) ← V π ( s t ) + α ( G t − V π ( s t ) ) (2.2) V^{\pi}(s_t)\leftarrow V^{\pi}(s_t)+\alpha(G_t- V^{\pi}(s_t))\tag{2.2} Vπ(st)←Vπ(st)+α(Gt−Vπ(st))(2.2)
蒙特卡洛方法方法基于实际轨迹的完整回报进行更新,具有回报估计的无偏性,但由于该方法依赖完整的轨迹计算回报,经过多个步骤,且每个步骤的状态转移都具有随机性质,故引入了较大的回报估计方差。
2.2 TD
TD(Temporal Difference,时序差分)方法基于当前的价值估计进行自举(Bootstraping),即通过包含当前对下一个状态的价值估计$ V^{\pi}(s_{t+1}) 的 T D 目标来更新价值函数 的TD目标来更新价值函数 的TD目标来更新价值函数 V^{\pi}(s_{t})$。与蒙特卡洛方法不同,TD方法不需要等到整个轨迹采样结束才对价值函数进行更新,而是每前进一个时间步就进行一次更新。具体来说,TD方法的价值函数更新规则如下
V π ( s t ) ← V π ( s t ) + α i ( s t ) [ r t + 1 + γ V π ( s t + 1 ) − V π ( s t ) ] (2.3) \begin{align*} V^{\pi}(s_t)&\leftarrow V^{\pi}(s_t)+\alpha_i(s_t)[r_{t+1}+\gamma V^{\pi}(s_{t+1})- V^{\pi}(s_t)]\\ \end{align*}\tag{2.3} Vπ(st)←Vπ(st)+αi(st)[rt+1+γVπ(st+1)−Vπ(st)](2.3)
上式中, r t + 1 + V π ( s t + 1 ) r_{t+1}+ V^{\pi}(s_{t+1}) rt+1+Vπ(st+1)也被称为TD目标,是价值函数通过式更新所要接近的目标; α k ( s t ) \alpha_k(s_t) αk(st)表示状态 s t s_t st在 t t t时刻的学习率,而 δ t = r t + 1 + γ V π ( s t + 1 ) − V π ( s t ) \delta_{t}=r_{t+1}+\gamma V^{\pi}(s_{t+1})- V^{\pi}(s_t) δt=rt+1+γVπ(st+1)−Vπ(st)被称为TD误差,衡量当前价值估计与TD目标之前的差异,若TD误差大于 0 0 0,则说明当前估计的 V π ( s t ) V^{\pi}(s_t) Vπ(st)偏低,反之若TD误差小于 0 0 0,则当前估计的 V π ( s t ) V^{\pi}(s_t) Vπ(st)偏高。
下面说明选定 r t + 1 + γ V π ( s t + 1 ) r_{t+1}+\gamma V^{\pi}(s_{t+1}) rt+1+γVπ(st+1)作为TD目标的原因。首先,对于真实的状态价值函数y有
V c π ( s t ) = E π [ G t ∣ S t = s t ] = E π [ ∑ k = 0 ∞ γ k R t + k + 1 ∣ S t = s t ] = E π [ R t + 1 + γ ∑ k = 0 ∞ γ k R t + k + 2 ∣ S t = s t ] = E π [ R t + 1 + γ V c π ( s t + 1 ) ∣ S t = s t ] (2.4) \begin{align*} V^{\pi}_{\mathrm{c}}(s_t)&=\mathbb{E}_{\pi}[G_t|S_t=s_t]\\ &=\mathbb{E}_{\pi}[\sum_{k=0}^{\infty}\gamma^{k} R_{t+k+1}|S_t=s_t]\\ &=\mathbb{E}_{\pi}[R_{t+1}+\gamma\sum_{k=0}^{\infty}\gamma^{k} R_{t+k+2}|S_t=s_t]\\ &=\mathbb{E}_{\pi}[R_{t+1}+\gamma V^{\pi}_{\mathrm{c}}(s_{t+1})|S_t=s_t]\\ \end{align*}\tag{2.4} Vcπ(st)=Eπ[Gt∣St=st]=Eπ[k=0∑∞γkRt+k+1∣St=st]=Eπ[Rt+1+γk=0∑∞γkRt+k+2∣St=st]=Eπ[Rt+1+γVcπ(st+1)∣St=st](2.4)
根据上式,对于真实的状态价值函数 V c π ( s t ) V^{\pi}_{\mathrm{c}}(s_t) Vcπ(st),c表示correct,其应该等于大量 R t + 1 + γ V c π ( s t + 1 ) ∣ S t = s t R_{t+1}+\gamma V^{\pi}_{\mathrm{c}}(s_{t+1})|S_t=s_t Rt+1+γVcπ(st+1)∣St=st采样的均值。然而,当前估计的状态价值函数 V π ( s t ) V^{\pi}(s_{t}) Vπ(st)通常并非真实的状态价值函数,故依据 R t + 1 + γ V π ( s t + 1 ) ∣ S t = s t R_{t+1}+\gamma V^{\pi}(s_{t+1})|S_t=s_t Rt+1+γVπ(st+1)∣St=st进行的采样在期望上和 V π ( s t ) V^{\pi}(s_{t}) Vπ(st)并不相等,并且,从采样角度有
[ R t + 1 + γ V c π ( s t + 1 ) ∣ S t = s t ] − [ R t + 1 + γ V π ( s t + 1 ) ∣ S t = s t ] = γ ( V c π ( s t + 1 ) − V π ( s t + 1 ) ) ∣ S t = s t (2.5) [R_{t+1}+\gamma V^{\pi}_{\mathrm{c}}(s_{t+1})|S_t=s_t]-[R_{t+1}+\gamma V^{\pi}(s_{t+1})|S_t=s_t]=\gamma\left(V^{\pi}_{\mathrm{c}}(s_{t+1})- V^{\pi}(s_{t+1})\right)|S_t=s_t\tag{2.5} [Rt+1+γVcπ(st+1)∣St=st]−[Rt+1+γVπ(st+1)∣St=st]=γ(Vcπ(st+1)−Vπ(st+1))∣St=st(2.5)
相比于当前步真实的状态价值函数和估计出的状态价值函数之差 V c π ( s t ) − V π ( s t ) V^{\pi}_{\mathrm{c}}(s_t)-V^{\pi}(s_t) Vcπ(st)−Vπ(st), ( 2.5 ) (2.5) (2.5)显然降低了误差。
当满足下述条件时, V π V^{\pi} Vπ经过 ( 2.2 ) (2.2) (2.2)的迭代最终收敛于 V c π V^{\pi}_{\mathrm{c}} Vcπ
- 状态空间有限且每个状态访问无限次
- 对每个状态 s ∈ S s\in \mathcal{S} s∈S, ∑ t = 0 ∞ α t ( s ) = ∞ \sum_{t=0}^{\infty}\alpha_t(s)=\infty ∑t=0∞αt(s)=∞, ∑ t = 0 ∞ ∣ α t ( s ) ∣ 2 < ∞ \sum_{t=0}^{\infty}|\alpha_t(s)|^{2}<\infty ∑t=0∞∣αt(s)∣2<∞,即步长要足够小,但不能太快趋近于 0 0 0
在TD方法中,如果价值估计不准确,那么自举会引入偏差,并且如果存在系统性误差,那么偏差可能会逐步累积;另一方面,由于TD方法的更新仅基于一步的转移,不需要等待整条轨迹采样结束,受到随机性的影响小,这减小了方差。以上分析基于状态价值函数,对于动作价值函数有类似结论。
3 策略梯度
3.1 策略梯度算法
策略梯度(Policy Gradient, PG)算法直接根据 ( 1.2 ) (1.2) (1.2)式求解策略参数 θ \theta θ。首先,写出希望最大化的目标函数
J ( θ ) = ∫ ν 0 ( s ) V π θ ( s ) d s (3.1) \begin{align*} J(\theta) &=\int \nu_0(s)V^{\pi_{\theta}}(s)ds\\ \end{align*}\tag{3.1} J(θ)=∫ν0(s)Vπθ(s)ds(3.1)
其中 ν 0 ( s ) \nu_0(s) ν0(s)表示初始状态分布。
根据策略梯度定理
∇ θ J ( θ ) = 1 1 − γ ∫ ν π θ ( s ) d s ∫ Q π θ ( s , a ) ∇ θ π θ ( a ∣ s ) d a (3.2) \nabla_{\theta} J(\theta)=\frac{1}{1-\gamma}\int \nu^{\pi_{\theta}}(s)ds\int Q^{\pi_\theta}(s,a)\nabla_{\theta}\pi_{\theta}(a|s)da\tag{3.2} ∇θJ(θ)=1−γ1∫νπθ(s)ds∫Qπθ(s,a)∇θπθ(a∣s)da(3.2)
继续简化上面的公式
∇ θ J ( θ ) ∝ ∫ ν π θ ( s ) d s ∫ Q π θ ( s , a ) ∇ θ π θ ( a ∣ s ) d a = ∫ ν π θ ( s ) d s ∫ π θ ( a ∣ s ) Q π θ ( s , a ) ∇ θ π θ ( a ∣ s ) π θ ( a ∣ s ) d a = E ( s , a ) ∼ ρ π θ ( s , a ) [ Q π θ ( s , a ) ∇ θ log π θ ( a ∣ s ) ] (3.3) \begin{align*} \nabla_{\theta} J(\theta) &\propto\int \nu^{\pi_{\theta}}(s)ds\int Q^{\pi_{\theta}}(s,a)\nabla_{\theta}\pi_{\theta}(a|s)da\\ &=\int \nu^{\pi_{\theta}}(s)ds\int \pi_{\theta}(a|s)Q^{\pi_{\theta}}(s,a)\frac{\nabla_{\theta}\pi_{\theta}(a|s)}{\pi_{\theta}(a|s)}da\\ &=\mathbb{E}_{(s,a)\sim\rho^{\pi_{\theta}}(s,a)}[Q^{\pi_{\theta}}(s,a)\nabla_{\theta}\log\pi_{\theta}(a|s)] \end{align*}\tag{3.3} ∇θJ(θ)∝∫νπθ(s)ds∫Qπθ(s,a)∇θπθ(a∣s)da=∫νπθ(s)ds∫πθ(a∣s)Qπθ(s,a)πθ(a∣s)∇θπθ(a∣s)da=E(s,a)∼ρπθ(s,a)[Qπθ(s,a)∇θlogπθ(a∣s)](3.3)
实际上, ( 3.3 ) (3.3) (3.3)还可以改写为
∇ θ J ( θ ) = E s ∼ ν π θ ( s ) [ V π θ ( s ) ∇ θ log π θ ( a ∣ s ) ] (3.4) \begin{align*} \nabla_{\theta} J(\theta) &=\mathbb{E}_{s\sim\nu^{\pi_{\theta}}(s)}[V^{\pi_{\theta}}(s)\nabla_{\theta}\log\pi_{\theta}(a|s)] \end{align*}\tag{3.4} ∇θJ(θ)=Es∼νπθ(s)[Vπθ(s)∇θlogπθ(a∣s)](3.4)
对每条轨迹,即对每条轨迹 τ \tau τ计算
∇ θ J ( θ ) = 1 T ∑ t = 0 T − 1 ( γ t Q π θ ( s t , a t ) ∇ θ log π θ ( a t ∣ s t ) ) (3.5) \nabla_{\theta}J(\theta)=\frac{1}{T}\sum_{t=0}^{T-1}\left(\gamma^t Q^{\pi_\theta}(s_t,a_t)\nabla_{\theta}\log\pi_{\theta}(a_t|s_t)\right)\tag{3.5} ∇θJ(θ)=T1t=0∑T−1(γtQπθ(st,at)∇θlogπθ(at∣st))(3.5)
并通过梯度上升的方法更新策略参数。实际上,并不需要在每个时间步加权 γ t \gamma^t γt
策略梯度算法伪代码如下:
1 初始化策略参数 θ \theta θ
2 如果策略参数没有收敛,循环执行
3 用当前策略 π θ \pi_{\theta} πθ 采样轨迹 τ = { ( s 0 , a 0 ) , ( r 1 , s 1 , a 1 ) , ( r 2 , s 2 , a 2 ) , ⋯ , ( r T − 1 , s T − 1 , a T − 1 ) , ( r T , s T ) } \tau=\{(s_0,a_0),(r_1,s_1,a_1),(r_2,s_2,a_2),\cdots,(r_{T-1},s_{T-1},a_{T-1}),(r_{T},s_{T})\} τ={(s0,a0),(r1,s1,a1),(r2,s2,a2),⋯,(rT−1,sT−1,aT−1),(rT,sT)}
4 计算轨迹每个时刻 t = 0 , 1 , ⋯ , T − 1 t=0,1,\cdots,T-1 t=0,1,⋯,T−1的加权动作价值 Q t π θ ( s t , a t ) = ∑ t ′ = t T − 1 γ t ′ − t r t ′ + 1 Q^{\pi_\theta}_{t}(s_t,a_t)=\sum_{t^{\prime}=t}^{T-1}\gamma^{t^{\prime}-t}r_{t^{\prime}+1} Qtπθ(st,at)=∑t′=tT−1γt′−trt′+1
5 求解目标函数梯度 ∇ θ J ( θ ) = 1 T ∑ t = 0 T − 1 ( γ t Q t π θ ( s t , a t ) ∇ θ log π θ ( a t ∣ s t ) ) \nabla_{\theta}J(\theta)=\frac{1}{T}\sum_{t=0}^{T-1}\left(\gamma^tQ^{\pi_\theta}_{t}(s_t,a_t)\nabla_{\theta}\log\pi_{\theta}(a_t|s_t)\right) ∇θJ(θ)=T1∑t=0T−1(γtQtπθ(st,at)∇θlogπθ(at∣st))
6 更新策略参数 θ ← θ + α ∇ θ J ( θ ) \theta\leftarrow\theta+\alpha\nabla_{\theta}J(\theta) θ←θ+α∇θJ(θ)
4 Actor-Critic
策略梯度算法中,依靠蒙特卡洛方法采样方法得到完整轨迹,并计算各时间步开始到轨迹结束的回报 R t ( τ i ) R_{t}(\tau^{i}) Rt(τi),蒙特卡洛方法引入了较大的回报估计方差,从而导致了较大的梯度方差。与策略梯度算法不同,Actor-Critic算法使用价值网络(也称为Critic网络)来近似状态当前策略 π θ \pi_{\theta} πθ的价值函数 V π θ ( s ) V^{\pi_{\theta}}(s) Vπθ(s)或动作价值函数 Q π θ ( s , a ) Q^{\pi_{\theta}}(s,a) Qπθ(s,a),以估计从当前状态或状态动作对开始的期望回报,从而计算梯度,并通过策略网络(也称为Actor网络)来跟新策略参数 θ \theta θ。通过使用Critic网络,可以大幅减小回报估计的方差,提高训练效率。
例如,如果Critic网络估计的是 V π θ ( s t ) V^{\pi_{\theta}}(s_t) Vπθ(st),且因为此时只需要单步采样,而非采样整条轨迹,那么策略梯度中目标函数的梯度可以写成
∇ θ J ( θ ) = ∇ θ log π θ ( a t ∣ s t ) V w ( s t ) (4.1) \nabla_{\theta}J(\theta)=\nabla_{\theta}\log\pi_{\theta}(a_t|s_t)V_{w}(s_t)\tag{4.1} ∇θJ(θ)=∇θlogπθ(at∣st)Vw(st)(4.1)
其中, θ \theta θ是Actor网络,即策略网络的参数, w w w是Critic网络,即价值网络的参数。该式也即策略网络的参数更新公式。
下面讨论价值网络的更新公式,采用动作价值函数的单步TD误差作为残差,则损失函数为
L ( w ) = 1 2 ( r t + 1 + V w ( s t + 1 ) − V w ( s t ) ) 2 (4.2) L(w)=\frac{1}{2}(r_{t+1}+V_w(s_{t+1})-V_{w}(s_t))^2\tag{4.2} L(w)=21(rt+1+Vw(st+1)−Vw(st))2(4.2)
首先计算损失函数的梯度,注意此处 r t + V w ( s t + 1 ) r_{t}+V_w(s_{t+1}) rt+Vw(st+1)仅作为预测值,不参与梯度计算,因此
∇ w L ( w ) = − ( r t + 1 + V w ( s t + 1 ) − V w ( s t ) ) ∇ w V w ( s t ) (4.3) \nabla_{w}L(w)=-(r_{t+1}+V_w(s_{t+1})-V_{w}(s_t))\nabla_{w}V_{w}(s_t)\tag{4.3} ∇wL(w)=−(rt+1+Vw(st+1)−Vw(st))∇wVw(st)(4.3)
由于希望最小化 L ( w ) L(w) L(w),因此采用梯度下降算法更新参数 w w w即可。
Actor-Critic算法伪代码如下:
1 初始化策略网络参数 θ \theta θ和价值网络参数 w w w,时刻 t = 0 t=0 t=0,状态 s 0 s_0 s0
2 在 s 0 s_0 s0下根据当前策略 π θ \pi_{\theta} πθ 采样动作 a 0 a_0 a0
3 如果策略网络参数或价值网络参数没有收敛,循环执行
4 执行动作 a t a_{t} at,得到奖励 r t + 1 r_{t+1} rt+1,并进入下一个状态 s t + 1 s_{t+1} st+1
5 在 s t + 1 s_{t+1} st+1下根据当前策略 π θ \pi_{\theta} πθ 采样 a t + 1 a_{t+1} at+1
6 计算策略网络的目标函数梯度 ∇ θ J ( θ ) = ∇ θ log π θ ( a t ∣ s t ) V w ( s t ) \nabla_{\theta}J(\theta)=\nabla_{\theta}\log\pi_{\theta}(a_t|s_t)V_{w}(s_t) ∇θJ(θ)=∇θlogπθ(at∣st)Vw(st)
7 通过梯度上升更新策略网络参数 θ ← θ + α θ ∇ θ J ( θ ) \theta\leftarrow\theta+\alpha_{\theta}\nabla_{\theta}J(\theta) θ←θ+αθ∇θJ(θ)
8 计算 t t t时刻动作价值函数的时序差分误差 δ t = r t + 1 + V w ( s t + 1 ) − V w ( s t ) \delta_{t}=r_{t+1}+V_w(s_{t+1})-V_{w}(s_t) δt=rt+1+Vw(st+1)−Vw(st)
9 计算价值网络的损失函数梯度 ∇ w L ( w ) = − ( r t + 1 + V w ( s t + 1 ) − V w ( s t ) ) ∇ w V w ( s t ) \nabla_{w}L(w)=-(r_{t+1}+V_w(s_{t+1})-V_{w}(s_t))\nabla_{w}V_{w}(s_t) ∇wL(w)=−(rt+1+Vw(st+1)−Vw(st))∇wVw(st)
10 通过下降上升更新价值网络参数 w ← w − α w ∇ w L ( w ) w\leftarrow w-\alpha_{w}\nabla_{w}L(w) w←w−αw∇wL(w)
11 t = t + 1 t=t+1 t=t+1