強化學習讀書筆記 - 12 - 資格痕跡(Eligibility Traces)


強化學習讀書筆記 - 12 - 資格痕跡(Eligibility Traces)

學習筆記:
Reinforcement Learning: An Introduction, Richard S. Sutton and Andrew G. Barto c 2014, 2015, 2016

參照

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

資格跡(Eligibility Traces)

如何理解資格跡

資格跡是一個向量,稱為eligibility trace vector。
強化學習是找最優策略\(\pi_*\)
最優策略\(\pi_*\)等價於最優行動\(\pi_*(s)\)
最優行動\(\pi_*(s)\)可以由最優狀態價值\(v_*(s)\)(或者最優行動價值\(q_*(s, a)\))決定。
如果把\(v_*(s)\)(或者\(q_*(s, a)\))看成一個函數,因此:強化學習變成了求這個函數。

在近似方法中\(v_*(s)\)(或者\(q_*(s, a)\))表示為近似預測函數\(\hat{v}(s, \theta)\)(或者近似控制函數\(\hat{q}(s, a, \theta)\))。
以近似預測函數\(\hat{v}(s, \theta)\)為例:

\[\hat{v} \doteq \theta^T \phi(s) \]

\(\phi(s)\)可以認為是固定的。它是將狀態變成一個計算向量的方法。
因此,求近似預測函數\(\hat{v}(s, \theta)\),就是求解權重向量\(\theta\)
求權重向量\(\theta\)是通過梯度下降的方法。比如:

\[\delta_t = G_t - \hat{v}(S_t, \theta_t) \\ \theta_{t+1} = \theta_t + \alpha \delta_t \nabla \hat{v}(S_t, \theta_t) \]

這里面,有三個元素:\(\alpha, G_t, \nabla \hat{v}(S_t, \theta_t)\)。每個都有自己的優化方法。

  • \(\alpha\)是學習步長
    要控制步長的大小。一般情況下步長是變化的。比如:如果誤差\(\delta_t\)變大了,步長要變小。
  • \(G_t\)的計算
    可以通過本章的\(\lambda\) - return方法。
  • \(\nabla \hat{v}(S_t, \theta_t)\)
    可以通過資格跡來優化。資格跡就是優化后的函數微分。
    為什么要優化,原因是在TD算法中\(\hat{v}(S_t, \theta_t)\)是不精確的。
    \(G_t\)也是不精確的。

\(\lambda\) - return

\(\lambda\) - return 提供了一個新的方式來估算\(G_t\),這個新的估值為\(G_t^{\lambda}\)
它是由它后面的所有\(G_t^{(n)}\)的加權平均值。
從下面的公式可以看出,這個方法可以用於連續性任務和情節性任務。

\[G_t^{(n)} \doteq R_{t+1} + \gamma R_{t+2} + \dots + \gamma^{n-1} R_{t+n} + \gamma^n \hat{v}(S_{t+n}, \theta_{t+n-1}) , \ 0 \le t \le T-n \\ \text{Continuing tasks: } \\ G_t^{\lambda} \doteq (1 - \lambda) \sum_{n=1}^{\infty} \lambda^{n-1}G_t^{(n)} \\ \text{Episodic tasks: } \\ G_t^{\lambda} \doteq (1 - \lambda) \sum_{n=1}^{T-t-1} \lambda^{n-1}G_t^{(n)} + \lambda^{T-t-1}G_t \\ where \\ \lambda \in [0, 1] \\ (1 - \lambda) \sum_{n=1}^{\infty}\lambda^{n-1} = 1 \\ (1 - \lambda) \sum_{n=1}^{T-t-1} \lambda^{n-1} + \lambda^{T-t-1} = 1 \\ \]

  • 算法描述

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 [\color{Red}{G_t^{\lambda}} -\hat{v}(S_t, \theta)] \nabla \hat{v}(S_t, \theta)\)

