Try   HackMD

Reinforcement Learning | Policy Gradient

上一篇筆記:
https://hackmd.io/@tsen159/RLNote
內容包含 RL 的介紹、Markov decision process、model-based evaluation and control。


Model-free RL 演算法可根據我們想要最佳化的目標,分為 value-based、policy-based 和混雜的 actor-critic:

  • Valued-based:一種基於 value function 的方法,試圖直接學習最優 policy 的 value function,而不是學習最優 policy 本身
  • Policy-based:直接學習 policy,相較於 value-based 更適合用在高維或連續的 action spaces,且可以學習 stochastic policy
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Policy Optimization

我們將 policy-based RL 視為一個 optimization problem。考慮

πθ(s,a) 並定義
Vπθ(μ):=Es0μ[Vπθ(s0)]

目標是希望可以找到讓
Vπθ
最大的 policy parameter
θ
,即找到
argmaxθVπθ(μ)

μ 表示
s0
的分布,
Es0μ
:可以理解為對所有可能的
s0
取 weighted sum

Policy optimization 包括 gradient-based 與 gradient-free,gradient-free 的方法包含如 cross-entropy method、hill climbing 等等;而 gradient-based 的方法通常可以有更好的效率,其中 policy gradient 即為一種 gradient-based 的方法。

一個進行 policy optimization 的例子如下:

  1. Parametrize the policy by a neural network.
    Sπθ()action distribution
  2. update
    πθ
    iteratively, e.g., by gradient ascent
    θθ+αθVπθ(μ)

Policy 可以如何參數化呢?一些 parametric policies 的例子:

Discrete action space

  • Softmax policies:
    πθ(a|s)=exp(θs,a)aAexp(θs,a)

對每一個 (s, a) pair 都給定一個 parameter

θ

  • Linear softmax policies:
    πθ(a|s)=exp(θTϕs,a)aAexp(θTϕs,a)

對每一個 (s, a) pair 設計一個 feature

ϕ

  • Neural softmax policies:
    πθ(a|s)=exp(fθ(s,a))aAexp(fθ(s,a))

f 為一 neural network

Continuous action space

  • Gaussian policies:
    aN(fθ(s),σ2)

Policy Gradient

定義

V(θ)=Vπθ(μ),則 policy gradient algorithms 透過 gradient ascent 尋求
V(θ)
的最大值,更新參數的方式為
θθ+αθV(θ)

其中

θV(θ) 即是 policy gradient
θV(θ)=[V(θ)θ1V(θ)θ2V(θ)θn]

定義:

Sample return

R(τ) along a trajectory
τ=(s0,a0,r1,...)

R(τ):=t=0γtrt+1(τ)

我們便可以改寫先前定義的 value function 為:

V(θ)=Vπθ(μ)=EτPμπθ[R(τ)]=τR(τ)Pμπθ(τ)
其中
Pμπθ
τ
的分佈,
Pμπθ(τ)
表執行 policy
πθ
時某個 trajectory
τ
的機率。

因此 policy gradient 為

θV(θ)=τR(τ)θPμπθ(τ)=τR(τ)(Pμπθ(τ)θlogPμπθ(τ))    //by chain rule=τR(τ)(Pμπθ(τ)θlog(μ(s0)initial state dist.πθ(a0|s0)P(s1|s0,a0)πθ(a1|s1)...))=τR(τ)(Pμπθ(τ)θ(logμ(s0)+t=0logπθ(at|st)+logP(st+1|st,at)))=τR(τ)(Pμπθ(τ)t=0θlogπθ(at|st))    //derivative =0 if independent from θ=EτPμπθ[R(τ)t=0θlogπθ(at|st)]

By chian rule,

xlogf(x)=1f(x)xf(x)

這裡

θlogπθ(a|s) 稱為 score function。

我們得出的結果

θVπθ(μ)=EτPμπθ[R(τ)t=0θlogπθ(at|st)]

即是 policy gradient 的其中一種計算方法,稱為 total reward method。

Total Reward Policy Gradient (P1)

θVπθ(μ)=EτPμπθ[R(τ)t=0θlogπθ(at|st)]

這個式子直觀而言,我們可以想成是對於某個 trajectory

