作为一个新手,写这个强化学习-基础知识专栏是想和大家分享一下自己学习强化学习的学习历程,希望对大家能有所帮助。这个系列后面会不断更新,希望自己在2021年能保证平均每日一更的更新速度,主要是介绍强化学习的基础知识,后面也会更新强化学习的论文阅读专栏。本来是想每一篇多更新一点内容的,后面发现大家上CSDN主要是来提问的,就把很多拆分开来了(而且这样每天任务量也小一点哈哈哈哈偷懒大法)。但是我还是希望知识点能成系统,所以我在目录里面都好按章节系统地写的,而且在github上写成了书籍的形式,如果大家觉得有帮助,希望从头看的话欢迎关注我的github啊,谢谢大家!另外我还会分享深度学习-基础知识专栏以及深度学习-论文阅读专栏,很早以前就和小伙伴们花了很多精力写的,如果有对深度学习感兴趣的小伙伴也欢迎大家关注啊。大家一起互相学习啊!可能会有很多错漏,希望大家批评指正!不要高估一年的努力,也不要低估十年的积累,与君共勉!
REINFOECE
先回顾一下强化学习的目标,最大化累计奖励:
θ ⋆ = arg max θ E τ ∼ p θ ( τ ) [ ∑ t r ( s t , a t ) ] \theta^{\star}=\arg \max _{\theta} E_{\tau \sim p_{\theta}(\tau)}\left[\sum_{t} r\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right)\right] θ⋆=argθmaxEτ∼pθ(τ)[t∑r(st,at)]
在有限长度轨迹的情况下:
θ ⋆ = arg max θ E ( s , a ) ∼ p θ ( s , a ) [ r ( s , a ) ] \theta^{\star}=\arg \max _{\theta} E_{(\mathbf{s}, \mathbf{a}) \sim p_{\theta}(\mathbf{s}, \mathbf{a})}[r(\mathbf{s}, \mathbf{a})] θ⋆=argθmaxE(s,a)∼pθ(s,a)[r(s,a)]
在无限长度轨迹的情况下:
θ ⋆ = arg max θ ∑ t = 1 T E ( s t , a t ) ∼ p θ ( s t , a t ) [ r ( s t , a t ) ] \theta^{\star}=\arg \max _{\theta} \sum_{t=1}^{T} E_{\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right) \sim p_{\theta}\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right)}\left[r\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right)\right] θ⋆=argθmaxt=1∑TE(st,at)∼pθ(st,at)[r(st,at)]
我们令目标函数:
J ( θ ) = E τ ∼ p θ ( τ ) [ ∑ t r ( s t , a t ) ] ≈ 1 N ∑ i ∑ t r ( s i , t , a i , t ) J(\theta)=E_{\tau \sim p_{\theta}(\tau)}\left[\sum_{t} r\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right)\right] \approx \frac{1}{N} \sum_{i} \sum_{t} r\left(\mathbf{s}_{i, t}, \mathbf{a}_{i, t}\right) J(θ)=Eτ∼pθ(τ)[t∑r(st,at)]≈N1i∑t∑r(si,t,ai,t),令第一项中括号中的为 r ( τ ) r(\tau) r(τ),
J ( θ ) = E τ ∼ p θ ( τ ) [ r ( τ ) ] ⏟ ↪ = ∫ p θ ( τ ) r ( τ ) d τ ∑ t = 1 T r ( s t , a t ) \begin{array}{c} J(\theta)=E_{\tau \sim p_{\theta}(\tau)}[\underbrace{r(\tau)]}{\hookrightarrow}=\int p_{\theta}(\tau) r(\tau) d \tau \\ \sum_{t=1}^{T} r\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right) \end{array} J(θ)=Eτ∼pθ(τ)[ r(τ)]↪=∫pθ(τ)r(τ)dτ∑t=1Tr(st,at)
对它直接进行梯度计算:
∇
θ
J
(
θ
)
=
∫
∇
θ
p
θ
(
τ
)
r
(
τ
)
d
τ
=
∫
p
θ
(
τ
)
∇
θ
log
p
θ
(
τ
)
r
(
τ
)
d
τ
=
E
τ
∼
p
θ
(
τ
)
[
∇
θ
log
p
θ
(
τ
)
r
(
τ
)
]
\nabla_{\theta} J(\theta)=\int \nabla_{\theta} p_{\theta}(\tau) r(\tau) d \tau=\int p_{\theta}(\tau) \nabla_{\theta} \log p_{\theta}(\tau) r(\tau) d \tau=E_{\tau \sim p_{\theta}(\tau)}\left[\nabla_{\theta} \log p_{\theta}(\tau) r(\tau)\right]
∇θJ(θ)=∫∇θpθ(τ)r(τ)dτ=∫pθ(τ)∇θlogpθ(τ)r(τ)dτ=Eτ∼pθ(τ)[∇θlogpθ(τ)r(τ)]
在这里的第二个等号中,使用了一个恒等式:
p θ ( τ ) ∇ θ log p θ ( τ ) = p θ ( τ ) ∇ θ p θ ( τ ) p θ ( τ ) = ∇ θ p θ ( τ ) p_{\theta}(\tau) \nabla_{\theta} \log p_{\theta}(\tau)=p_{\theta}(\tau) \frac{\nabla_{\theta} p_{\theta}(\tau)}{p_{\theta}(\tau)}=\nabla_{\theta} p_{\theta}(\tau) pθ(τ)∇θlogpθ(τ)=pθ(τ)pθ(τ)∇θpθ(τ)=∇θpθ(τ)
而 p θ ( s 1 , a 1 , … , s T , a T ) ⏟ p θ ( τ ) = p ( s 1 ) ∏ t = 1 π θ ( a t ∣ s t ) p ( s t + 1 ∣ s t , a t ) \underbrace{p_{\theta}\left(\mathbf{s}_{1}, \mathbf{a}_{1}, \ldots, \mathbf{s}_{T}, \mathbf{a}_{T}\right)}_{p_{\theta}(\tau)}=p\left(\mathbf{s}_{1}\right) \prod_{t=1} \pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right) p\left(\mathbf{s}_{t+1} \mid \mathbf{s}_{t}, \mathbf{a}_{t}\right) pθ(τ) pθ(s1,a1,…,sT,aT)=p(s1)t=1∏πθ(at∣st)p(st+1∣st,at)
两边取log:
log p θ ( τ ) = log p ( s 1 ) + ∑ t = 1 T log π θ ( a t ∣ s t ) + log p ( s t + 1 ∣ s t , a t ) \log p_{\theta}(\tau)=\log p\left(\mathbf{s}_{1}\right)+\sum_{t=1}^{T} \log \pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right)+\log p\left(\mathbf{s}_{t+1} \mid \mathbf{s}_{t}, \mathbf{a}_{t}\right) logpθ(τ)=logp(s1)+t=1∑Tlogπθ(at∣st)+logp(st+1∣st,at)
所以:
∇ θ log p θ ( τ ) r ( τ ) \nabla_{\theta} \log p_{\theta}(\tau) r(\tau) ∇θlogpθ(τ)r(τ)= ∇ θ [ log p̸ ( s 1 ) + ∑ t = 1 T log π θ ( a t ∣ s t ) + log p ( s t + 1 s t ^ , a t ) ] \nabla_{\theta}\left[\log \not p\left(\mathbf{s}_{1}\right)+\sum_{t=1}^{T} \log \pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right)+\log p\left(\mathbf{s}_{t+1} \widehat{\mathbf{s}_{t}}, \mathbf{a}_{t}\right)\right] ∇θ[logp(s1)+t=1∑Tlogπθ(at∣st)+logp(st+1st ,at)]
第一项和第三项都和 θ \theta θ没有关系,所以
∇ θ J ( θ ) = E τ ∼ p θ ( τ ) [ ∇ θ log p θ ( τ ) r ( τ ) ] \nabla_{\theta} J(\theta)=E_{\tau \sim p_{\theta}(\tau)}\left[\nabla_{\theta} \log p_{\theta}(\tau) r(\tau)\right] ∇θJ(θ)=Eτ∼pθ(τ)[∇θlogpθ(τ)r(τ)] = E τ ∼ p θ ( τ ) [ ( ∑ t = 1 T ∇ θ log π θ ( a t ∣ s t ) ) ( ∑ t = 1 T r ( s t , a t ) ) ] E_{\tau \sim p_{\theta}(\tau)}\left[\left(\sum_{t=1}^{T} \nabla_{\theta} \log \pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right)\right)\left(\sum_{t=1}^{T} r\left(\mathbf{s}_{t}, \mathbf{a}_{t}\right)\right)\right] Eτ∼pθ(τ)[(t=1∑T∇θlogπθ(at∣st))(t=1∑Tr(st,at))]
≈ 1 N ∑ i = 1 N ( ∑ t = 1 T ∇ θ log π θ ( a i , t ∣ s i , t ) ) ( ∑ t = 1 T r ( s i , t , a i , t ) ) \approx \frac{1}{N} \sum_{i=1}^{N}\left(\sum_{t=1}^{T} \nabla_{\theta} \log \pi_{\theta}\left(\mathbf{a}_{i, t} \mid \mathbf{s}_{i, t}\right)\right)\left(\sum_{t=1}^{T} r\left(\mathbf{s}_{i, t}, \mathbf{a}_{i, t}\right)\right) ≈N1i=1∑N(t=1∑T∇θlogπθ(ai,t∣si,t))(t=1∑Tr(si,t,ai,t))
终于!这两项都可以通过采样然后通过样本算出来了! 然后根据强化学习算法的经典的流程图:
我们就可以得到REINFORCE 算法,也就是根据当前策略进行采样,采样之后算出梯度,算出梯度之后对策略进行更新,然后继续采样,如此往复:
值得注意的是,在上述推导过程中,我们并没有用到Markov property,所以REINFORCE对partially observed MDPs而言也是可以使用的,因为它针对的是trajectory。
在离散动作空间下,可以跟分类任务一样,使用多个输出结点作为不同action的选择。
而在连续动作空间下,往往会使用Gaussian polices进行action选择:通过神经网络对state 进行处理,得到高斯分布的均值(方差可以得到也可以指定),再通过这个分布进行抽样。
π
θ
(
a
t
∣
s
t
)
=
N
(
f
neural network
(
s
t
)
;
Σ
)
\pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right)=\mathcal{N}\left(f_{\text {neural network }}\left(\mathbf{s}_{t}\right) ; \Sigma\right)
πθ(at∣st)=N(fneural network (st);Σ)
而由于高斯分布梯度易求,所以参数优化也比较方便
log
π
θ
(
a
t
∣
s
t
)
=
−
1
2
∥
f
(
s
t
)
−
a
t
∥
Σ
2
+
const
∇
θ
log
π
θ
(
a
t
∣
s
t
)
=
−
1
2
Σ
−
1
(
f
(
s
t
)
−
a
t
)
d
f
d
θ
\begin{array}{l} \log \pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right)=-\frac{1}{2}\left\|f\left(\mathbf{s}_{t}\right)-\mathbf{a}_{t}\right\|_{\Sigma}^{2}+\text { const } \\ \nabla_{\theta} \log \pi_{\theta}\left(\mathbf{a}_{t} \mid \mathbf{s}_{t}\right)=-\frac{1}{2} \Sigma^{-1}\left(f\left(\mathbf{s}_{t}\right)-\mathbf{a}_{t}\right) \frac{d f}{d \theta} \end{array}
logπθ(at∣st)=−21∥f(st)−at∥Σ2+ const ∇θlogπθ(at∣st)=−21Σ−1(f(st)−at)dθdf
上一篇:强化学习的学习之路(三十)_2021-01-30: Policy Optimazation 简介
下一篇:强化学习的学习之路(三十二)_2021-02-01:Differences between RL and Imitation learning(强化学习和模仿学习的差别)