比較下面這個算法(第9章的蒙特卡羅方法),紅色是不同之處。

  • 算法描述

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 [\color{Red}{G_t} -\hat{v}(S_t, \theta)] \nabla \hat{v}(S_t, \theta)\)

可以看出當\(\lambda=1\)的時候,\(\lambda\) - return算法就是蒙特卡羅算法。所以說\(\lambda\) - return算法是蒙特卡羅算法的通用化算法

\(\lambda\)\(\gamma\)一起控制了n步回報\(G_t^{(n)}\)的權重。

TD(\(\lambda\))

\(e_t\) - 第t步資格跡向量(eligibility trace rate)。
資格跡向量是近似價值函數的優化微分值。
其優化的技術稱為(backward view)。仔細觀察公式可以發現\(e_t\)的算法中包含了以前的微分值。

  • 數學公式

\[e_0 \doteq 0 \\ e_t \doteq \nabla \hat{v}(S_t, \theta_t) + \gamma \lambda e_{t-1} \\ \delta_t \doteq R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t) - \hat{v}(S_{t}, \theta_t) \\ \theta_{t+1} \doteq \theta_t + \alpha \delta_t e_t \\ where \\ e_t \text{ - eligibility accumulating traces, the estimation differential of } \nabla \hat{v}(S_t, \theta) \\ \delta_t \text{ - the TD error} \\ \theta_t \text{ - the weighted vector in the approximation value function } \hat{v}(S, \theta) \\ \]

  • 算法描述(Semi-gradient TD(\(\lambda\)) for estimating \(\hat{v} \approx v_{\pi}\))
    請參考原書。

On-line Forward View

On-line和off-line的一個區別是off-line的數據是完整的,比如擁有一個情節的所有Return(G)。
這個導致off-line算法不適合on-line的情景,就是說在完成一個情節前,學習不到任何東西。
這個章節要開發一個on-line的算法,首先引入一個概念h。
h(horizon)- 水平線h表示on-line當時可以模擬的數據步驟。\(t < h \le T\)
沒有步驟h之后的數據。

  • h-truncated \(\lambda\)-return

\[G_t^{\lambda | h} \doteq (1 - \lambda) \sum_{n=1}^{h-t-1} \lambda^{n-1} G_t^{(n)} + \lambda^{h-t-1} G_t^{(h-t)}, \ 0 \le t < h \le T \\ \theta_{t+1}^h \doteq \theta_{t}^h \alpha \left [ G_t^{\lambda | h} - \hat{v}(S_t, \theta_t^h) \right ] \nabla \hat{v}(S_t, \theta_t^h) , \ 0 \le t < h \le T \\ \theta_t \doteq \ \theta_t^t \\ where \\ h \text{ - the horizon, we have the n-step returns up to the the horizon, but beyond the horizon there is no data} \]

True on-line TD(\(\lambda\))

\[e_0 \doteq 0 \\ e_t \doteq \gamma \lambda e_{t-1} + (1 - \alpha \gamma \lambda e_{t-1}^T \phi_t) \phi_t \\ \delta_t \doteq R_{t+1} + \gamma \hat{v}(S_{t+1}, \theta_t) - \hat{v}(S_{t}, \theta_t) \\ \theta_{t+1} \doteq \theta_t + \alpha \delta_t e_t + \alpha \left ( \theta_t^T \phi_t - \theta_{t-1}^T \phi_t \right ) (e_t - \phi_t) \\ where \\ e_t \text{ - eligibility dutch trace, the estimation differential of } \nabla \hat{v}(S_t, \theta) \\ \delta_t \text{ - the TD error} \\ \theta_t \text{ - the weighted vector in the approximation value function } \hat{v}(s, \theta) \\ \hat{v}(s, \theta) = \theta^T \phi(s) \\ \]

  • 算法描述(True Online TD(\(\lambda\)) for estimating \(\theta^T \phi \approx v_{\pi}\))
    請參考原書。

原書還沒有完成,這章先停在這里


免責聲明!

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



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