[Reinforcement Learning] Policy Gradient Methods

上一篇博文的內容整理了咱們如何去近似價值函數或者是動做價值函數的方法:
\[ V_{\theta}(s)\approx V^{\pi}(s) \\ Q_{\theta}(s)\approx Q^{\pi}(s, a) \]
經過機器學習的方法咱們一旦近似了價值函數或者是動做價值函數就能夠經過一些策略進行控制,好比 \(\epsilon\)-greedy。html

那麼咱們簡單回顧下 RL 的學習目標:經過 agent 與環境進行交互,獲取累計回報最大化。既然咱們最終要學習如何與環境交互的策略,那麼咱們能夠直接學習策略嗎,而以前先近似價值函數,再經過貪婪策略控制的思路更像是"曲線救國"。
這就是本篇文章的內容,咱們如何直接來學習策略,用數學的形式表達就是:
\[\pi_{\theta}(s, a) = P[a | s, \theta]\]web

這就是被稱爲策略梯度(Policy Gradient,簡稱PG)算法。算法

固然,本篇內容一樣的是針對 model-free 的強化學習。app

Value-Based vs. Policy-Based RL

Value-Based:機器學習

  • 學習價值函數
  • Implicit policy,好比 \(\epsilon\)-greedy

Policy-Based:函數

  • 沒有價值函數
  • 直接學習策略

Actor-Critic:學習

  • 學習價值函數
  • 學習策略

三者的關係能夠形式化地表示以下:
優化



認識到 Value-Based 與 Policy-Based 區別後,咱們再來討論下 Policy-Based RL 的優缺點:google

優勢:spa

  • 收斂性更好
  • 對於具備高維或者連續動做空間的問題更加有效
  • 能夠學習隨機策略

缺點:

  • 絕大多數狀況下收斂到局部最優勢,而非全局最優
  • 評估一個策略通常狀況下低效且存在較高的方差

Policy Search

咱們首先定義下目標函數。

Policy Objective Functions

目標:給定一個帶有參數 \(\theta\) 的策略 \(\pi_{\theta}(s, a)\),找到最優的參數 \(\theta\)
可是咱們如何評估不一樣參數下策略 \(\pi_{\theta}(s, a)\) 的優劣呢?

  • 對於episode 任務來講,咱們能夠使用start value:
    \[J_1(\theta)=V^{\pi_{\theta}}(s_1)=E_{\pi_{\theta}}[v_1]\]
  • 對於連續性任務來講,咱們能夠使用 average value:
    \[J_{avV}(\theta)=\sum_{s}d^{\pi_{\theta}}(s)V^{\pi_{\theta}}(s)\]
    或者每一步的平均回報:
    \[J_{avR}(\theta)=\sum_{s}d^{\pi_{\theta}}(s)\sum_{a}\pi_{\theta}(s, a)R_s^a\]
    其中 \(d^{\pi_{\theta}}(s)\) 是馬爾卡夫鏈在 \(\pi_{\theta}\) 下的靜態分佈。

Policy Optimisation

在明確目標之後,咱們再來看基於策略的 RL 爲一個典型的優化問題:找出 \(\theta\) 最大化 \(J(\theta)\)
最優化的方法有不少,好比不依賴梯度(gradient-free)的算法:

  • 登山算法
  • 模擬退火
  • 進化算法
  • ...

可是通常來講,若是咱們能在問題中得到梯度的話,基於梯度的最優化方法具備比較好的效果:

  • 梯度降低
  • 共軛梯度
  • 擬牛頓法
  • ...

咱們本篇討論梯度降低的方法。

策略梯度定理

假設策略 \(\pi_{\theta}\) 爲零的時候可微,而且已知梯度 \(\triangledown_{\theta}\pi_{\theta}(s, a)\),定義 \(\triangledown_{\theta}\log\pi_{\theta}(s, a)\) 爲得分函數(score function)。兩者關係以下:
\[\triangledown_{\theta}\pi_{\theta}(s, a) = \triangledown_{\theta}\pi_{\theta}(s, a) \frac{\triangledown_{\theta}\pi_{\theta}(s, a)}{\pi_{\theta}(s, a)}=\pi_{\theta}(s, a)\triangledown_{\theta}\log\pi_{\theta}(s, a)\]
接下來咱們考慮一個只走一步的MDP,對它使用策略梯度降低。\(\pi_{\theta}(s, a)\) 表示關於參數 \(\theta\) 的函數,映射是 \(p(a|s,\theta)\)。它在狀態 \(s\) 向前走一步,得到獎勵\(r=R_{s, a}\)。那麼選擇行動 \(a\) 的獎勵爲 \(\pi_{\theta}(s, a)R_{s, a}\),在狀態 \(s\) 的加權獎勵爲 \(\sum_{a\in A}\pi_{\theta}(s, a)R_{s, a}\),應用策略所能得到的獎勵指望及梯度爲:
\[ J(\theta)=E_{\pi_{\theta}}[r] = \sum_{s\in S}d(s)\sum_{a\in A}\pi_{\theta}(s, a)R_{s, a}\\ \triangledown_{\theta}J(\theta) = \color{Red}{\sum_{s\in S}d(s)\sum_{a\in A}\pi_{\theta}(s, a)}\triangledown_{\theta}\log\pi_{\theta}(s, a)R_{s, a}=E_{\pi_{\theta}}[\triangledown_{\theta}\log\pi_{\theta}(s, a)r] \]

