強化學習讀書筆記 - 09 - on-policy預測的近似方法


強化學習讀書筆記 - 09 - on-policy預測的近似方法

參照

需要了解強化學習的數學符號,先看看這里:

這一章開始了第二部門 - 近似解決方案

近似方法的重要性

我們先看看傳統方法中存在的問題:

  • 不適用復雜的環境。主要原因是狀態和行動太多,策略需要大量空間來記憶策略價值。
  • 環境可能是不穩定的,過去的經驗不能適用於未來的情況。需要一個通用性的方法來更新策略價值。
  • 策略價值是一個數值,缺乏通用性。期望有一個通用的方法來計算策略價值。

所以對近似預測方法的理解是,找到一個通用的方法\(\hat{v}(s, \theta)\)
數學表示

\[\hat{v}(s, \theta) \approx v_{\pi}(s) \\ where \\ \theta \text{ - a weight vector} \\ \theta \doteq (\theta_1, \theta_2, ..., \theta_n)^T \]

解釋
近似預測方法是指求策略的狀態價值的近似值。
求策略的行動狀態價值的近似值叫做近似控制方法(Control Methods)(下一章的內容)。

近似預測方法的目標

首先,我們需要找到一個判斷近似預測方法質量的計算公式。

價值均方誤差(Mean Squared Value Error)

\[MSVE(\theta) = \sum_{s \in \mathcal{S}} d(s) [v_{\pi} - \hat{v}(s, \theta)]^2 \\ where \\ d(s) \text{ - on-policy distribution, the fraction of time spent in s under the target policy } \pi \\ \]

  • 在情節性任務中

\[\eta(s) = h(s) + \sum_{\bar{s}} \eta(\bar{s}) \sum_{a} \pi(a|\bar{s})p(s|\bar{s}, a), \ \forall s \in \mathcal{S} \\ d(s) = \frac{\eta(s)}{\sum_{s'} \eta(s')} \\ where \\ \eta(s) \text{ - the number of time steps spent in state s in a single episode} \\ h(s) \text{ - time spent in a state s if episodes start in it} \]

  • 在連續性任務中

\[d(s) = \text{ the stationary distribution under } \pi \\ \]

解釋:
\(\eta(s) = h(s) + \sum_{\bar{s}} \eta(\bar{s}) \sum_{a} \pi(a|\bar{s})p(s|\bar{s}, a), \ \forall s \in \mathcal{S}\)
狀態s的發生時間(次數) = 在情節中狀態s發生在開始的時間(次數) + 狀態s發生在其它的時間(次數)

隨機梯度遞減方法(Stochastic gradient descend method)

那么如何求\(\theta\)呢?一個常見的方法是通過梯度遞減的方法,迭代的求解\(\theta\)

隨機梯度遞減算法

Stochastic gradient descend

\[\begin{align} \theta_{t+1} & \doteq \theta_{t} - \frac{1}{2} \alpha \nabla [v_{\pi}(S_t) - \hat{v}(S_t, \theta_t)]^2 \\ & = \theta_{t} + \alpha [v_{\pi}(S_t) - \hat{v}(S_t, \theta_t)] \nabla \hat{v}(S_t, \theta_t) \\ \end{align} \\ where \\ \nabla f(\theta) \doteq \left ( \frac{\partial f(\theta)}{\partial \theta_1}, \frac{\partial f(\theta)}{\partial \theta_2}, \cdots, \frac{\partial f(\theta)}{\partial \theta_n} \right )^T \\ \alpha \text{ - the step size, learning rate} \]

解釋
這個方法可以在多次迭代后,讓\(\theta\)最優。
\(v_{\pi}(S_t)\)是實際值。
\(\hat{v}(S_t, \theta_t)\)是當前計算值。
隨機梯度遞減方法通過誤差(實際值 - 當前計算值)接近最優值的方法。
比較麻煩的是:如何求\(\nabla \hat{v}(S_t, \theta_t)\)
傳統的方法是求\(v_{\pi}(s), q_{\pi}(s, a)\),在近似方法中變成了求\(\theta, \hat{v}(s, \theta), \hat{q}(s, a,\theta)\)

蒙特卡洛

  • 算法描述

Input: the policy \(\pi\) to be evaluated
Input: a differentiable function \(\hat{v} : \mathcal{S} \times \mathbb{R^n} \to \mathbb{R}\)

