1. Basic idea:
- (1) 回顾与引入:
- 之前的策略是表格形式的,之后的研究采用函数形式:$\pi(a|s,\theta)$,$\theta \in \mathbb{R}^m$ 是 parameter vector
- 也可写作 $\pi_\theta(a|s)$ 或 $\pi_\theta(a,s)$
- ① 此函数可以是神经网络,$s \to \boxed{\,\theta\,} \begin{matrix} \to \pi(a_1|s,\theta) \\ \vdots \\ \to \pi(a_n|s,\theta) \end{matrix}$
- ② 采用函数形式,可以解决需要大量数据和泛化性问题
- 之前的策略是表格形式的,之后的研究采用函数形式:$\pi(a|s,\theta)$,$\theta \in \mathbb{R}^m$ 是 parameter vector
- (2) 对比表格型定义:
- 最优策略定义
- 表格:$\textcolor{#3F6E9E}{\pi^* \text{ 会使所有 } v_\pi(s) \text{ 最大}}$
- 函数:$\textcolor{#3F6E9E}{\text{最大化 certain scalar metrics}}$
- 得到动作概率
- 表格:可直接访问
- 函数:需计算
- 更新策略方式
- 表格:直接改值
- 函数:不可以直接改,而是先更新 $\theta$,间接更新 $\pi_\theta(a|s)$

图源:赵世钰《强化学习的数学原理》第 9 章 - 最优策略定义
2. Metrics to define optimal policies:Average value

向量形式:
$$\bar v_\pi = \sum_{s\in S} d(s)\, v_\pi(s) = d^T v_\pi$$
其中 $v_\pi = [\cdots, v_\pi(s), \cdots]^T \in \mathbb{R}^{|S|}$
$d = [\cdots, d(s), \cdots]^T \in \mathbb{R}^{|S|}$
- (1) distribution $d$ 的选择:
- ① independent of $\pi$:
- metric 的梯度更好计算,$\bar v_\pi = d^T v_\pi$,相对简单。这种 $d$ 和 $\bar v_\pi$ 记为 $d_0$ 和 $\bar v_\pi^0$
- $d_0$
- 每个状态同样重要,$d_0(s) = \frac{1}{|S|}$
- 特别关注某个状态 $s_0$,极端情况 $d_0(s_0) = 1$,$d_0(s \neq s_0) = 0$
- ② depends on $\pi$:
- stationary distribution $d_\pi$:$d_\pi^T P_\pi = d_\pi^T$($P_\pi$ 是 state transition probability matrix)
- ① independent of $\pi$:
3. Metrics to define optimal policies:Average reward

- tips:
- 所有 metrices 都是 $\pi$ 的函数
- $\pi$ 以 $\theta$ 为参数
- ⇒ 不同 $\theta$ ⇒ 不同 metrics 的值
- ⇒ 可以搜索 optimal values of $\theta$ 来最大化 matric
第二种表达形式:


- metrics 可以被定义为
- discounted case:$\gamma \in [0,1)$ (这门课重点讨论)
- undiscounted case:$\gamma = 1$
tips:直觉上,$\bar r_\pi$ 更短视而 $\bar v_\pi$ 更长视,
但其实两者是等价的:$\bar r_\pi = (1-\gamma)\bar v_\pi$ (贝尔曼公式)
对其中一个进行优化时候另一个也达到极值
$\textcolor{#B3565B}{\bar r_\pi \text{ 和 } \bar v_\pi \text{ 的两种定义形式:}}$
- $$\textcolor{#B3565B}{\bar r_\pi = \sum_{s\in S} d_\pi(s)\, r_\pi(s) = \lim_{n\to\infty} \frac{1}{n} E\Big[\sum_{t=1}^{\infty} R_t\Big]}$$
- $$\textcolor{#B3565B}{\bar v_\pi = \sum_{s\in S} d(s)\, \bar v_\pi(s)}\ \textcolor{#3F6E9E}{\boxed{\textcolor{#B3565B}{= E\Big[\sum_{t=0}^{\infty} \gamma^t R_{t+1}\Big]}}}$$
$$E\Big[\sum_{t=0}^{\infty} \gamma^t R_{t+1}\Big] = \sum_{s\in S} d(s)\, E\Big[\underbrace{\sum_{t=0}^{\infty} \gamma^t R_{t+1} \Big| S_0 = s}_{v_\pi(s)}\Big]$$
3. Gradient of the metrics
这是最复杂的部分,需要区分 $\bar v_\pi$、$\bar r_\pi$、$\bar v_\pi^0$ 以及 un/discounted case


- discounted case:$\approx$
- un~:$=$

Proof:
$\because\ \nabla_\theta \ln\pi(a|s,\theta) = \dfrac{\nabla_\theta \pi(a|s,\theta)}{\pi(a|s,\theta)}$
$\therefore\ \nabla_\theta \pi(a|s,\theta) = \pi(a|s,\theta) \cdot \big[\nabla_\theta \ln\pi(a|s,\theta)\big]$
$$\begin{aligned} \therefore \text{ 原式 } \nabla_\theta J &= \sum_s d(s) \sum_a \underline{\underline{\nabla_\theta \pi(a|s,\theta)}}\, q_\pi(s,a) \\ &= \underline{\underline{\sum_s d(s)}} \sum_a \pi(a|s,\theta) \cdot \big[\nabla_\theta \ln\pi(a|s,\theta)\big]\, q_\pi(s,a) \\ &= E_{S\sim d}\Big[\underline{\underline{\sum_a \pi(a|S,\theta)}}\, \nabla_\theta \ln\pi(a|S,\theta)\, q_\pi(S,a)\Big] \\ &= E_{S\sim d, A\sim\pi}\big[\nabla_\theta \ln\pi(A|S,\theta)\, q_\pi(S,A)\big] \\ &= E\big[\nabla_\theta \ln\pi(A|S,\theta)\, q_\pi(S,A)\big] \end{aligned}$$


$s \to \boxed{\,\theta\,} \underbrace{\begin{matrix} \to \pi \\ \to \pi \\ \to \pi \end{matrix}}_{\text{softmax}}$
能够应对无穷个 action
5. Gradient-based algorithms and REINFORCE:Gradient-ascent

策略 $\pi$ 对应的真实的 action value,不可直接用

图源:赵世钰《强化学习的数学原理》第 9 章 
图源:赵世钰《强化学习的数学原理》第 9 章 
图源:赵世钰《强化学习的数学原理》第 9 章 
图源:赵世钰《强化学习的数学原理》第 9 章 
图源:赵世钰《强化学习的数学原理》第 9 章|手写批注:exploitation;exploration 
图源:赵世钰《强化学习的数学原理》第 9 章 
图源:赵世钰《强化学习的数学原理》第 9 章