【例子】
我们目前使用的都是state value表格形式
- 优点:直观易分析
- 缺点:没法处理大的和连续的state空间或action空间
假设我有一些状态 s 1 , … , s ∣ S ∣ s_1, \ldots, s_{|\mathcal{S}|} s1,…,s∣S∣,他们的state value是 v π ( s 1 ) , … , v π ( s ∣ S ∣ ) v_\pi\left(s_1\right), \ldots, v_\pi\left(s_{|\mathcal{S}|}\right) vπ(s1),…,vπ(s∣S∣),其中 π \pi π是给定的策略。假设 ∣ S ∣ |\mathcal{S}| ∣S∣很大我们希望用一个曲线来代表这些点:
-
首先我们用直线拟合
v ^ ( s , w ) = a s + b = [ s , 1 ] ⏟ ϕ T ( s ) [ a b ] ⏟ w = ϕ T ( s ) w \hat{v}(s, w)=a s+b=\underbrace{[s, 1]}_{\phi^T(s)} \underbrace{\left[\begin{array}{c} a \\ b \end{array}\right]}_w=\phi^T(s) w v^(s,w)=as+b=ϕT(s) [s,1]w [ab]=ϕT(s)w- w w w:参数向量
- ϕ ( s ) \phi(s) ϕ(s):特征向量
- v ^ ( s , w ) \hat{v}(s, w) v^(s,w):对 w w w的一个线性的关系
- 其减少了存储的state value,但是近似是不精确的。
-
使用二阶曲线拟合
v ^ ( s , w ) = a s 2 + b s + c = [ s 2 , s , 1 ] ⏟ ϕ T ( s ) [ a b c ] ⏟ w = ϕ T ( s ) w . \hat{v}(s, w)=a s^2+b s+c=\underbrace{\left[s^2, s, 1\right]}_{\phi^T(s)} \underbrace{\left[\begin{array}{c} a \\ b \\ c \end{array}\right]}_w=\phi^T(s) w . v^(s,w)=as2+bs+c=ϕT(s) [s2,s,1]w abc =ϕT(s)w.- 增加了存储的值,但是拟合的精度提高了
-
使用更高阶的曲线拟合,使得拟合更好但是参数增加
【状态值估计算法】
✌目标函数:
目标: v π ( s ) v_\pi(s) vπ(s)是真值, v ^ ( s , w ) \hat{v}(s, w) v^(s,w)是估计的值,我们的目标就是使估计的值接近真值。(当函数形式确定的时候我们主要找到最优的 w w w参数使得其接近 v π ( s ) v_\pi(s) vπ(s))
定义目标函数:
J
(
w
)
=
E
[
(
v
π
(
S
)
−
v
^
(
S
,
w
)
)
2
]
.
J(w)=\mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right)^2\right] .
J(w)=E[(vπ(S)−v^(S,w))2].
-
我们目标找到最优的 w w w优化这个 J ( w ) J(w) J(w)
-
s获得:
-
平均分布:给每个状态求平均的权重都是一样的 1 / ∣ S ∣ 1 /|\mathcal{S}| 1/∣S∣
目标函数变成:
J ( w ) = E [ ( v π ( S ) − v ^ ( S , w ) ) 2 ] = 1 ∣ S ∣ ∑ s ∈ S ( v π ( s ) − v ^ ( s , w ) ) 2 J(w)=\mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right)^2\right]=\frac{1}{|\mathcal{S}|} \sum_{s \in \mathcal{S}}\left(v_\pi(s)-\hat{v}(s, w)\right)^2 J(w)=E[(vπ(S)−v^(S,w))2]=∣S∣1s∈S∑(vπ(s)−v^(s,w))2 -
平稳分布: { d π ( s ) } s ∈ S \left\{d_\pi(s)\right\}_{s \in \mathcal{S}} {dπ(s)}s∈S 为 s s s 的概率,其中每一个值 d π ( s ) ≥ 0 d_\pi(s) \geq 0 dπ(s)≥0 并且 ∑ s ∈ S d π ( s ) = 1 \sum_{s \in \mathcal{S}} d_\pi(s)=1 ∑s∈Sdπ(s)=1
目标函数变成:
J ( w ) = E [ ( v π ( S ) − v ^ ( S , w ) ) 2 ] = ∑ s ∈ S d π ( s ) ( v π ( s ) − v ^ ( s , w ) ) 2 J(w)=\mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right)^2\right]=\sum_{s \in \mathcal{S}} d_\pi(s)\left(v_\pi(s)-\hat{v}(s, w)\right)^2 J(w)=E[(vπ(S)−v^(S,w))2]=s∈S∑dπ(s)(vπ(s)−v^(s,w))2
-
✌优化算法:
为了最小化
J
(
w
)
J(w)
J(w),我们可以使用梯度下降法:
w
k
+
1
=
w
k
−
α
k
∇
w
J
(
w
k
)
w_{k+1}=w_k-\alpha_k \nabla_w J\left(w_k\right)
wk+1=wk−αk∇wJ(wk)
计算:
∇
w
J
(
w
)
=
∇
w
E
[
(
v
π
(
S
)
−
v
^
(
S
,
w
)
)
2
]
=
E
[
∇
w
(
v
π
(
S
)
−
v
^
(
S
,
w
)
)
2
]
=
2
E
[
(
v
π
(
S
)
−
v
^
(
S
,
w
)
)
(
−
∇
w
v
^
(
S
,
w
)
)
]
=
−
2
E
[
(
v
π
(
S
)
−
v
^
(
S
,
w
)
)
∇
w
v
^
(
S
,
w
)
]
\begin{aligned} \nabla_w J(w) & =\nabla_w \mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right)^2\right] \\ & =\mathbb{E}\left[\nabla_w\left(v_\pi(S)-\hat{v}(S, w)\right)^2\right] \\ & =2 \mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right)\left(-\nabla_w \hat{v}(S, w)\right)\right] \\ & =-2 \mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right) \nabla_w \hat{v}(S, w)\right] \end{aligned}
∇wJ(w)=∇wE[(vπ(S)−v^(S,w))2]=E[∇w(vπ(S)−v^(S,w))2]=2E[(vπ(S)−v^(S,w))(−∇wv^(S,w))]=−2E[(vπ(S)−v^(S,w))∇wv^(S,w)]
这里我们需要计算一个均方,我们可以使用随机梯度进行替代:
w
t
+
1
=
w
t
+
α
t
(
v
π
(
s
t
)
−
v
^
(
s
t
,
w
t
)
)
∇
w
v
^
(
s
t
,
w
t
)
,
w_{t+1}=w_t+\alpha_t\left(v_\pi\left(s_t\right)-\hat{v}\left(s_t, w_t\right)\right) \nabla_w \hat{v}\left(s_t, w_t\right),
wt+1=wt+αt(vπ(st)−v^(st,wt))∇wv^(st,wt),
这里面涉及到
v
π
(
s
t
)
v_\pi(s_t)
vπ(st),我们有两种方法进行代替:
-
蒙特卡洛方法:用 g t g_t gt作为 v π ( s t ) v_\pi(s_t) vπ(st)的估计值
w t + 1 = w t + α t ( g t − v ^ ( s t , w t ) ) ∇ w v ^ ( s t , w t ) . w_{t+1}=w_t+\alpha_t\left(g_t-\hat{v}\left(s_t, w_t\right)\right) \nabla_w \hat{v}\left(s_t, w_t\right) . wt+1=wt+αt(gt−v^(st,wt))∇wv^(st,wt). -
TD learning:
w t + 1 = w t + α t [ r t + 1 + γ v ^ ( s t + 1 , w t ) − v ^ ( s t , w t ) ] ∇ w v ^ ( s t , w t ) w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \hat{v}\left(s_{t+1}, w_t\right)-\hat{v}\left(s_t, w_t\right)\right] \nabla_w \hat{v}\left(s_t, w_t\right) wt+1=wt+αt[rt+1+γv^(st+1,wt)−v^(st,wt)]∇wv^(st,wt)
✌函数的选择:
v ^ ( s , w ) ⟶ V π \hat{v}(s, w) \longrightarrow V_\pi v^(s,w)⟶Vπ
-
第一种:线性函数
v ^ ( s , w ) = ϕ T ( s ) w \hat{v}(s, w)=\phi^T(s) w v^(s,w)=ϕT(s)w-
由于线性函数我们有了其梯度 ∇ w v ^ ( s , w ) = ϕ ( s ) \nabla_w \hat{v}(s, w)=\phi(s) ∇wv^(s,w)=ϕ(s)
-
将其带到TD learning中得到:也叫做TD-Linear
w t + 1 = w t + α t [ r t + 1 + γ v ^ ( s t + 1 , w t ) − v ^ ( s t , w t ) ] ∇ w v ^ ( s t , w t ) w t + 1 = w t + α t [ r t + 1 + γ ϕ T ( s t + 1 ) w t − ϕ T ( s t ) w t ] ϕ ( s t ) \begin{aligned} &w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \hat{v}\left(s_{t+1}, w_t\right)-\hat{v}\left(s_t, w_t\right)\right] \nabla_w \hat{v}\left(s_t, w_t\right)\\ &w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \phi^T\left(s_{t+1}\right) w_t-\phi^T\left(s_t\right) w_t\right] \phi\left(s_t\right) \end{aligned} wt+1=wt+αt[rt+1+γv^(st+1,wt)−v^(st,wt)]∇wv^(st,wt)wt+1=wt+αt[rt+1+γϕT(st+1)wt−ϕT(st)wt]ϕ(st)- 劣势:需要有很好的特征向量
- 优点:理论好分析,具有比较强的表征能力
-
-
第二种:神经网络逼近函数
✌例子说明:
-
给定策略对任何的 s , a s,a s,a 其 π ( a ∣ s ) = 0.2 \pi(a \mid s)=0.2 π(a∣s)=0.2
-
目标:估计state values在这个策略
-
参数: r forbidden = r boundary = − 1 , r target = 1 r_{\text {forbidden }}=r_{\text {boundary }}=-1, r_{\text {target }}=1 rforbidden =rboundary =−1,rtarget =1, γ = 0.9 \gamma=0.9 γ=0.9
-
真实结果:
-
近似:我们有500个episodes,每个episodes有500步,每个episode出发是随机选择的且服从均匀分布
通过TD-Table绘制:
TD-Linear:
-
特征向量选择:
ϕ ( s ) = [ 1 x y ] ∈ R 3 \phi(s)=\left[\begin{array}{l} 1 \\ x \\ y \end{array}\right] \in \mathbb{R}^3 ϕ(s)= 1xy ∈R3 -
近似状态值:
v ^ ( s , w ) = ϕ T ( s ) w = [ 1 , x , y ] [ w 1 w 2 w 3 ] = w 1 + w 2 x + w 3 y \hat{v}(s, w)=\phi^T(s) w=[1, x, y]\left[\begin{array}{l} w_1 \\ w_2 \\ w_3 \end{array}\right]=w_1+w_2 x+w_3 y v^(s,w)=ϕT(s)w=[1,x,y] w1w2w3 =w1+w2x+w3y -
通过TD-Linear进行估计:
最终收敛了但是最终值不是0,我们后续可以通过高阶进行拟合
High-order feater vectors:
-
特征向量选择:
ϕ ( s ) = [ 1 , x , y , x 2 , y 2 , x y ] T ∈ R 6 \phi(s)=\left[1, x, y, x^2, y^2, x y\right]^T \in \mathbb{R}^6 ϕ(s)=[1,x,y,x2,y2,xy]T∈R6 -
近似状态值:
v ^ ( s , w ) = ϕ T ( s ) w = w 1 + w 2 x + w 3 y + w 4 x 2 + w 5 y 2 + w 6 x y \hat{v}(s, w)=\phi^T(s) w=w_1+w_2 x+w_3 y+w_4 x^2+w_5 y^2+w_6 x y v^(s,w)=ϕT(s)w=w1+w2x+w3y+w4x2+w5y2+w6xy -
拟合结果:
-
✌总结:
-
从目标函数出发,为真实的state value和估计的state value的加权平均:
J ( w ) = E [ ( v π ( S ) − v ^ ( S , w ) ) 2 ] J(w)=\mathbb{E}\left[\left(v_\pi(S)-\hat{v}(S, w)\right)^2\right] J(w)=E[(vπ(S)−v^(S,w))2] -
对算法使用梯度下降进行优化:
w t + 1 = w t + α t ( v π ( s t ) − v ^ ( s t , w t ) ) ∇ w v ^ ( s t , w t ) w_{t+1}=w_t+\alpha_t\left(v_\pi\left(s_t\right)-\hat{v}\left(s_t, w_t\right)\right) \nabla_w \hat{v}\left(s_t, w_t\right) wt+1=wt+αt(vπ(st)−v^(st,wt))∇wv^(st,wt) -
由于 v π ( s t ) v_\pi(s_t) vπ(st)不知道,对其进行替代(这里不严谨可能会有问题):
w t + 1 = w t + α t [ r t + 1 + γ v ^ ( s t + 1 , w t ) − v ^ ( s t , w t ) ] ∇ w v ^ ( s t , w t ) w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \hat{v}\left(s_{t+1}, w_t\right)-\hat{v}\left(s_t, w_t\right)\right] \nabla_w \hat{v}\left(s_t, w_t\right) wt+1=wt+αt[rt+1+γv^(st+1,wt)−v^(st,wt)]∇wv^(st,wt)
【Sarsa 函数近似】
w t + 1 = w t + α t [ r t + 1 + γ q ^ ( s t + 1 , a t + 1 , w t ) − q ^ ( s t , a t , w t ) ] ∇ w q ^ ( s t , a t , w t ) w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \hat{q}\left(s_{t+1}, a_{t+1}, w_t\right)-\hat{q}\left(s_t, a_t, w_t\right)\right] \nabla_w \hat{q}\left(s_t, a_t, w_t\right) wt+1=wt+αt[rt+1+γq^(st+1,at+1,wt)−q^(st,at,wt)]∇wq^(st,at,wt)
🤞Sarsa 函数伪代码:
-
对于每个episode,如果当前 s t s_t st不是target state:
-
依据 π t ( s t ) \pi_t\left(s_t\right) πt(st)执行 a t a_t at,生成 r t + 1 , s t + 1 r_{t+1}, s_{t+1} rt+1,st+1然后依据 π t ( s t + 1 ) \pi_t\left(s_{t+1}\right) πt(st+1)执行 a t + 1 a_{t+1} at+1
-
value update:
w t + 1 = w t + α t [ r t + 1 + γ q ^ ( s t + 1 , a t + 1 , w t ) − q ^ ( s t , a t , w t ) ] ∇ w q ^ ( s t , a t , w t ) \begin{aligned} & w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \hat{q}\left(s_{t+1}, a_{t+1}, w_t\right)-\hat{q}\left(s_t, a_t, w_t\right)\right] \nabla_w \hat{q}\left(s_t, a_t, w_t\right) \end{aligned} wt+1=wt+αt[rt+1+γq^(st+1,at+1,wt)−q^(st,at,wt)]∇wq^(st,at,wt) -
policy update:
π t + 1 ( a ∣ s t ) = 1 − ε ∣ A ( s ) ∣ ( ∣ A ( s ) ∣ − 1 ) if a = arg max a ∈ A ( s t ) q ^ ( s t , a , w t + 1 ) π t + 1 ( a ∣ s t ) = ε ∣ A ( s ) ∣ otherwise \begin{aligned} & \pi_{t+1}\left(a \mid s_t\right)=1-\frac{\varepsilon}{|\mathcal{A}(s)|}(|\mathcal{A}(s)|-1) \text { if } a=\arg \max _{a \in \mathcal{A}\left(s_t\right)} \hat{q}\left(s_t, a, w_{t+1}\right) \\ & \pi_{t+1}\left(a \mid s_t\right)=\frac{\varepsilon}{|\mathcal{A}(s)|} \text { otherwise } \end{aligned} πt+1(a∣st)=1−∣A(s)∣ε(∣A(s)∣−1) if a=arga∈A(st)maxq^(st,a,wt+1)πt+1(a∣st)=∣A(s)∣ε otherwise
-
【Q-learning 函数近似】
w t + 1 = w t + α t [ r t + 1 + γ max a ∈ A ( s t + 1 ) q ^ ( s t + 1 , a , w t ) − q ^ ( s t , a t , w t ) ] ∇ w q ^ ( s t , a t , w t ) w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \max _{a \in \mathcal{A}\left(s_{t+1}\right)} \hat{q}\left(s_{t+1}, a, w_t\right)-\hat{q}\left(s_t, a_t, w_t\right)\right] \nabla_w \hat{q}\left(s_t, a_t, w_t\right) wt+1=wt+αt[rt+1+γa∈A(st+1)maxq^(st+1,a,wt)−q^(st,at,wt)]∇wq^(st,at,wt)
🤞Q-learning 函数伪代码【on-policy】:
-
对每个episode,如果当前 s t s_t st不是目标状态:
-
依据 π t ( s t ) \pi_t\left(s_t\right) πt(st)执行 a t a_t at,生成 r t + 1 , s t + 1 r_{t+1}, s_{t+1} rt+1,st+1然后生成 r t + 1 , s t + 1 r_{t+1},s_{t+1} rt+1,st+1
-
value update:
w t + 1 = w t + α t [ r t + 1 + γ max a ∈ A ( s t + 1 ) q ^ ( s t + 1 , a , w t ) − q ^ ( s t , a t , w t ) ] ∇ w q ^ ( s t , a t , w t ) \begin{aligned} & w_{t+1}=w_t+\alpha_t\left[r_{t+1}+\gamma \max _{a \in \mathcal{A}\left(s_{t+1}\right)} \hat{q}\left(s_{t+1}, a, w_t\right)-\hat{q}\left(s_t, a_t, w_t\right)\right] \nabla_w \hat{q}\left(s_t, a_t, w_t\right) \end{aligned} wt+1=wt+αt[rt+1+γa∈A(st+1)maxq^(st+1,a,wt)−q^(st,at,wt)]∇wq^(st,at,wt) -
policy update:
π t + 1 ( a ∣ s t ) = 1 − ε ∣ A ( s ) ∣ ( ∣ A ( s ) ∣ − 1 ) if a = arg max a ∈ A ( s t ) q ^ ( s t , a , w t + 1 ) π t + 1 ( a ∣ s t ) = ε ∣ A ( s ) ∣ otherwise \begin{aligned} & \pi_{t+1}\left(a \mid s_t\right)=1-\frac{\varepsilon}{|\mathcal{A}(s)|}(|\mathcal{A}(s)|-1) \text { if } a=\arg \max _{a \in \mathcal{A}\left(s_t\right)} \hat{q}\left(s_t, a, w_{t+1}\right) \\ & \pi_{t+1}\left(a \mid s_t\right)=\frac{\varepsilon}{|\mathcal{A}(s)|} \text { otherwise } \end{aligned} πt+1(a∣st)=1−∣A(s)∣ε(∣A(s)∣−1) if a=arga∈A(st)maxq^(st,a,wt+1)πt+1(a∣st)=∣A(s)∣ε otherwise
【Deep Q-learning】
Deep Q-learning也叫做deep Q-network(DQN):
目标函数:
J
(
w
)
=
E
[
(
R
+
γ
max
a
∈
A
(
S
′
)
q
^
(
S
′
,
a
,
w
)
−
q
^
(
S
,
A
,
w
)
)
2
]
J(w)=\mathbb{E}\left[\left(R+\gamma \max _{a \in \mathcal{A}\left(S^{\prime}\right)} \hat{q}\left(S^{\prime}, a, w\right)-\hat{q}(S, A, w)\right)^2\right]
J(w)=E[(R+γa∈A(S′)maxq^(S′,a,w)−q^(S,A,w))2]
梯度计算: 假设:
y
≐
R
+
γ
max
a
∈
A
(
S
′
)
q
^
(
S
′
,
a
,
w
)
y \doteq R+\gamma \max _{a \in \mathcal{A}\left(S^{\prime}\right)} \hat{q}\left(S^{\prime}, a, w\right)
y≐R+γa∈A(S′)maxq^(S′,a,w)
引入了两个函数:
- 第一个网络(main network): q ^ ( s , a , w ) \hat{q}(s, a, w) q^(s,a,w),参数是 w w w
- 第二个网络(target network): q ^ ( s , a , w T ) \hat{q}\left(s, a, w_T\right) q^(s,a,wT),参数是 w T w_T wT
J = E [ ( R + γ max a ∈ A ( S ′ ) q ^ ( S ′ , a , w T ) − q ^ ( S , A , w ) ) 2 ] J=\mathbb{E}\left[\left(R+\gamma \max _{a \in \mathcal{A}\left(S^{\prime}\right)} \hat{q}\left(S^{\prime}, a, w_T\right)-\hat{q}(S, A, w)\right)^2\right] J=E[(R+γa∈A(S′)maxq^(S′,a,wT)−q^(S,A,w))2]
基本思想:刚开始两个是一样的,首先保持 w T w_T wT不动,然后更新 w w w,等更新一段时间 w w w后将 w w w赋值给 w T w_T wT一直往复直到达到最优
于是它的梯度为:
🤞经验回放:
我们在收集顺序的时候是有一定顺序的,我们将他们放到一个集合中 B ≐ { ( s , a , r , s ′ ) } \mathcal{B} \doteq\left\{\left(s, a, r, s^{\prime}\right)\right\} B≐{(s,a,r,s′)},每次我们在训练神经网络的时候我们从回放缓冲器中抽取一小批随机样本(均匀分布,每个概率相等),这就叫做经验回放,因为经验已经在那了。
问题:我们在拿的时候为什么服从均匀分布?
回答:
J
=
E
[
(
R
+
γ
max
a
∈
A
(
S
′
)
q
^
(
S
′
,
a
,
w
)
−
q
^
(
S
,
A
,
w
)
)
2
]
J=\mathbb{E}\left[\left(R+\gamma \max _{a \in \mathcal{A}\left(S^{\prime}\right)} \hat{q}\left(S^{\prime}, a, w\right)-\hat{q}(S, A, w)\right)^2\right]
J=E[(R+γa∈A(S′)maxq^(S′,a,w)−q^(S,A,w))2]
- ( S , A ) ∼ d : ( S , A ) (S, A) \sim d:(S, A) (S,A)∼d:(S,A)是一个索引,随机变量
- R ∼ p ( R ∣ S , A ) , S ′ ∼ p ( S ′ ∣ S , A ) : R R \sim p(R \mid S, A), S^{\prime} \sim p\left(S^{\prime} \mid S, A\right): R R∼p(R∣S,A),S′∼p(S′∣S,A):R 和 S S S由系统模型决定
使用均匀分布可以打破不同数据之间的关联。
🤞Deep Q-learning 函数伪代码【off-policy】:
- 假设有策略
π
b
\pi_b
πb,产生很多buffer
B
=
{
(
s
,
a
,
r
,
s
′
)
}
\mathcal{B}=\left\{\left(s, a, r, s^{\prime}\right)\right\}
B={(s,a,r,s′)}
- 从replay buff中随机获得一些采样
- 对采样做处理计算目标值 y T = r + γ max a ∈ A ( s ′ ) q ^ ( s ′ , a , w T ) y_T=r + \gamma \max _{a \in \mathcal{A}\left(s^{\prime}\right)} \hat{q}\left(s^{\prime}, a, w_T\right) yT=r+γmaxa∈A(s′)q^(s′,a,wT)
- 更新main network来最小化 ( y T − q ^ ( s , a , w ) ) 2 \left(y_T-\hat{q}(s, a, w)\right)^2 (yT−q^(s,a,w))2
- 当C次后 w T = w w_T=w wT=w
【总结】
- 首先给出曲线拟合的例子,提出function
- 然后揭示value function approximation思想
- 最后介绍了两个算法,把Sarsa、Q-learning相结合
- 最后提出了Deep Q-learning