τ,如果其 return
R(τ)
相對於其他的 trajectories 較大,則它對於 gradient 的貢獻也會較大。


Policy gradient 也有其他的表示形式:

REINFORCE (P2)

θVπθ(μ)=EτPμπθ[t=0γtQπθ(st,at)θlogπθ(at|st)]

proof:
Recall that

Vπθ(s0)=a0Aπθ(a0|s0)Qπθ(s0,a0)
therefore,
θVπθ(s0)=a0θ(πθ(a0|s0)Qπθ(s0,a0))=a0θ(πθ(a0|s0))Qπθ(s0,a0)(1)+a0πθ(a0|s0)θQπθ(s0,a0)(2)   //by product rule(1)=a0πθ(a0|s0)θ(logπθ(a0|s0))Qπθ(s0,a0)   //by chain rule=EτPs0πθ[θlogπθ(a0|s0)Qπθ(s0,a0)](2)=a0πθ(a0|s0)θQπθ(s0,a0)=a0πθ(a0|s0)θ(r(s0,a0)+γs1P(s1|s0,a0)Vπθ(s1))=a0πθ(a0|s0)(γs1P(s1|s0,a0)θVπθ(s1))=EτPs0πθ[γθVπθ(s1)]=>θVπθ(s0)=EτPs0πθ[θlogπθ(a0|s0)Qπθ(s0,a0)]+EτPs0πθ[γθVπθ(s1)]

which is a recursion.
By solving

θVπθ(s0),θVπθ(s1),..., we can get
θVπθ(μ)=EτPμπθ[t=0γtQπθ(st,at)θlogπθ(at|st)]


定義:

Discounted state visitation distribution (occupancy measure)

ds0π(s):=(1γ)t=0γtP(st=s|so,π)

表示在策略

π 下,從
s0
出發並訪問
s
的機率。整個公式可以理解為:遵從
π
並從
s0
出發,在所有可能的 timestep 下轉移到
s
的機率加權和。

則訪問某狀態

s 的機率可以表示為:
dμπ(s):=Es0μ[ds0π(s)]

我們可以將 policy gradient 表示成:

Q-value and discounted state visitation (P3)

θVπθ(μ)=11γEsdμπθEaπθ(|s)[Qπθ(s,a)θlogπθ(a|s)]

此時 policy gradient 便和 trajectory 無關,只考慮每一個

(s,a) pair。

統整以上提及的各種 Policy Gradient 表示法:

Expression of Policy Gradient (Policy Gradient Theorem)

Total Reward

θVπθ(μ)=EτPμπθ[R(τ)t=0θlogπθ(at|st)]

REINFORCE

θVπθ(μ)=EτPμπθ[t=0γtQπθ(st,at)θlogπθ(at|st)]

Q-value and discounted state visitation

θVπθ(μ)=11γEsdμπθEaπθ(|s)[Qπθ(s,a)θlogπθ(a|s)]

Score function

先前討論 policy gradient 時,我們直接假設了

πθ(a|s) 可微。以下介紹一些可微的 policy classes 以及它們的 score function:

Linear Softmax Policy

每個 action 的機率和 exponentiated weight 成比例。

πθ(a|s)=exp(θTϕs,a)aAexp(θTϕs,a)
其 score function 為
θlogπθ(a|s)=ϕ(s,a)Eaπθ(|s)[ϕ(s,a)]

Gaussian Policy

每個 action 是從一個 Gaussian distribution 抽取出來的:

aN(fθ(s),σ2)

Mean 是和 state features 的線性組合

f(s)=ϕ(s)Tθ,variance 可以是固定的
σ2
,也可以參數化。

其 score function 為

θlogπθ(a|s)=(aμθ(s))ϕ(s)σ2

Stochastic Policy Gradient

事實上,計算確切 policy gradient 的值是一件非常困難的事情,因為在 model-free 之下,我們並不知道實際上 trajectory 的分佈

Pμπθ 或是 state 的分佈
dμπ
為何。

因此,我們使用 sampling 來估計期望值:

θVπθ(μ)=EτPμπθ[R(τ)t=0θlogπθ(at|st)]R(τ)t=0θlogπθ(at|st)