Initialize value-function weights \(\theta\) arbitrarily (e.g. \(\theta = 0\))
Repeat (for each episode):
  Generate an episode \(S_0, A_0, R_1 ,S_1 ,A_1, \cdots ,R_t ,S_t\) using \(\pi\)
  For \(t = 0, 1, \cdots, T - 1\)
   \(\theta \gets \theta + \alpha [G_t -\hat{v}(S_t, \theta)] \nabla \hat{v}(S_t, \theta)\)

半梯度遞減方法(Semi-gradient method)

之所以叫半梯度遞減的原因是TD(0)和n-steps TD計算價值的公式不是精確的(而蒙特卡羅方法是精確的)。

半梯度下降(Semi-gradient TD(0))

  • 算法描述

Input: the policy \(\pi\) to be evaluated
Input: a differentiable function \(\hat{v} : S^+ \times \mathbb{R^n} \to \mathbb{R}\) such that \(\hat{v}(terminal, \dot \ ) = 0\)

Initialize value-function weights \(\theta\) arbitrarily (e.g. \(\theta = 0\))
Repeat (for each episode):
  Initialize \(\mathcal{S}\)
  Repeat (for each step of episode):
   Choose $A \sim \pi(\dot \ |S) $
   Take action \(A\), observe \(R, S'\)
   \(\theta \gets \theta + \alpha [R + \gamma \hat{v}(S', \theta) -\hat{v}(S', \theta)] \nabla \hat{v}(S, \theta)\)
   \(S \gets S'\)
  Until \(S'\) is terminal

n-steps TD

請看原書,不做拗述。

特征選擇

線性方程的定義

\[\phi(s) \doteq (\phi_1(s), \phi_2(s), \dots, \phi_n(s))^T \\ \hat{v} \doteq \theta^T \phi(s) \doteq \sum_{i=1}^n \theta_i \phi_i(s) \]

\(\phi(s)\)特征函數
這里討論特征函數的通用化定義方法。

多項式基(polynomials basis)

\(s\)的每一個維度都可以看成一個特征。多項式基的方法是使用\(s\)的高維多項式作為新的特征。
比如:二維的\(s = (s_1, s_2)\),可以選擇多項式為\((1, s_1, s_2, s_1s_2)\)或者\((1, s_1, s_2, s_1s_2, s_1^2, s_2^2, s_1s_2^2, s_1^2s_2, s_1^2s_2^2)\)

多項式基方法的通用數學表達:

\[\phi_i(s) = \prod_{j=1}^d s_j^{C_{i,j}} \\ where \\ s = (s_1,s_2,\cdots,s_d)^T \\ \phi_i(s) \text{ - polynomials basis function} \]

傅里葉基(Fourier basis)

傅里葉基方法的通用數學表達:

\[\phi_i(s) = \cos(\pi c^i \dot s), \ s \in [0,1)] \\ where \\ c^i = (x_1^i, c_2^i, \cdots, c_d^i)^T, \ with \ c_j^i \in \{0, \cdots, N\} \ for \ j = 1, \cdots, d \ and \ i = 0, \cdots, (N + 1)^d \]

徑向基(Radial Basis)

徑向基方法的通用數學表達:

\[\phi_i(s) \doteq exp \left ( - \frac{\lVert s-c_i \rVert ^2 }{2 \sigma_i^2} \right ) \]

最小二乘法TD(Least-Squares TD)

Input: feature representation \(\phi(s) \in \mathbb{R}^n, \forall s \in \mathcal{S}, \phi(terminal) \doteq 0\)

$\hat{A^{-1}} \gets \epsilon^{-1} I \qquad \text{An } n \times n \ matrix \( \)\hat{b} \gets 0$
Repeat (for each episode):
  Initialize S; obtain corresponding \(\phi\)
  Repeat (for each step of episode):
   Choose \(A \sim \pi(\dot \ | S)\)
   Take action \(A\), observer \(R, S'\); obtain corresponding \(\phi'\)
   \(v \gets \hat{A^{-1}}^T (\phi - \gamma \phi')\)
   \(\hat{A^{-1}} \gets \hat{A^{-1}} - (\hat{A^{-1}}\phi) v^T / (1+v^T\phi)\)
   \(\hat{b} \gets \hat{b} + R \phi\)
   \(\theta \gets \hat{A^{-1}} \hat{b}\)
   \(S \gets S'; \phi \gets \phi'\)
  until S' is terminal


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM