RL【9】:Policy Gradient
系列文章目录
Fundamental Tools
RL【1】:Basic Concepts
RL【2】:Bellman Equation
RL【3】:Bellman Optimality Equation
Algorithm
RL【4】:Value Iteration and Policy Iteration
RL【5】:Monte Carlo Learning
RL【6】:Stochastic Approximation and Stochastic Gradient Descent
Method
RL【7-1】:Temporal-difference Learning
RL【7-2】:Temporal-difference Learning
文章目录
- 系列文章目录
- Fundamental Tools
- Algorithm
- Method
- 前言
- Basic idea of policy gradient
- Metrics to define optimal policies
- Average value
- Average reward
- Remarks
- Gradients of the metrics
- Gradient-ascent algorithm (REINFORCE)
- 总结
前言
本系列文章主要用于记录 B站 赵世钰老师的【强化学习的数学原理】的学习笔记,关于赵老师课程的具体内容,可以移步:
B站视频:【【强化学习的数学原理】课程:从零开始到透彻理解(完结)】
GitHub 课程资料:Book-Mathematical-Foundation-of-Reinforcement-Learning
Basic idea of policy gradient
Policy Parameterization
-
Now, policies can be represented by parameterized functions:
$\pi(a|s,\theta)$
- where θ∈Rm\theta \in \mathbb{R}^mθ∈Rm is a parameter vector.
-
The function can be, for example, a neural network, whose input is sss, output is the probability to take each action, and parameter is θ\thetaθ.
-
Advantage: when the state space is large, the tabular representation will be of low efficiency in terms of storage and generalization.
-
The function representation is also sometimes written as π(a,s,θ)\pi(a,s,\theta)π(a,s,θ), πθ(a∣s)\pi_\theta(a|s)πθ(a∣s), or πθ(a,s)\pi_\theta(a,s)πθ(a,s).
Policy Parameterization
公式:
π(a∣s,θ)\pi(a|s,\theta)π(a∣s,θ)
- 表示在状态 sss 下,选择动作 aaa 的概率,其中 θ\thetaθ 是参数向量。
直观理解:
- 在 tabular 方法里,我们用表格直接存储 π(s,a)\pi(s,a)π(s,a) 的概率;
- 在函数表示方法里,我们用参数(比如神经网络的权重 θ\thetaθ)去“生成”这些概率。
优点:
当状态空间很大时(比如图像输入,连续状态),tabular 表存储和泛化能力都很差;
而函数(尤其是神经网络)可以高效地表示复杂策略,并且具有泛化能力。
Differences between tabular and function representations
- First, how to define optimal policies?
- When represented as a table, a policy π\piπ is optimal if it can maximize every state value.
- When represented by a function, a policy π\piπ is optimal if it can maximize certain scalar metrics.
- Second, how to access the probability of an action?
- In the tabular case, the probability of taking a at s can be directly accessed by looking up the tabular policy.
- In the case of function representation, we need to calculate the value of π(a∣s,θ)\pi(a|s,\theta)π(a∣s,θ) given the function structure and the parameter.
- Third, how to update policies?
- When represented by a table, a policy π\piπ can be updated by directly changing the entries in the table.
- When represented by a parameterized function, a policy π\piπ cannot be updated in this way anymore. Instead, it can only be updated by changing the parameter θ\thetaθ.
Tabular vs. Function Representation 的三方面区别
- 如何定义最优策略
- Tabular:必须在 每个状态 上都最优(即最大化所有状态值)。
- Function:只需要优化一个 全局的指标(scalar metric),比如期望回报 J(θ)J(\theta)J(θ)。
- 这也是为什么深度强化学习中经常定义目标函数,然后用梯度下降来优化。
- 如何获得某个动作的概率
- Tabular:直接查表就能得到 π(a∣s)\pi(a|s)π(a∣s)。
- Function:需要通过函数计算(比如神经网络前向传播),输出 π(a∣s,θ)\pi(a|s,\theta)π(a∣s,θ)。
- 这增加了计算复杂度,但同时提供了泛化能力。
- 如何更新策略
- Tabular:直接修改表项,比如更新 π(s,a)\pi(s,a)π(s,a) 的概率值。
- Function:不能直接修改,只能通过 更新参数 θ\thetaθ 来间接改变策略。
- 例如神经网络里,通过反向传播更新权重 θ\thetaθ,进而改变输出的概率分布。
Policy Gradient: Basic Idea
-
The basic idea of the policy gradient is simple:
-
First, metrics (or objective functions) to define optimal policies: J(θ)J(\theta)J(θ), which can define optimal policies.
-
Second, gradient-based optimization algorithms to search for optimal policies:
θt+1=θt+α∇θJ(θt)\theta_{t+1} = \theta_t + \alpha \nabla_\theta J(\theta_t)θt+1=θt+α∇θJ(θt)
-
-
Although the idea is simple, the complication emerges when we try to answer the following questions:
Policy Gradient 的基本思想
目标:定义一个性能指标 J(θ)J(\theta)J(θ),比如期望累计回报。
更新方法:使用梯度上升(因为我们要最大化 J(θ)J(\theta)J(θ)):
θt+1=θt+α∇θJ(θt)\theta_{t+1} = \theta_t + \alpha \nabla_\theta J(\theta_t)θt+1=θt+α∇θJ(θt)
难点:
- 该用什么指标 J(θ)J(\theta)J(θ) 来定义最优策略?(比如 expected return)
- 如何计算 ∇θJ(θ)\nabla_\theta J(\theta)∇θJ(θ)?(涉及到策略梯度定理)
这就是后续 Policy Gradient 方法(如 REINFORCE, Actor-Critic)的基础。
策略梯度定理(Policy Gradient Theorem)
核心结论(适用于随机策略):
∇θJ(θ)=Es∼dπθ,a∼πθ[∇θlogπθ(a∣s)Qπθ(s,a)]\boxed{\;\nabla_\theta J(\theta) =\mathbb{E}{s\sim d{\pi_\theta},\,a\sim\pi_\theta}\big[\nabla_\theta \log \pi_\theta(a|s)\,Q^{\pi_\theta}(s,a)\big]\;}∇θJ(θ)=Es∼dπθ,a∼πθ[∇θlogπθ(a∣s)Qπθ(s,a)]
直观推导(likelihood-ratio/score function):
- 轨迹概率 Pθ(τ)=ρ(s0)∏tπθ(at∣st)P(st+1∣st,at)P_\theta(\tau)=\rho(s_0)\prod_t \pi_\theta(a_t|s_t)P(s_{t+1}|s_t,a_t)Pθ(τ)=ρ(s0)∏tπθ(at∣st)P(st+1∣st,at);
- ∇θJ=∑τR(τ)∇θPθ(τ)=Eτ[R(τ)∇θlogPθ(τ)]\nabla_\theta J=\sum_\tau R(\tau)\nabla_\theta P_\theta(\tau) =\mathbb{E}{\tau}\!\big[R(\tau)\nabla\theta\log P_\theta(\tau)\big]∇θJ=∑τR(τ)∇θPθ(τ)=Eτ[R(τ)∇θlogPθ(τ)];
- logPθ(τ)=∑tlogπθ(at∣st)+(与 θ无关)\log P_\theta(\tau)=\sum_t \log \pi_\theta(a_t|s_t)+\text{(与 }\theta\text{ 无关)}logPθ(τ)=∑tlogπθ(at∣st)+(与 θ 无关);
- 把 R(τ)R(\tau)R(τ) 只分配给发生后的梯度项,得到上式(用 QπQ^\piQπ 或后续回报替代)。
这个定理把“怎样改参数让回报变大”转化为“增加产生高回报动作的对数概率”
Metrics to define optimal policies
Average value
Definition
vˉπ=∑s∈Sd(s)vπ(s)\bar v_\pi = \sum_{s \in \mathcal S} d(s) v_\pi(s)vˉπ=∑s∈Sd(s)vπ(s)
-
vˉπ\bar v_\pivˉπ is a weighted average of the state values.
-
d(s)≥0d(s) \geq 0d(s)≥0 is the weight for state sss.
-
Since ∑s∈Sd(s)=1\sum_{s \in \mathcal S} d(s) = 1∑s∈Sd(s)=1, we can interpret d(s)d(s)d(s) as a probability distribution. Then the metric can be written as
vˉπ=E[vπ(S)]\bar v_\pi = \mathbb{E}[v_\pi(S)]vˉπ=E[vπ(S)]
- where S∼dS \sim dS∼d.
平均状态价值的含义
公式:
vˉπ=∑s∈Sd(s)vπ(s)\bar v_\pi = \sum_{s \in \mathcal S} d(s) v_\pi(s)vˉπ=∑s∈Sd(s)vπ(s)
- 这里的 vˉπ\bar v_\pivˉπ 是一个 标量,表示“在某个状态分布 ddd 下,状态价值的加权平均值”。
- vπ(s)v_\pi(s)vπ(s):策略 π\piπ 下,状态 sss 的价值函数。
- d(s)d(s)d(s):权重,可以理解为状态出现的概率。
换句话说,vˉπ\bar v_\pivˉπ 衡量了在长期运行时,策略 π\piπ 的整体表现。
-
Vector-product form
vˉπ=∑s∈Sd(s)vπ(s)=dTvπ\bar v_\pi = \sum_{s \in \mathcal S} d(s) v_\pi(s) = d^T v_\pivˉπ=∑s∈Sd(s)vπ(s)=dTvπ
-
where
vπ=[…,vπ(s),…]T∈R∣S∣v_\pi = [\ldots, v_\pi(s), \ldots]^T \in \mathbb{R}^{|\mathcal S|}vπ=[…,vπ(s),…]T∈R∣S∣
d=[…,d(s),…]T∈R∣S∣d = [\ldots, d(s), \ldots]^T \in \mathbb{R}^{|\mathcal S|}d=[…,d(s),…]T∈R∣S∣
-
How to select the distribution ddd?
-
The first case is that ddd is independent of the policy π\piπ.
- This case is relatively simple because the gradient of the metric is easier to calculate.
- In this case, we specifically denote d as d0d_0d0 and vˉπ\bar v_\pivˉπ as vˉπ0\bar v^0_\pivˉπ0.
- How to select d0d_0d0?
-
One trivial way is to treat all the states equally important and hence select
d0(s)=1∣S∣d_0(s) = \frac{1}{|\mathcal S|}d0(s)=∣S∣1
-
Another important case is that we are only interested in a specific state s0s_0s0. For example, the episodes in some tasks always start from the same state s0s_0s0. Then, we only care about the long-term return starting from s0s_0s0. In this case,
d0(s0)=1,d0(s≠s0)=0d_0(s_0) = 1, \quad d_0(s \neq s_0) = 0d0(s0)=1,d0(s=s0)=0
-
-
The second case is that ddd depends on the policy π\piπ.
-
A common way to select ddd as dπ(s)d_\pi(s)dπ(s), which is the stationary distribution under π\piπ.
-
One basic property of dπd_\pidπ is that it satisfies
dπTPπ=dπTd_\pi^T P_\pi = d_\pi^TdπTPπ=dπT
- where PπP_\piPπ is the state transition probability matrix.
-
The interpretation of selecting dπd_\pidπ is as follows:
- If one state is frequently visited in the long run, it is more important and deserves more weight.
- If a state is hardly visited, then we give it less weight.
-
两种分布选择方式
- Case 1: ddd 与策略 π\piπ 无关
- 简单场景,比如希望所有状态都被平等对待。
- 选择方式:
- 平均分布:d0(s)=1/∣S∣d_0(s) = 1/|\mathcal S|d0(s)=1/∣S∣,所有状态同等重要。
- 单点分布:比如只关心起始状态 s0s_0s0,那么 d0(s0)=1d_0(s_0)=1d0(s0)=1,其它为 000。
- 应用场景:任务有特定起点,或者我们希望学习“全局最优”而不考虑状态访问频率。
- Case 2: ddd 依赖于策略 π\piπ
常见的选择是 stationary distribution dπ(s)d_\pi(s)dπ(s)。
它表示在策略 π\piπ 下,长期运行后各状态的访问频率。
性质:
dπTPπ=dπTd_\pi^T P_\pi = d_\pi^TdπTPπ=dπT
- 说明在状态转移矩阵 PπP_\piPπ 下,dπd_\pidπ 是不变分布。
直观理解:
- 高频访问的状态权重大(学习时更关注这些状态)。
- 低频访问的状态权重小(可能近似不准,但无所谓)。
应用场景:我们希望优化的目标与实际运行的分布一致,更符合真实策略表现。
Average reward
Definition
-
In particular, the metric is
rˉπ≐∑s∈Sdπ(s)rπ(s)=E[rπ(S)],\bar{r}\pi \doteq \sum{s \in \mathcal{S}} d_\pi(s) r_\pi(s) = \mathbb{E}[r_\pi(S)],rˉπ≐∑s∈Sdπ(s)rπ(s)=E[rπ(S)],
-
where S∼dπS \sim d_\piS∼dπ. Here,
rπ(s)≐∑a∈Aπ(a∣s)r(s,a)r_\pi(s) \doteq \sum_{a \in \mathcal{A}} \pi(a|s) r(s,a)rπ(s)≐∑a∈Aπ(a∣s)r(s,a)
-
is the average of the one-step immediate reward that can be obtained starting from state s, and
r(s,a)=E[R∣s,a]=∑rrp(r∣s,a)r(s,a) = \mathbb{E}[R|s,a] = \sum_r r p(r|s,a)r(s,a)=E[R∣s,a]=∑rrp(r∣s,a)
- The weight dπd_\pidπ is the stationary distribution.
- As its name suggests, rˉπ\bar{r}_\pirˉπ is simply a weighted average of the one-step immediate rewards.
Equivalent definition
-
Suppose an agent follows a given policy and generate a trajectory with the rewards as (Rt+1,Rt+2,⋯R_{t+1}, R_{t+2}, \cdotsRt+1,Rt+2,⋯).
-
The average single-step reward along this trajectory is
limn→∞1nE[Rt+1+Rt+2+⋯+Rt+n∣St=s0]\lim_{n \to \infty} \frac{1}{n} \mathbb{E}\Big[ R_{t+1} + R_{t+2} + \cdots + R_{t+n} \,\big|\, S_t = s_0 \Big]limn→∞n1E[Rt+1+Rt+2+⋯+Rt+nSt=s0]
=limn→∞1nE[∑k=1nRt+k∣St=s0]= \lim_{n \to \infty} \frac{1}{n} \mathbb{E}\Big[ \sum_{k=1}^n R_{t+k} \,\big|\, S_t = s_0 \Big]=limn→∞n1E[∑k=1nRt+kSt=s0]
- where s0s_0s0 is the starting state of the trajectory.
Important property
-
An important property is that
limn→∞1nE[∑k=1nRt+k∣St=s0]=limn→∞1nE[∑k=1nRt+k]\lim_{n \to \infty} \frac{1}{n} \mathbb{E}\Big[ \sum_{k=1}^n R_{t+k} \,\big|\, S_t = s_0 \Big] = \lim_{n \to \infty} \frac{1}{n} \mathbb{E}\Big[ \sum_{k=1}^n R_{t+k} \Big]limn→∞n1E[∑k=1nRt+kSt=s0]=limn→∞n1E[∑k=1nRt+k]
=∑sdπ(s)rπ(s)= \sum_s d_\pi(s) r_\pi(s)=∑sdπ(s)rπ(s)
$= \bar{r}_\pi$
-
Note that
- The starting state s0s_0s0 does not matter.
- The two definitions of rˉπ\bar{r}_\pirˉπ are equivalent.
average reward (平均一步奖励)
- 定义回顾
平均一步奖励(average one-step reward),记作 rˉπ\bar r_\pirˉπ:
rˉπ=∑s∈Sdπ(s)rπ(s)=E[rπ(S)].\bar r_\pi = \sum_{s \in \mathcal{S}} d_\pi(s) r_\pi(s) = \mathbb{E}[r_\pi(S)] .rˉπ=∑s∈Sdπ(s)rπ(s)=E[rπ(S)].
这里:
dπ(s)d_\pi(s)dπ(s):在策略 π\piπ 下的 stationary distribution(长期访问某状态的概率);
rπ(s)r_\pi(s)rπ(s):在状态 sss 下的 期望一步奖励,定义为
rπ(s)=∑a∈Aπ(a∣s)r(s,a),r_\pi(s) = \sum_{a \in \mathcal{A}} \pi(a|s)\, r(s,a),rπ(s)=∑a∈Aπ(a∣s)r(s,a),
- 其中 r(s,a)=E[R∣s,a]r(s,a) = \mathbb{E}[R | s,a]r(s,a)=E[R∣s,a] 是在 (s,a)(s,a)(s,a) 下得到的一步奖励的期望。
换句话说,rˉπ\bar r_\pirˉπ 衡量了智能体 长期运行时,每一步能获得的平均奖励。
- 直观理解
- 分布加权平均
- 我们不能单纯对所有状态取均值,因为有些状态很少访问(权重应该小)。
- 所以引入 dπ(s)d_\pi(s)dπ(s) 作为权重。它保证常访问的状态在期望里更重要。
- 这样 rˉπ\bar r_\pirˉπ 就是 “长期平均意义下的每步奖励”。
- 等价定义
另一种定义方式是直接看一条轨迹:
rˉπ=limn→∞1nE[Rt+1+Rt+2+⋯+Rt+n∣St=s0].\bar r_\pi = \lim_{n \to \infty} \frac{1}{n}\, \mathbb{E}\Big[ R_{t+1} + R_{t+2} + \cdots + R_{t+n} \,\Big|\, S_t = s_0 \Big].rˉπ=limn→∞n1E[Rt+1+Rt+2+⋯+Rt+nSt=s0].
这表示:无论从什么初始状态 s0s_0s0 开始,沿着策略 π\piπ 跑无限长时间,每一步的平均奖励收敛到同一个值 rˉπ\bar r_\pirˉπ。
也就是说,起始状态并不重要,长期看平均值一定相同。
- 重要性质
最终可以证明:
limn→∞1nE[∑k=1nRt+k∣St=s0]=∑sdπ(s)rπ(s)=rˉπ.\lim_{n \to \infty} \frac{1}{n} \mathbb{E}\Big[ \sum_{k=1}^n R_{t+k} \,\big|\, S_t = s_0 \Big] = \sum_s d_\pi(s) r_\pi(s) = \bar r_\pi .limn→∞n1E[∑k=1nRt+kSt=s0]=∑sdπ(s)rπ(s)=rˉπ.
这说明 轨迹平均 与 加权期望 是等价的。
Remarks
- Remark 1 about the metrics:
- All these metrics are functions of π\piπ.
- Since π\piπ is parameterized by θ\thetaθ, these metrics are functions of θ\thetaθ.
- In other words, different values of θ\thetaθ can generate different metric values.
- Therefore, we can search for the optimal values of θ\thetaθ to maximize these metrics.
- Remark 2 about the metrics:
- One complication is that the metrics can be defined in either the discounted case where γ∈(0,1)\gamma \in (0,1)γ∈(0,1) or the undiscounted case where γ=1\gamma = 1γ=1.
- Remark 3 about the metrics:
-
Intuitively, rˉπ\bar r_\pirˉπ is more short-sighted because it merely considers the immediate rewards, whereas vˉπ\bar v_\pivˉπ considers the total reward overall steps.
-
However, the two metrics are equivalent to each other. In the discounted case where γ<1\gamma < 1γ<1, it holds that
rˉπ=(1−γ)vˉπ.\bar r_\pi = (1-\gamma)\bar v_\pi.rˉπ=(1−γ)vˉπ.
-
Summary of Remarks about the Metrics
Remark 1: Metrics are functions of parameters
所有指标(如平均状态价值 vˉπ\bar v_\pivˉπ、平均奖励 rˉπ\bar r_\pirˉπ)本质上都是策略 π\piπ 的函数。
策略 π\piπ 又由参数 θ\thetaθ 控制,因此这些指标是 θ\thetaθ 的函数。
改变参数 θ\thetaθ 会导致指标值的变化。
目标就是找到能最大化这些指标的最优参数 θ∗\theta^*θ∗。
→ 这就是 Policy Gradient 方法的基本思想:通过梯度优化参数,提升指标。
Remark 2: Discounted vs. Undiscounted cases
- 指标可以在 折扣情况(γ∈(0,1)\gamma \in (0,1)γ∈(0,1))或 无折扣情况(γ=1\gamma = 1γ=1)下定义。
- 折扣情况:更常用,考虑未来奖励时进行折扣,保证收敛性。
- 无折扣情况:直接平均长期奖励,但理论和计算会更复杂。
Remark 3: Relation between average reward and average state value
直观区别:
- 平均奖励 rˉπ\bar r_\pirˉπ:更“短视”,只看一步的即时奖励。
- 平均状态价值 vˉπ\bar v_\pivˉπ:更“长远”,看累计回报。
数学关系:在折扣情况 γ<1\gamma < 1γ<1 下,两者等价,满足
rˉπ=(1−γ)vˉπ\bar r_\pi = (1-\gamma)\bar v_\pirˉπ=(1−γ)vˉπ
也就是说,优化 vˉπ\bar v_\pivˉπ 与优化 rˉπ\bar r_\pirˉπ 没有本质区别,只是表现形式不同。
一句话总结:
这三条 remark 阐明了三个关键点:
- 指标依赖于参数 θ\thetaθ,因此可以用梯度方法优化(Policy Gradient 基础)。
- 指标可以基于折扣或无折扣定义,但常用折扣版本。
- 平均状态价值和平均奖励是等价的度量方式(在折扣情形下),一个偏重长期,一个偏重即时。
平均状态价值(average state value) 和 平均奖励(average reward) 的异同与联系
1. 定义
平均状态价值
vˉπ=∑s∈Sd(s)vπ(s)=ES∼d[vπ(S)]\bar v_\pi = \sum_{s \in \mathcal{S}} d(s) v_\pi(s) = \mathbb{E}{S \sim d}[v\pi(S)]vˉπ=∑s∈Sd(s)vπ(s)=ES∼d[vπ(S)]
- 是状态价值函数 vπ(s)v_\pi(s)vπ(s) 的加权平均;
- 权重 d(s)d(s)d(s) 来自于一个状态分布(可以是 均匀分布 或 stationary distribution dπd_\pidπ)。
平均奖励
rˉπ=∑s∈Sdπ(s)rπ(s)=ES∼dπ[rπ(S)]\bar r_\pi = \sum_{s \in \mathcal{S}} d_\pi(s) r_\pi(s) = \mathbb{E}{S \sim d\pi}[r_\pi(S)]rˉπ=∑s∈Sdπ(s)rπ(s)=ES∼dπ[rπ(S)]
- 是一步奖励的加权平均;
- 权重 必须 是策略 π\piπ 下的 stationary distribution dπd_\pidπ。
相同点
- 都基于加权平均
- 二者本质上都是对某个函数(vπ(s)v_\pi(s)vπ(s) 或 rπ(s)r_\pi(s)rπ(s))在状态空间上的加权平均。
- 加权方式都由状态分布决定。
- 都体现长期行为
- vˉπ\bar v_\pivˉπ:反映智能体在长期运行下,整体的状态价值水平。
- rˉπ\bar r_\pirˉπ:反映智能体在长期运行下,每步能获得的平均奖励水平。
不同点
方面 平均状态价值 vˉπ\bar v_\pivˉπ 平均奖励 rˉπ\bar r_\pirˉπ 对象 状态价值 vπ(s)v_\pi(s)vπ(s):长期折扣回报 一步奖励 rπ(s,a)r_\pi(s,a)rπ(s,a):即时回报 公式 vˉπ=∑sd(s)vπ(s)\bar v_\pi = \sum_s d(s) v_\pi(s)vˉπ=∑sd(s)vπ(s) rˉπ=∑sdπ(s)rπ(s)\bar r_\pi = \sum_s d_\pi(s) r_\pi(s)rˉπ=∑sdπ(s)rπ(s) 权重分布 可以选均匀分布 d0d_0d0,或 stationary distribution dπd_\pidπ 必须是 stationary distribution dπd_\pidπ 任务场景 多用于分析函数逼近下的价值误差 多用于 average reward RL(持续任务) 时间尺度 关注长期回报(折扣累积) 关注每步的即时平均回报 联系
- 价值函数和奖励函数的关系
状态价值函数本身就是基于奖励定义的:
vπ(s)=Eπ[∑t=0∞γtRt+1∣S0=s].v_\pi(s) = \mathbb{E}\pi\Big[\sum{t=0}^\infty \gamma^t R_{t+1} \,\Big|\, S_0 = s\Big].vπ(s)=Eπ[∑t=0∞γtRt+1S0=s].
因此:
- vˉπ\bar v_\pivˉπ 可以看作 “对长期折扣奖励的加权平均”;
- rˉπ\bar r_\pirˉπ 可以看作 “对即时奖励的加权平均”。
- 互补作用
- 在 discounted reward RL 框架下,vˉπ\bar v_\pivˉπ 更常用。
- 在 average reward RL 框架下,rˉπ\bar r_\pirˉπ 是核心目标。
- 二者本质上是 不同角度的长期表现度量。
一句话总结:
- 平均状态价值 vˉπ\bar v_\pivˉπ 是 长期折扣回报的全局加权平均;
- 平均奖励 rˉπ\bar r_\pirˉπ 是 即时奖励的长期平均;
- 前者强调 长期价值,后者强调 长期平均收益,二者都依赖于状态分布,但适用场景和优化目标不同。
Gradients of the metrics
Summary of the results about the gradients
∇θJ(θ)=∑s∈Sη(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta J(\theta) = \sum_{s \in \mathcal S} \eta(s) \sum_{a \in \mathcal A} \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θJ(θ)=∑s∈Sη(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)
- where:
- J(θ)J(\theta)J(θ) can be vˉπ,rˉπ\bar v_\pi, \bar r_\pivˉπ,rˉπ, or vˉπ0\bar v^0_\pivˉπ0
- “===” may denote strict equality, approximation, or proportional to
- η\etaη is a distribution or weight of the states
Some specific results
∇θrˉπ≃∑sdπ(s)∑a∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta \bar r_\pi \simeq \sum_s d_\pi(s) \sum_a \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θrˉπ≃∑sdπ(s)∑a∇θπ(a∣s,θ)qπ(s,a)
∇θvˉπ=11−γ∇θrˉπ\nabla_\theta \bar v_\pi = \frac{1}{1-\gamma} \nabla_\theta \bar r_\pi∇θvˉπ=1−γ1∇θrˉπ
∇θvˉπ0=∑s∈Sρπ(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta \bar v_\pi^0 = \sum_{s \in \mathcal S} \rho_\pi(s) \sum_{a \in \mathcal A} \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θvˉπ0=∑s∈Sρπ(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)
A compact and useful form of the gradient
∇θJ(θ)=∑s∈Sη(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)=E[∇θlnπ(A∣S,θ)qπ(S,A)]\nabla_\theta J(\theta) = \sum_{s \in \mathcal S} \eta(s) \sum_{a \in \mathcal A} \nabla_\theta \pi(a|s,\theta) q_\pi(s,a) = \mathbb{E}[\nabla_\theta \ln \pi(A|S,\theta) q_\pi(S,A)]∇θJ(θ)=∑s∈Sη(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)=E[∇θlnπ(A∣S,θ)qπ(S,A)]
-
where S∼ηS \sim \etaS∼η and A∼π(A∣S,θ)A \sim \pi(A|S,\theta)A∼π(A∣S,θ).
-
Why is this expression useful?
-
Because we can use samples to approximate the gradient!
∇θJ≈∇θlnπ(a∣s,θ)qπ(s,a)\nabla_\theta J \approx \nabla_\theta \ln \pi(a|s,\theta) q_\pi(s,a)∇θJ≈∇θlnπ(a∣s,θ)qπ(s,a)
-
-
How to prove the above equation?
-
Consider the function lnπ\ln \pilnπ where ln\lnln is the natural logarithm. It is easy to see that
∇θlnπ(a∣s,θ)=∇θπ(a∣s,θ)π(a∣s,θ)\nabla_\theta \ln \pi(a|s,\theta) = \frac{\nabla_\theta \pi(a|s,\theta)}{\pi(a|s,\theta)}∇θlnπ(a∣s,θ)=π(a∣s,θ)∇θπ(a∣s,θ)
-
and hence
∇θπ(a∣s,θ)=π(a∣s,θ)∇θlnπ(a∣s,θ).\nabla_\theta \pi(a|s,\theta) = \pi(a|s,\theta) \nabla_\theta \ln \pi(a|s,\theta).∇θπ(a∣s,θ)=π(a∣s,θ)∇θlnπ(a∣s,θ).
-
Then, we have
∇θJ=∑sd(s)∑a∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta J = \sum_s d(s) \sum_a \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θJ=∑sd(s)∑a∇θπ(a∣s,θ)qπ(s,a)
=∑sd(s)∑aπ(a∣s,θ)∇θlnπ(a∣s,θ)qπ(s,a)= \sum_s d(s) \sum_a \pi(a|s,\theta) \nabla_\theta \ln \pi(a|s,\theta) q_\pi(s,a)=∑sd(s)∑aπ(a∣s,θ)∇θlnπ(a∣s,θ)qπ(s,a)
=ES∼d[∑aπ(a∣S,θ)∇θlnπ(a∣S,θ)qπ(S,a)]= \mathbb{E}{S \sim d} \Bigg[ \sum_a \pi(a|S,\theta) \nabla\theta \ln \pi(a|S,\theta) q_\pi(S,a) \Bigg]=ES∼d[∑aπ(a∣S,θ)∇θlnπ(a∣S,θ)qπ(S,a)]
=ES∼d,A∼π[∇θlnπ(A∣S,θ)qπ(S,A)]= \mathbb{E}{S \sim d, A \sim \pi} \big[ \nabla\theta \ln \pi(A|S,\theta) q_\pi(S,A) \big]=ES∼d,A∼π[∇θlnπ(A∣S,θ)qπ(S,A)]
≐E[∇θlnπ(A∣S,θ)qπ(S,A)]\doteq \mathbb{E}[\nabla_\theta \ln \pi(A|S,\theta) q_\pi(S,A)]≐E[∇θlnπ(A∣S,θ)qπ(S,A)]
-
Some remarks
-
Because we need to calculate lnπ(a∣s,θ)\ln \pi(a|s,\theta)lnπ(a∣s,θ), we must ensure that for all s,a,θs,a,\thetas,a,θ
π(a∣s,θ)>0\pi(a|s,\theta) > 0π(a∣s,θ)>0
-
This can be archived by using softmax functions that can normalize the entries in a vector from (−∞,+∞)(-\infty,+\infty)(−∞,+∞) to (0,1)(0,1)(0,1).
-
For example, for any vector x=[x1,…,xn]Tx = [x_1, \ldots, x_n]^Tx=[x1,…,xn]T,
zi=exi∑j=1nexjz_i = \frac{e^{x_i}}{\sum_{j=1}^n e^{x_j}}zi=∑j=1nexjexi
- where zi∈(0,1)z_i \in (0,1)zi∈(0,1) and ∑i=1nzi=1\sum_{i=1}^n z_i = 1∑i=1nzi=1.
-
Then, the policy function has the form of
π(a∣s,θ)=eh(s,a,θ)∑a’∈Aeh(s,a’,θ),\pi(a|s,\theta) = \frac{e^{h(s,a,\theta)}}{\sum_{a’ \in \mathcal{A}} e^{h(s,a’,\theta)}},π(a∣s,θ)=∑a’∈Aeh(s,a’,θ)eh(s,a,θ),
- where h(s,a,θ)h(s,a,\theta)h(s,a,θ) is another function.
Policy Gradient (策略梯度方法) 的数学推导的进一步解释
- 一般形式:梯度表达式
核心公式:
∇θJ(θ)=∑s∈Sη(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta J(\theta) = \sum_{s \in \mathcal S} \eta(s) \sum_{a \in \mathcal A} \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θJ(θ)=∑s∈Sη(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)
J(θ)J(\theta)J(θ) 表示优化目标,可以是:
- vˉπ\bar v_\pivˉπ(平均状态价值)
- rˉπ\bar r_\pirˉπ(平均奖励)
- vˉπ0\bar v_\pi^0vˉπ0(起始状态的价值)
η(s)\eta(s)η(s) 表示状态分布或权重。
qπ(s,a)q_\pi(s,a)qπ(s,a) 是 Q-value,即在状态 sss 采取动作 aaa 后的期望回报。
这个公式说明:策略参数更新方向取决于动作对未来回报的贡献。
- 三个常见具体结果
平均奖励:
∇θrˉπ≃∑sdπ(s)∑a∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta \bar r_\pi \simeq \sum_s d_\pi(s) \sum_a \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θrˉπ≃∑sdπ(s)∑a∇θπ(a∣s,θ)qπ(s,a)
- 其中 dπ(s)d_\pi(s)dπ(s) 是在策略 π\piπ 下的稳态分布。
折扣累计回报:
∇θvˉπ=11−γ∇θrˉπ\nabla_\theta \bar v_\pi = \frac{1}{1-\gamma} \nabla_\theta \bar r_\pi∇θvˉπ=1−γ1∇θrˉπ
- 说明平均价值和平均奖励梯度之间有比例关系。
起始状态价值:
∇θvˉπ0=∑s∈Sρπ(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)\nabla_\theta \bar v_\pi^0 = \sum_{s \in \mathcal S} \rho_\pi(s) \sum_{a \in \mathcal A} \nabla_\theta \pi(a|s,\theta) q_\pi(s,a)∇θvˉπ0=∑s∈Sρπ(s)∑a∈A∇θπ(a∣s,θ)qπ(s,a)
- 这里 ρπ(s)\rho_\pi(s)ρπ(s) 是从初始状态分布出发,经过若干步在策略 π\piπ 下到达 sss 的概率。
三者本质相同,只是加权的分布不同。
3. 紧凑形式(Likelihood Ratio Trick)
通过 对数技巧:
∇θπ(a∣s,θ)=π(a∣s,θ)∇θlnπ(a∣s,θ)\nabla_\theta \pi(a|s,\theta) = \pi(a|s,\theta) \nabla_\theta \ln \pi(a|s,\theta)∇θπ(a∣s,θ)=π(a∣s,θ)∇θlnπ(a∣s,θ)
将公式化简为:
∇θJ(θ)=ES∼η,A∼π[∇θlnπ(A∣S,θ)qπ(S,A)]\nabla_\theta J(\theta) = \mathbb{E}{S \sim \eta, A \sim \pi} \big[ \nabla\theta \ln \pi(A|S,\theta) q_\pi(S,A) \big]∇θJ(θ)=ES∼η,A∼π[∇θlnπ(A∣S,θ)qπ(S,A)]
好处:避免直接对 π\piπ 求导,只需对 lnπ\ln \pilnπ 求导,更稳定。
更重要的好处:可以用采样来近似:
∇θJ≈∇θlnπ(a∣s,θ)qπ(s,a)\nabla_\theta J \approx \nabla_\theta \ln \pi(a|s,\theta) q_\pi(s,a)∇θJ≈∇θlnπ(a∣s,θ)qπ(s,a)
这就是 Policy Gradient 方法可以在实际采样下工作的关键。
- 为什么需要 Softmax
因为我们要用 lnπ(a∣s,θ)\ln \pi(a|s,\theta)lnπ(a∣s,θ),所以必须保证 π(a∣s,θ)>0\pi(a|s,\theta) > 0π(a∣s,θ)>0。
Softmax 可以自然保证这个条件:
任意实数输入 h(s,a,θ)h(s,a,\theta)h(s,a,θ) 都能映射到 (0,1)(0,1)(0,1) 且和为 111。
π(a∣s,θ)=eh(s,a,θ)∑a’∈Aeh(s,a’,θ)\pi(a|s,\theta) = \frac{e^{h(s,a,\theta)}}{\sum_{a’ \in \mathcal A} e^{h(s,a’,\theta)}}π(a∣s,θ)=∑a’∈Aeh(s,a’,θ)eh(s,a,θ)
Softmax 的性质:
- 每个动作概率 ∈(0,1)\in (0,1)∈(0,1)
- 所有动作概率和为 111
这使得策略既合法,又能覆盖所有动作(exploration)。
- 直观理解
- 目标:最大化期望回报 J(θ)J(\theta)J(θ)。
- 方法:通过梯度上升更新参数 θ\thetaθ。
- 关键技巧:
- Likelihood Ratio Trick (∇θπ=π∇θlnπ\nabla_\theta \pi = \pi \nabla_\theta \ln \pi∇θπ=π∇θlnπ),把导数搬到 log 上。
- Softmax 保证概率合法性,方便求 lnπ\ln \pilnπ 的梯度。
- 本质:更新方向由 ∇θlnπ(a∣s,θ)\nabla_\theta \ln \pi(a|s,\theta)∇θlnπ(a∣s,θ) 决定,而更新幅度由 qπ(s,a)q_\pi(s,a)qπ(s,a)(动作的好坏)加权。
Gradient-ascent algorithm (REINFORCE)
Introduction
-
The gradient-ascent algorithm maximizing J(\theta) is
θt+1=θt+α∇θJ(θ)\theta_{t+1} = \theta_t + \alpha \nabla_\theta J(\theta)θt+1=θt+α∇θJ(θ)
=θt+αE[∇θlnπ(A∣S,θt)qπ(S,A)]= \theta_t + \alpha \mathbb{E}\Big[ \nabla_\theta \ln \pi(A|S, \theta_t) q_\pi(S, A) \Big]=θt+αE[∇θlnπ(A∣S,θt)qπ(S,A)]
-
The true gradient can be replaced by a stochastic one:
θt+1=θt+α∇θlnπ(at∣st,θt)qπ(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t, \theta_t) q_\pi(s_t, a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qπ(st,at)
-
Furthermore, since qπq_\piqπ is unknown, it can be approximated:
θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t, \theta_t) q_t(s_t, a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)
-
There are different methods to approximate qπ(st,at)q_\pi(s_t, a_t)qπ(st,at)
直观解释
- Gradient-ascent algorithm 基本思想
我们的目标是最大化某个性能指标(objective function) J(θ)J(\theta)J(θ),即期望回报或平均奖励。
用 梯度上升 (gradient ascent) 来更新参数:
θt+1=θt+α∇θJ(θ),\theta_{t+1} = \theta_t + \alpha \nabla_\theta J(\theta),θt+1=θt+α∇θJ(θ),
- 其中 α\alphaα 是学习率。
Policy Gradient 定理告诉我们:
∇θJ(θ)=E[∇θlnπ(A∣S,θ)qπ(S,A)],\nabla_\theta J(\theta) = \mathbb{E}\Big[\nabla_\theta \ln \pi(A|S,\theta) q_\pi(S,A)\Big],∇θJ(θ)=E[∇θlnπ(A∣S,θ)qπ(S,A)],
- 这意味着梯度可以通过采样和 qπq_\piqπ 的估计来近似。
- Stochastic approximation & REINFORCE
由于期望 E[⋅]\mathbb{E}[\cdot]E[⋅] 很难精确计算,可以用 随机样本 来替代:
θt+1=θt+α∇θlnπ(at∣st,θt)qπ(st,at).\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t,\theta_t) q_\pi(s_t,a_t).θt+1=θt+α∇θlnπ(at∣st,θt)qπ(st,at).
但问题是:qπ(st,at)q_\pi(s_t,a_t)qπ(st,at) 本身未知。于是引入近似 qt(st,at)q_t(s_t,a_t)qt(st,at)。
如果 qt(st,at)q_t(s_t,a_t)qt(st,at) 通过 Monte Carlo 方法 来估计未来回报(即从该状态动作出发,直到 episode 结束累积的实际回报),那么这个更新公式就是经典的 REINFORCE 算法。
Remark
-
Remark 1: How to do sampling?
ES∼d,A∼π[∇θlnπ(A∣S,θt)qπ(S,A)]⟶∇θlnπ(a∣s,θt)qπ(s,a)\mathbb{E}{S \sim d, A \sim \pi} \Big[ \nabla\theta \ln \pi(A|S, \theta_t) q_\pi(S,A) \Big] \;\;\longrightarrow\;\; \nabla_\theta \ln \pi(a|s,\theta_t) q_\pi(s,a)ES∼d,A∼π[∇θlnπ(A∣S,θt)qπ(S,A)]⟶∇θlnπ(a∣s,θt)qπ(s,a)
- How to sample SSS?
- S∼dS \sim dS∼d, where the distribution d is a long-run behavior under π\piπ.
- How to sample AAA?
- A∼π(A∣S,θ)A \sim \pi(A|S,\theta)A∼π(A∣S,θ). Hence, ata_tat should be sampled following π(θt)\pi(\theta_t)π(θt) at sts_tst.
- Therefore, the policy gradient method is on-policy.
- How to sample SSS?
-
Remark 2: How to interpret this algorithm?
-
Since
∇θlnπ(at∣st,θt)=∇θπ(at∣st,θt)π(at∣st,θt),\nabla_\theta \ln \pi(a_t|s_t, \theta_t) = \frac{\nabla_\theta \pi(a_t|s_t,\theta_t)}{\pi(a_t|s_t,\theta_t)},∇θlnπ(at∣st,θt)=π(at∣st,θt)∇θπ(at∣st,θt),
-
the algorithm can be rewritten as
θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t, \theta_t) q_t(s_t,a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)
=θt+α(qt(st,at)π(at∣st,θt))∇θπ(at∣st,θt).= \theta_t + \alpha \Bigg( \frac{q_t(s_t,a_t)}{\pi(a_t|s_t,\theta_t)} \Bigg) \nabla_\theta \pi(a_t|s_t,\theta_t).=θt+α(π(at∣st,θt)qt(st,at))∇θπ(at∣st,θt).
-
Therefore, we have the important expression of the algorithm:
θt+1=θt+αβt∇θπ(at∣st,θt),\theta_{t+1} = \theta_t + \alpha \beta_t \nabla_\theta \pi(a_t|s_t,\theta_t),θt+1=θt+αβt∇θπ(at∣st,θt),
- where βt=qt(st,at)π(at∣st,θt)\beta_t = \dfrac{q_t(s_t,a_t)}{\pi(a_t|s_t,\theta_t)}βt=π(at∣st,θt)qt(st,at).
-
Remark
Remark 1: Sampling 的意义
- S∼dS \sim dS∼d:状态来自策略 π\piπ 下的长期分布。
- A∼π(⋅∣S,θ)A \sim \pi(\cdot|S,\theta)A∼π(⋅∣S,θ):动作根据当前策略 π\piπ 采样。
因此,Policy Gradient 是 on-policy 的方法:必须按照当前策略生成数据。
Remark 2: 更新公式的解释
有一个重要推导:
∇θlnπ(at∣st,θt)=∇θπ(at∣st,θt)π(at∣st,θt).\nabla_\theta \ln \pi(a_t|s_t,\theta_t) = \frac{\nabla_\theta \pi(a_t|s_t,\theta_t)}{\pi(a_t|s_t,\theta_t)}.∇θlnπ(at∣st,θt)=π(at∣st,θt)∇θπ(at∣st,θt).
所以更新式可以写成:
θt+1=θt+αβt∇θπ(at∣st,θt),\theta_{t+1} = \theta_t + \alpha \beta_t \nabla_\theta \pi(a_t|s_t,\theta_t),θt+1=θt+αβt∇θπ(at∣st,θt),
- 其中
βt=qt(st,at)π(at∣st,θt).\beta_t = \frac{q_t(s_t,a_t)}{\pi(a_t|s_t,\theta_t)}.βt=π(at∣st,θt)qt(st,at).
Gradient-ascent interpretation
-
It is a gradient-ascent algorithm for maximizing π(at∣st,θ)\pi(a_t|s_t,\theta)π(at∣st,θ):
θt+1=θt+αβt∇θπ(at∣st,θt).\theta_{t+1} = \theta_t + \alpha \beta_t \nabla_\theta \pi(a_t|s_t,\theta_t).θt+1=θt+αβt∇θπ(at∣st,θt).
-
Intuition: When \alpha \beta_t is sufficiently small:
-
If βt>0\beta_t > 0βt>0, the probability of choosing (st,at)(s_t,a_t)(st,at) is enhanced:
π(at∣st,θt+1)>π(at∣st,θt).\pi(a_t|s_t,\theta_{t+1}) > \pi(a_t|s_t,\theta_t).π(at∣st,θt+1)>π(at∣st,θt).
-
If βt<0\beta_t < 0βt<0, then
π(at∣st,θt+1)<π(at∣st,θt).\pi(a_t|s_t,\theta_{t+1}) < \pi(a_t|s_t,\theta_t).π(at∣st,θt+1)<π(at∣st,θt).
-
-
Math: When θt+1−θt\theta_{t+1} - \theta_tθt+1−θt is sufficiently small, we have
π(at∣st,θt+1)≈π(at∣st,θt)+(∇θπ(at∣st,θt))T(θt+1−θt)\pi(a_t|s_t,\theta_{t+1}) \approx \pi(a_t|s_t,\theta_t) + \big( \nabla_\theta \pi(a_t|s_t,\theta_t) \big)^T (\theta_{t+1} - \theta_t)π(at∣st,θt+1)≈π(at∣st,θt)+(∇θπ(at∣st,θt))T(θt+1−θt)
=π(at∣st,θt)+αβt(∇θπ(at∣st,θt))T(∇θπ(at∣st,θt))= \pi(a_t|s_t,\theta_t) + \alpha \beta_t \big( \nabla_\theta \pi(a_t|s_t,\theta_t) \big)^T \big( \nabla_\theta \pi(a_t|s_t,\theta_t) \big)=π(at∣st,θt)+αβt(∇θπ(at∣st,θt))T(∇θπ(at∣st,θt))
=π(at∣st,θt)+αβt∥∇θπ(at∣st,θt)∥2.= \pi(a_t|s_t,\theta_t) + \alpha \beta_t \big\| \nabla_\theta \pi(a_t|s_t,\theta_t) \big\|^2.=π(at∣st,θt)+αβt∇θπ(at∣st,θt)2.
Coefficient βt\beta_tβt
The coefficient βt\beta_tβt can well balance exploration and exploitation.
θt+1=θt+α(qt(st,at)π(at∣st,θt))∇θπ(at∣st,θt)\theta_{t+1} = \theta_t + \alpha \left( \frac{q_t(s_t,a_t)}{\pi(a_t|s_t,\theta_t)} \right) \nabla_\theta \pi(a_t|s_t,\theta_t)θt+1=θt+α(π(at∣st,θt)qt(st,at))∇θπ(at∣st,θt)
- First, βt\beta_tβt is proportional to qt(st,at)q_t(s_t,a_t)qt(st,at).
- If qt(st,at)q_t(s_t,a_t)qt(st,at) is great, then βt\beta_tβt is great.
- Therefore, the algorithm intends to enhance actions with greater values.
- Second, βt\beta_tβt is inversely proportional to π(at∣st,θt)\pi(a_t|s_t,\theta_t)π(at∣st,θt).
- If π(at∣st,θt)\pi(a_t|s_t,\theta_t)π(at∣st,θt) is small, then βt\beta_tβt is large.
- Therefore, the algorithm intends to explore actions that have low probabilities.
βt\beta_tβt 的作用:平衡探索与利用
- 与 Q 值成正比:如果 qt(st,at)q_t(s_t,a_t)qt(st,at) 大,说明该动作回报高,则 βt\beta_tβt 大,更新幅度更强。→ 利用 (exploitation)。
- 与动作概率成反比:如果 π(at∣st,θt)\pi(a_t|s_t,\theta_t)π(at∣st,θt) 小,则 βt\beta_tβt 大,更新时更倾向于提高低概率但可能有价值的动作。→ 探索 (exploration)。
因此,βt\beta_tβt 同时控制了 提升高价值动作 和 促进低概率动作的尝试,自然实现了探索-利用平衡。
REINFORCE
-
Recall that
θt+1=θt+α∇θlnπ(at∣st,θt)qπ(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t,\theta_t) q_\pi(s_t,a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qπ(st,at)
-
is replaced by
θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t,\theta_t) q_t(s_t,a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)
- where qt(st,at)q_t(s_t,a_t)qt(st,at) is an approximation of qπ(st,at)q_\pi(s_t,a_t)qπ(st,at).
-
If qπ(st,at)q_\pi(s_t,a_t)qπ(st,at) is approximated by Monte Carlo estimation, the algorithm has a specifics name, REINFORCE.
-
REINFORCE is one of earliest and simplest policy gradient algorithms.
Pseudocode: Policy Gradient by Monte Carlo (REINFORCE)
- Initialization: A parameterized function π(a∣s,θ)\pi(a|s,\theta)π(a∣s,θ), γ∈(0,1)\gamma \in (0,1)γ∈(0,1), and α>0\alpha > 0α>0.
- Aim: Search for an optimal policy maximizing J(θ)J(\theta)J(θ).
- For the kkk-th iteration, do
-
Select s0s_0s0 and generate an episode following π(θk)\pi(\theta_k)π(θk).
-
Suppose the episode is {s0,a0,r1,…,sT−1,aT−1,rT}\{s_0, a_0, r_1, \ldots, s_{T-1}, a_{T-1}, r_T\}{s0,a0,r1,…,sT−1,aT−1,rT}.
-
For t=0,1,…,T−1t = 0,1,\ldots,T-1t=0,1,…,T−1, do
-
Value update:
qt(st,at)=∑k=t+1Tγk−t−1rkq_t(s_t,a_t) = \sum_{k=t+1}^T \gamma^{k-t-1} r_kqt(st,at)=∑k=t+1Tγk−t−1rk
-
Policy update:
θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t,\theta_t) q_t(s_t,a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)
-
-
θk=θT\theta_k = \theta_Tθk=θT
-
伪代码逐行解释
Initialization
- π(a∣s,θ)\pi(a|s,\theta)π(a∣s,θ)
- 表示一个 参数化策略函数,输入状态 sss,输出动作 aaa 的概率分布。
- 参数 θ\thetaθ 是神经网络权重或其他可学习参数。
- γ∈(0,1)\gamma \in (0,1)γ∈(0,1)
- 折扣因子,用于平衡短期奖励和长期奖励。
- α>0\alpha > 0α>0
- 学习率,控制参数更新的步长。
目的:找到最优策略参数 θ∗\theta^*θ∗,使得 性能目标函数 J(θ)J(\theta)J(θ) 最大化。
For the kkkth iteration
在第 kkk 次迭代中,重复以下步骤:
生成一条轨迹 (Episode)
从初始状态 s0s_0s0 开始,使用当前策略 π(θk)\pi(\theta_k)π(θk) 生成一个完整的序列:
s0,a0,r1,…,sT−1,aT−1,rT{s_0, a_0, r_1, \ldots, s_{T-1}, a_{T-1}, r_T}s0,a0,r1,…,sT−1,aT−1,rT
- 其中 TTT 是 episode 的长度。
目的:收集当前策略下的经验数据。
For t=0,1,…,T−1t=0,1,\ldots,T-1t=0,1,…,T−1
对轨迹中的每一步 (st,at)(s_t,a_t)(st,at),进行更新:
Value update
qt(st,at)=∑k=t+1Tγk−t−1rkq_t(s_t,a_t) = \sum_{k=t+1}^T \gamma^{k-t-1} r_kqt(st,at)=∑k=t+1Tγk−t−1rk
- 这是 蒙特卡洛回报 (Monte Carlo return),表示从时刻 ttt 开始,执行动作 ata_tat 后能获得的 折扣累计奖励。
- 即:
- rt+1r_{t+1}rt+1 是立即奖励;
- γrt+2\gamma r_{t+2}γrt+2 是一步后奖励;
- 以此类推。
目的:为每个状态-动作对 (st,at)(s_t,a_t)(st,at) 提供一个估计的价值。
Policy update
θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)\theta_{t+1} = \theta_t + \alpha \nabla_\theta \ln \pi(a_t|s_t,\theta_t) q_t(s_t,a_t)θt+1=θt+α∇θlnπ(at∣st,θt)qt(st,at)
∇θlnπ(at∣st,θt)\nabla_\theta \ln \pi(a_t|s_t,\theta_t)∇θlnπ(at∣st,θt)
表示对数似然梯度,告诉我们如何调整 θ\thetaθ 使得该动作 ata_tat 在 sts_tst 下的概率增大。
qt(st,at)q_t(s_t,a_t)qt(st,at)
是权重,告诉算法这个动作的好坏:
- 如果 qt(st,at)q_t(s_t,a_t)qt(st,at) 大 → 说明动作有高回报 → 增强该动作的概率;
- 如果 qt(st,at)q_t(s_t,a_t)qt(st,at) 小或负 → 说明动作差 → 降低该动作的概率。
目的:更新策略参数,使得高回报动作的概率增加。
参数更新完成
在一个 episode 内完成所有 ttt 的更新后,得到新的参数 θT\theta_TθT。
最后赋值:
θk=θT\theta_k = \theta_Tθk=θT
- 表示第 kkk 次迭代的最终参数。
总结
这段伪代码描述的 REINFORCE 算法 本质上是:
- 用当前策略 采样一条完整轨迹。
- 计算 每一步的累计折扣回报。
- 用回报作为权重,调整策略参数,使得:
- 高回报动作的概率 ↑
- 低回报动作的概率 ↓
这就是 蒙特卡洛方法 + 策略梯度 的结合,也是最基础的 Policy Gradient 方法。
总结
REINFORCE 算法通过采样轨迹、用蒙特卡洛回报近似 Q 值,并用策略梯度更新参数,从而增加高回报动作的概率、减少低回报动作的概率,实现策略的优化。