g(θk,τ)=R(τ)t=0θlogπθ(at|st) 稱為 stochastic estimate of PG,且
g(θk,τ)
是 exact PG
θVπθ(μ)|θ=θk
的不偏估計 (unbiased estimate)。

Unbiased Estimate

x is an unbiased estimate of some deterministic quantity
x¯
if
E[x]=x¯

which is equivalent to
x=x¯+ϵ where E[ϵ]=0

我們可以透過 sample 一或多個 trajectories 來估計

θVπθ(μ),例如 n 條 trajectories:
θVπθ(μ)1/nτR(τ)t=0θlogπθ(at|st)

Stochastic Gradient Descent

更一般化而言,定義 optimization 問題為:

θ=argminθΘF(θ), whereF(θ):=Eξ[f(θ;ξ)]

其中

ξ 代表此期望值下的隨機性,通常為未知的,則此期望值計算困難,因此我們可以使用 stochastic gradient descent:
θk+1=θkηkE[θf(θk;ξk)]θkηkg(θk;ξk)

SGD 通常會出現更具有隨機性的行為,可以視覺化如下:

這是因為每一次更新參數時,我們只做 sampling,導致 gradient descent 的每一步更新方向不穩定。

Algorithms with SPG

REINFORCE policy gradient 定義如下:

θVπθ(μ)=EτPμπθ[t=0γtQπθ(st,at)θlogπθ(at|st)]

若在 policy

π 下,每一回合迭代取一條 trajectory
τ=(s0,a0,r1,s1,a1,...)
,則
Gt(τ)=m=tγmrm+1


Qπθ(st,at)=E[Gt(τ)]

則我們可以得到

θVπθ(μ) 的不偏估計
τ^

τ^:=t=0γtGt(τ)θlogπθ(at|st)

REINFORCE Algorithm

  1. Initialize
    θ0
    and step size
    η
    .
  2. Sample a trajectory
    τPμπθ
    and make the update as
    θk+1=θk+ητ^=θk+η(t=0γtGt(τ)θlogπθ(at|st))

    and repeat step 2 until termination.

當然也可以用 total reward policy gradient 作為演算法內參數更新的方法:

Algorithm with Total Reward Method

  1. Initialize
    θ0
    and step size
    η
    .
  2. Sample a trajectory
    τPμπθ
    and make the update as
    θk+1=θk+ητ¯=θk+η(R(τ)θlogπθ(at|st))

    and repeat step 2 until termination.

或者用 Q-value and discounted state visitation 作為演算法內更新參數的方法:

Algorithm with Q-value and discounted state visitation

  1. Initialize
    θ0
    and step size
    η
    .
  2. Draw a batch
    B
    of n state-action pairs from
    dμπθ
    and
    πθ
    and construct
    τ~:=11γ(1n(s,a)BQθπ(s,a)θlogπθ(a|s))

    then apply
    θk+1=θk+ητ~

值得注意的是,以 Q-value and discounted state visitation method 估計的

τ~ 並不是 exact PG 的不偏估計!

想像

dμπθ 是由所有
τμ
中的每個
(s,a)
pair 所構成,則我們可以靠 sample 很多 trajectories 並把所有
(s,a)
pair 打亂丟進一個 buffer 內來逼近
dμπθ
。但因為我們 sample 出來的 trajectories 必為有限個,因此這個 buffer 並不是真正的
dμπθ
,故
τ~
是 biased 的。

不過只要 n 夠大,

τ~ 仍然可以逼近 exact PG。


上述使用到 sampling 來做估計的 policy gradient 方法被稱作 Monte-Carlo policy gradient。

Variance Reduction

Monte-Carlo policy gradient 方法具有很高的 variance,意即我們需要很多步才可以得到較好的估計。

以 REINFORCE policy gradient 的一個簡單的例子來觀察這個現象:

假設有一個環境僅有一個 state,action

a
b
皆會往 terminal state,而 reward 分別為 100 與 99。:

假設使用 softmax policy,因此 parameters 分別為

θ(s,a)
θ(s,b)
。Softmax policy 下 policy 定義為
πθ(a|s)=exp(θs,a)aAexp(θs,a)