再考慮走了多步的MDP,使用 \(Q^{\pi_{\theta}}(s, a)\) 代替獎勵值 \(r\),對於任意可微的策略,策略梯度爲:
\[\triangledown_{\theta}J(\theta) = E_{\pi_{\theta}}[\triangledown_{\theta}\log\pi_{\theta}(s, a)Q^{\pi_{\theta}}(s, a)]\]

策略梯度定理

對於任意可微策略 \(\pi_{\theta}(s, a)\),任意策略目標方程 \(J = J_1, J_{avR}, ...\),策略梯度:
\[\triangledown_{\theta}J(\theta) = E_{\pi_{\theta}}[\triangledown_{\theta}\log\pi_{\theta}(s, a)Q^{\pi_{\theta}}(s, a)]\]

蒙特卡洛策略梯度算法(REINFORCE)

Monte-Carlo策略梯度算法,即REINFORCE:

  • 經過採樣episode來更新參數:;
  • 使用隨機梯度上升法更新參數;
  • 使用return \(v_t\) 做爲 \(Q^{\pi_{\theta}}(s_t, a_t)\) 的無偏估計

\(\Delta\theta_t = \alpha \triangledown_{\theta}\log\pi_{\theta}(s_t, a_t)v_t\),具體以下:

Actir-Critic 策略梯度算法

Monte-Carlo策略梯度的方差較高,所以放棄用return來估計行動-價值函數Q,而是使用 critic 來估計Q:
\[Q_w(s, a)\approx Q^{\pi_{\theta}}(s, a)\]
這就是大名鼎鼎的 Actor-Critic 算法,它有兩套參數:

  • Critic:更新動做價值函數參數 \(w\)
  • Actor: 朝着 Critic 方向更新策略參數 \(\theta\)

Actor-Critic 算法是一個近似的策略梯度算法:
\[ \triangledown_\theta J(\theta)\approx E_{\pi_{\theta}}[\triangledown_{\theta}\log \pi_{\theta}(s, a)Q_w(s, a)]\\ \Delta\theta = \alpha\triangledown_\theta\log\pi_{\theta}(s,a)Q_w(s,a) \]

Critic 本質就是在進行策略評估:How good is policy \(\pi_{\theta}\) for current parameters \(\theta\).
策略評估咱們以前介紹過MC、TD、TD(\(\lambda\)),以及價值函數近似方法。以下所示,簡單的 Actir-Critic 算法 Critic 爲動做價值函數近似,使用最爲簡單的線性方程,即:\(Q_w(s, a) = \phi(s, a)^T w\),具體的僞代碼以下所示:

在 Actir-Critic 算法中,對策略進行了估計,這會產生偏差(bias),可是當知足如下兩個條件時,策略梯度是準確的:

  • 價值函數的估計值沒有和策略相違背,即:\(\triangledown_w Q_w(s,a) = \triangledown_\theta\log\pi_{\theta}(s,a)\)
  • 價值函數的參數w可以最小化偏差,即:\(\epsilon = E_{\pi_{\theta}}[(Q^{\pi_{\theta}}(s, a) - Q_w(s,a))^2]\)

優點函數

另外,咱們能夠經過將策略梯度減去一個基線函數(baseline funtion)B(s),能夠在不改變指望的狀況降低低方差(variance)。證實不改變指望,就是證實相加和爲0:
\[ \begin{align} E_{\pi_{\theta}}[\triangledown_\theta\log\pi_{\theta}(s,a)B(s)] &=\sum_{s\in S}d^{\pi_{\theta}}(s)\sum_a \triangledown_\theta\pi_{\theta}(s, a)B(s)\\ &=\sum_{s\in S}d^{\pi_{\theta}}(s)B(s)\triangledown_\theta\sum_{a\in A}\pi_{\theta}(s,a )\\ &= 0 \end{align} \]

狀態價值函數 \(V^{\pi_{\theta}}(s)\) 是一個好的基線。所以能夠經過使用優點函數(Advantage function)\(A^{\pi_{\theta}}(s,a)\) 來重寫價值梯度函數。
\[ A^{\pi_{\theta}}(s,a)=Q^{\pi_{\theta}}(s,a)-V^{\pi_{\theta}}(s)\\ \triangledown_\theta J(\theta)=E_{\pi_{\theta}}[\triangledown_\theta\log\pi_{\theta}(s,a)A^{\pi_{\theta}}(s,a)] \]

\(V^{\pi_{\theta}}(s)\) 是真實的價值函數,TD算法利用bellman方程來逼近真實值,偏差爲 \(\delta^{\pi_{\theta}}=r+\gamma V^{\pi_{\theta}}(s') - V^{\pi_{\theta}}(s)\)。該偏差是優點函數的無偏估計。所以咱們能夠使用該偏差計算策略梯度:
\[\triangledown_\theta J(\theta)=E_{\pi_{\theta}}[\triangledown_\theta\log\pi_{\theta}(s,a)\delta^{\pi_{\theta}}]\]
該方法只須要critic,不須要actor。更多關於 Advantage Function 的能夠看這裏

最後總結一下策略梯度算法:

Reference

[1] Reinforcement Learning: An Introduction, Richard S. Sutton and Andrew G. Barto, 2018
[2] David Silver's Homepage
[3] Advantage Learning

相關文章
相關標籤/搜索