logπθ(a|s)θ(s,a)=1πθ(a|s), logπθ(a|s)θ(s,b)=πθ(b|s)logπθ(b|s)θ(s,a)=1+πθ(a|s), logπθ(b|s)θ(s,b)=πθ(b|s)

假設此時

θ(s,a)=θ(s,b)=0,因此
πθ(a|s)=πθ(b|s)=0.5
,則 true PG 為

θVπθ(μ)=0.5(γQ(s,a)θlogπθ(a|s))+0.5(γQ(s,b)θlogπθ(b|s))=0.5(γQ(s,a)[1πθ(s,a)πθ(s,b)])+0.5(γQ(s,b)[1+πθ(s,a)πθ(s,b)])=0.5(1100[0.50.5])+0.5(199[0.50.5])=[0.250.25]

但如果我們做 sampling,同一時間只有一個 action 會被選擇,則我們估計出來的

θ^ 可能為
θ^={100[0.50.5] if action a is sampled99[0.50.5] if action b is sampled

下圖比較 true gradient 和兩個 sample gradient,可以發現不同 sample gradient 的差異會非常大:

這正是為何使用 sampling 來做估計的話會有很大的 variance。

我們可以透過下列幾個方法來進行 variance reduction:

  1. Baseline
  2. Critic
  3. Advantage function (baseline + critic)

Baseline

Baseline 的概念是希望可以透過對

Qπθ(s,a) 減去一個 baseline function
B(s)
來達到減少 variance 的效果,以 REINFORCE 為例:
EτPμπθ[t=0γt(Qπθ(st,at)B(st))θlogπθ(at|st)]

減去

B(s) 並不會影響期望值,我們可以靠證明
EτPμπθ[B(st)θlogπθ(at|st)]=0
得到此結論:
EτPμπθ[B(st)θlogπθ(at|st)]=sP(st=s)aπθ(a|s)θlogπθ(a|s)B(s)=sP(st=s)B(s)θaπθ(a|s)   //by chain rule=sP(st=s)B(s)θ1=0

REINFORCE with baseline

  1. Initialize
    θ0
    and step size
    η
    .
  2. Sample a trajectory
    τPμπθ
    and make the update as
    θk+1=θk+ητ^=θk+η(t=0γt(GtB(st))θlogπθ(at|st))

    and repeat step 2 until termination.

基於減少 variance 的目的,常會取

B(s)=Vπθ(s) (證明略)。

Critic

這個方法的想法是希望可以學一個 critic 來估計 action-value function:

Qw(s,a)Qπθ(s,a)

此類方法統稱為 actor-critic algorithms:

  • Critic:更新 action-value function 的參數
    w
  • Actor:更新 policy 的參數
    θ
    ,且更新方向是由 critic 所建議的

以 Q-value and discounted state visitation 為例,此時 approximate policy gradient 為

θVπθ(μ)11γEsdμπθEaπθ(|s)[Qw(s,a)θlogπθ(a|s)]

以此為例,一個基於 Q-function critic 的 actor-critic algorithm 可以如下:

Q-Value Actor-Critic

  1. Initialize
    θ,w
    , step size
    η,s0
    and sample
    a0πθ
  2. For each step
    t=0,1,2,...

    Sample reward
    rt+1
    , transition
    st+1
    , action
    at+1πθ(st+1,at+1)

    θθ+ηQw(st,at)θlogπθ(at|st)

    Update
    w
    for
    Qw(s,a)
    (possibly using
    rt+1,st+1,at+1
    )

Advantage Function

此方法同時使用了 baseline 和 critic。

Advantage function

Aπθ(s,a)=Qπθ(s,a)Vπθ(s)

則我們可以改寫 (P3) 為

θVπθ(μ)=11γEsdμπθEaπθ(|s)[Aπθ(s,a)θlogπθ(a|s)]

Policy Gradient with Advantage (P4)

θVπθ(μ)=11γEsdμπθEaπθ(|s)[Aπθ(s,a)θlogπθ(a|s)]
REINFORCE with Advantage(P5)
θVπθ(μ)=EτPμπθ[t=0γtAπθ(st,at)θlogπθ(at|st)]

Next

下一篇筆記:
https://hackmd.io/@tsen159/RLNote3
內容為 Model-Free Prediction。