当前位置: 首页 > news >正文

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)πθ(as), or πθ(a,s)\pi_\theta(a,s)πθ(a,s).

Policy Parameterization

  • 公式

    π(a∣s,θ)\pi(a|s,\theta)π(as,θ)

    • 表示在状态 sss 下,选择动作 aaa 的概率,其中 θ\thetaθ 是参数向量。
  • 直观理解

    • 在 tabular 方法里,我们用表格直接存储 π(s,a)\pi(s,a)π(s,a) 的概率;
    • 在函数表示方法里,我们用参数(比如神经网络的权重 θ\thetaθ)去“生成”这些概率。
  • 优点

    当状态空间很大时(比如图像输入,连续状态),tabular 表存储和泛化能力都很差;

    而函数(尤其是神经网络)可以高效地表示复杂策略,并且具有泛化能力。

Differences between tabular and function representations

  1. 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.
  2. 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)π(as,θ) given the function structure and the parameter.
  3. 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 的三方面区别

  1. 如何定义最优策略
    • Tabular:必须在 每个状态 上都最优(即最大化所有状态值)。
    • Function:只需要优化一个 全局的指标(scalar metric),比如期望回报 J(θ)J(\theta)J(θ)
      • 这也是为什么深度强化学习中经常定义目标函数,然后用梯度下降来优化。
  2. 如何获得某个动作的概率
    • Tabular:直接查表就能得到 π(a∣s)\pi(a|s)π(as)
    • Function:需要通过函数计算(比如神经网络前向传播),输出 π(a∣s,θ)\pi(a|s,\theta)π(as,θ)
      • 这增加了计算复杂度,但同时提供了泛化能力。
  3. 如何更新策略
    • 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)

  • 难点

    1. 该用什么指标 J(θ)J(\theta)J(θ) 来定义最优策略?(比如 expected return)
    2. 如何计算 ∇θ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(θ)=Esdπθ,aπθ[θlogπθ(as)Qπθ(s,a)]

  • 直观推导(likelihood-ratio/score function):

    1. 轨迹概率 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πθ(atst)P(st+1st,at)
    2. ∇θJ=∑τR(τ)∇θPθ(τ)=Eτ⁣[R(τ)∇θlog⁡Pθ(τ)]\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θ(τ)]
    3. log⁡Pθ(τ)=∑tlog⁡πθ(at∣st)+(与 θ无关)\log P_\theta(\tau)=\sum_t \log \pi_\theta(a_t|s_t)+\text{(与 }\theta\text{ 无关)}logPθ(τ)=tlogπθ(atst)+( θ 无关)
    4. 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ˉπ=sSd(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) = 1sSd(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 dSd.

平均状态价值的含义

  • 公式:

    vˉπ=∑s∈Sd(s)vπ(s)\bar v_\pi = \sum_{s \in \mathcal S} d(s) v_\pi(s)vˉπ=sSd(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ˉπ=sSd(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),]TRS

      d=[…,d(s),…]T∈R∣S∣d = [\ldots, d(s), \ldots]^T \in \mathbb{R}^{|\mathcal S|}d=[,d(s),]TRS

How to select the distribution ddd?

  1. 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)=S1

      • 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

  2. 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.

两种分布选择方式

  1. 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
    • 应用场景:任务有特定起点,或者我们希望学习“全局最优”而不考虑状态访问频率。
  2. 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ˉπsSdπ(s)rπ(s)=E[rπ(S)],

  • where S∼dπS \sim d_\piSdπ. 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)aAπ(as)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[Rs,a]=rrp(rs,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

    lim⁡n→∞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]limnn1E[Rt+1+Rt+2++Rt+nSt=s0]

    =lim⁡n→∞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]=limnn1E[k=1nRt+kSt=s0]

    • where s0s_0s0 is the starting state of the trajectory.

Important property

  • An important property is that

    lim⁡n→∞1nE[∑k=1nRt+k∣St=s0]=lim⁡n→∞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]limnn1E[k=1nRt+kSt=s0]=limnn1E[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 (平均一步奖励)

  1. 定义回顾
    • 平均一步奖励(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ˉπ=sSdπ(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)=aAπ(as)r(s,a),

      • 其中 r(s,a)=E[R∣s,a]r(s,a) = \mathbb{E}[R | s,a]r(s,a)=E[Rs,a] 是在 (s,a)(s,a)(s,a) 下得到的一步奖励的期望。
    • 换句话说,rˉπ\bar r_\pirˉπ 衡量了智能体 长期运行时,每一步能获得的平均奖励

  2. 直观理解
    1. 分布加权平均
      • 我们不能单纯对所有状态取均值,因为有些状态很少访问(权重应该小)。
      • 所以引入 dπ(s)d_\pi(s)dπ(s) 作为权重。它保证常访问的状态在期望里更重要。
      • 这样 rˉπ\bar r_\pirˉπ 就是 “长期平均意义下的每步奖励”
    2. 等价定义
      • 另一种定义方式是直接看一条轨迹:

        rˉπ=lim⁡n→∞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ˉπ=limnn1E[Rt+1+Rt+2++Rt+nSt=s0].

      • 这表示:无论从什么初始状态 s0s_0s0 开始,沿着策略 π\piπ 跑无限长时间,每一步的平均奖励收敛到同一个值 rˉπ\bar r_\pirˉπ

      • 也就是说,起始状态并不重要,长期看平均值一定相同。

  3. 重要性质
    • 最终可以证明:

      lim⁡n→∞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 .limnn1E[k=1nRt+kSt=s0]=sdπ(s)rπ(s)=rˉπ.

    • 这说明 轨迹平均加权期望 是等价的。

Remarks

  1. 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.
  2. 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.
  3. 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

  1. Remark 1: Metrics are functions of parameters

    • 所有指标(如平均状态价值 vˉπ\bar v_\pivˉπ、平均奖励 rˉπ\bar r_\pirˉπ)本质上都是策略 π\piπ 的函数。

    • 策略 π\piπ 又由参数 θ\thetaθ 控制,因此这些指标是 θ\thetaθ 的函数。

    • 改变参数 θ\thetaθ 会导致指标值的变化。

    • 目标就是找到能最大化这些指标的最优参数 θ∗\theta^*θ

      → 这就是 Policy Gradient 方法的基本思想:通过梯度优化参数,提升指标。

  2. Remark 2: Discounted vs. Undiscounted cases

    • 指标可以在 折扣情况γ∈(0,1)\gamma \in (0,1)γ(0,1))或 无折扣情况γ=1\gamma = 1γ=1)下定义。
    • 折扣情况:更常用,考虑未来奖励时进行折扣,保证收敛性。
    • 无折扣情况:直接平均长期奖励,但理论和计算会更复杂。
  3. 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ˉπ 没有本质区别,只是表现形式不同。

  4. 一句话总结

    这三条 remark 阐明了三个关键点:

    1. 指标依赖于参数 θ\thetaθ,因此可以用梯度方法优化(Policy Gradient 基础)。
    2. 指标可以基于折扣或无折扣定义,但常用折扣版本。
    3. 平均状态价值和平均奖励是等价的度量方式(在折扣情形下),一个偏重长期,一个偏重即时。

平均状态价值(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ˉπ=sSd(s)vπ(s)=ESd[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ˉπ=sSdπ(s)rπ(s)=ESdπ[rπ(S)]

    • 是一步奖励的加权平均;
    • 权重 必须 是策略 π\piπ 下的 stationary distribution dπd_\pidπ
  1. 相同点

    1. 都基于加权平均
      • 二者本质上都是对某个函数(vπ(s)v_\pi(s)vπ(s)rπ(s)r_\pi(s)rπ(s))在状态空间上的加权平均。
      • 加权方式都由状态分布决定。
    2. 都体现长期行为
      • vˉπ\bar v_\pivˉπ:反映智能体在长期运行下,整体的状态价值水平。
      • rˉπ\bar r_\pirˉπ:反映智能体在长期运行下,每步能获得的平均奖励水平。
  2. 不同点

    方面平均状态价值 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(持续任务)
    时间尺度关注长期回报(折扣累积)关注每步的即时平均回报
  3. 联系

    • 价值函数和奖励函数的关系
      • 状态价值函数本身就是基于奖励定义的:

        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ˉπ 是核心目标。
      • 二者本质上是 不同角度的长期表现度量
  4. 一句话总结

    • 平均状态价值 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(θ)=sSη(s)aAθπ(as,θ)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θπ(as,θ)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=sSρπ(s)aAθπ(as,θ)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(θ)=sSη(s)aAθπ(as,θ)qπ(s,a)=E[θlnπ(AS,θ)qπ(S,A)]

  • where S∼ηS \sim \etaSη and A∼π(A∣S,θ)A \sim \pi(A|S,\theta)Aπ(AS,θ).

  • 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π(as,θ)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π(as,θ)=π(as,θ)θπ(as,θ)

    • 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).θπ(as,θ)=π(as,θ)θlnπ(as,θ).

    • 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θπ(as,θ)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π(as,θ)θlnπ(as,θ)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]=ESd[aπ(aS,θ)θlnπ(aS,θ)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]=ESd,Aπ[θlnπ(AS,θ)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π(AS,θ)qπ(S,A)]

Some remarks

  • Because we need to calculate ln⁡π(a∣s,θ)\ln \pi(a|s,\theta)lnπ(as,θ), we must ensure that for all s,a,θs,a,\thetas,a,θ

    π(a∣s,θ)>0\pi(a|s,\theta) > 0π(as,θ)>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 = 1i=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)}},π(as,θ)=aAeh(s,a,θ)eh(s,a,θ),

    • where h(s,a,θ)h(s,a,\theta)h(s,a,θ) is another function.

Policy Gradient (策略梯度方法) 的数学推导的进一步解释

  1. 一般形式:梯度表达式
    • 核心公式:

      ∇θ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(θ)=sSη(s)aAθπ(as,θ)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 后的期望回报。

    • 这个公式说明:策略参数更新方向取决于动作对未来回报的贡献。

  2. 三个常见具体结果
    • 平均奖励:

      ∇θ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θπ(as,θ)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=sSρπ(s)aAθπ(as,θ)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)θπ(as,θ)=π(as,θ)θlnπ(as,θ)

  • 将公式化简为:

    ∇θ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π(AS,θ)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π(as,θ)qπ(s,a)

  • 这就是 Policy Gradient 方法可以在实际采样下工作的关键。

  1. 为什么需要 Softmax
    • 因为我们要用 ln⁡π(a∣s,θ)\ln \pi(a|s,\theta)lnπ(as,θ),所以必须保证 π(a∣s,θ)>0\pi(a|s,\theta) > 0π(as,θ)>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)}}π(as,θ)=aAeh(s,a,θ)eh(s,a,θ)

    • Softmax 的性质:

      • 每个动作概率 ∈(0,1)\in (0,1)(0,1)
      • 所有动作概率和为 111
    • 这使得策略既合法,又能覆盖所有动作(exploration)。

  2. 直观理解
    • 目标:最大化期望回报 J(θ)J(\theta)J(θ)
    • 方法:通过梯度上升更新参数 θ\thetaθ
    • 关键技巧
      1. Likelihood Ratio Trick (∇θπ=π∇θln⁡π\nabla_\theta \pi = \pi \nabla_\theta \ln \piθπ=πθlnπ),把导数搬到 log 上。
      2. Softmax 保证概率合法性,方便求 ln⁡π\ln \pilnπ 的梯度。
    • 本质:更新方向由 ∇θln⁡π(a∣s,θ)\nabla_\theta \ln \pi(a|s,\theta)θlnπ(as,θ) 决定,而更新幅度由 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π(AS,θ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π(atst,θ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π(atst,θt)qt(st,at)

  • There are different methods to approximate qπ(st,at)q_\pi(s_t, a_t)qπ(st,at)

直观解释

  1. 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π(AS,θ)qπ(S,A)],

      • 这意味着梯度可以通过采样和 qπq_\piqπ 的估计来近似。
  2. 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π(atst,θ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

  1. 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)ESd,Aπ[θlnπ(AS,θt)qπ(S,A)]θlnπ(as,θt)qπ(s,a)

    • How to sample SSS?
      • S∼dS \sim dSd, 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π(AS,θ). Hence, ata_tat should be sampled following π(θt)\pi(\theta_t)π(θt) at sts_tst.
      • Therefore, the policy gradient method is on-policy.
  2. 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π(atst,θt)=π(atst,θt)θπ(atst,θ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π(atst,θ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+α(π(atst,θt)qt(st,at))θπ(atst,θ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θπ(atst,θ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=π(atst,θt)qt(st,at).

Remark

  1. Remark 1: Sampling 的意义

    • S∼dS \sim dSd:状态来自策略 π\piπ 下的长期分布。
    • A∼π(⋅∣S,θ)A \sim \pi(\cdot|S,\theta)Aπ(S,θ):动作根据当前策略 π\piπ 采样。

    因此,Policy Gradient 是 on-policy 的方法:必须按照当前策略生成数据。

  2. 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π(atst,θt)=π(atst,θt)θπ(atst,θ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θπ(atst,θ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=π(atst,θt)qt(st,at).

Gradient-ascent interpretation

  • It is a gradient-ascent algorithm for maximizing π(at∣st,θ)\pi(a_t|s_t,\theta)π(atst,θ):

    θ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θπ(atst,θ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).π(atst,θt+1)>π(atst,θ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).π(atst,θt+1)<π(atst,θ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)π(atst,θt+1)π(atst,θt)+(θπ(atst,θ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)=π(atst,θt)+αβt(θπ(atst,θt))T(θπ(atst,θ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.=π(atst,θt)+αβtθπ(atst,θ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+α(π(atst,θt)qt(st,at))θπ(atst,θ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)π(atst,θt).
    • If π(at∣st,θt)\pi(a_t|s_t,\theta_t)π(atst,θ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)π(atst,θ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π(atst,θ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π(atst,θ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)π(as,θ), γ∈(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,,sT1,aT1,rT}.

    • For t=0,1,…,T−1t = 0,1,\ldots,T-1t=0,1,,T1, 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γkt1rk

      • 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π(atst,θt)qt(st,at)

    • θk=θT\theta_k = \theta_Tθk=θT

伪代码逐行解释

  • Initialization

    • π(a∣s,θ)\pi(a|s,\theta)π(as,θ)
      • 表示一个 参数化策略函数,输入状态 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 次迭代中,重复以下步骤:

    1. 生成一条轨迹 (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,,sT1,aT1,rT

        • 其中 TTT 是 episode 的长度。

      目的:收集当前策略下的经验数据。

    2. For t=0,1,…,T−1t=0,1,\ldots,T-1t=0,1,,T1

      对轨迹中的每一步 (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γkt1rk

        • 这是 蒙特卡洛回报 (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π(atst,θt)qt(st,at)

        • ∇θln⁡π(at∣st,θt)\nabla_\theta \ln \pi(a_t|s_t,\theta_t)θlnπ(atst,θt)

          表示对数似然梯度,告诉我们如何调整 θ\thetaθ 使得该动作 ata_tatsts_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) 小或负 → 说明动作差 → 降低该动作的概率。

        目的:更新策略参数,使得高回报动作的概率增加。

    3. 参数更新完成

      • 在一个 episode 内完成所有 ttt 的更新后,得到新的参数 θT\theta_TθT

      • 最后赋值:

        θk=θT\theta_k = \theta_Tθk=θT

        • 表示第 kkk 次迭代的最终参数。

总结

这段伪代码描述的 REINFORCE 算法 本质上是:

  1. 用当前策略 采样一条完整轨迹
  2. 计算 每一步的累计折扣回报
  3. 用回报作为权重,调整策略参数,使得:
    • 高回报动作的概率 ↑
    • 低回报动作的概率 ↓

这就是 蒙特卡洛方法 + 策略梯度 的结合,也是最基础的 Policy Gradient 方法。


总结

REINFORCE 算法通过采样轨迹、用蒙特卡洛回报近似 Q 值,并用策略梯度更新参数,从而增加高回报动作的概率、减少低回报动作的概率,实现策略的优化。


文章转载自:

http://UCicWYFg.ysbrz.cn
http://aEFF2lnC.ysbrz.cn
http://Eu9v9aWi.ysbrz.cn
http://ARHzwT5t.ysbrz.cn
http://DMBtCBEA.ysbrz.cn
http://ZtIl9o5v.ysbrz.cn
http://m2HhosO9.ysbrz.cn
http://MPGoxCPZ.ysbrz.cn
http://Va9lVvGr.ysbrz.cn
http://AaRIugjk.ysbrz.cn
http://fO3GUokb.ysbrz.cn
http://BawOScHz.ysbrz.cn
http://yxWSvIJW.ysbrz.cn
http://0nWWoFSC.ysbrz.cn
http://fFzyAj03.ysbrz.cn
http://brx1VhZt.ysbrz.cn
http://3jzQEIQQ.ysbrz.cn
http://mSe8KFE7.ysbrz.cn
http://uhi36L1S.ysbrz.cn
http://t1Eksrry.ysbrz.cn
http://dAcIRrXa.ysbrz.cn
http://Co7gEZVC.ysbrz.cn
http://naYbt4XC.ysbrz.cn
http://XF6LmWiD.ysbrz.cn
http://toAkxCvI.ysbrz.cn
http://6Eqf6RPQ.ysbrz.cn
http://zkvgDMeV.ysbrz.cn
http://ihkOmTlc.ysbrz.cn
http://L1lqEUsu.ysbrz.cn
http://h7uDQeif.ysbrz.cn
http://www.dtcms.com/a/379814.html

相关文章:

  • Java短链接生成服务实战指南
  • JAVA Web —— A / 网页开发基础
  • TensorFlow深度学习实战:从零开始构建你的第一个神经网络
  • Keepalived 负载均衡
  • 智能文档处理业务,应该选择大模型还是OCR专用小模型?
  • 《Redis核心机制解析》
  • Netty 在 API 网关中的应用篇(请求转发、限流、路由、负载均衡)
  • 金蝶云星空插件开发记录(一)
  • Knockout-ES5 入门教程
  • 基于 Art_DAQ、InfluxDB 和 PyQt 的传感器数据采集、存储与可视化
  • 【图像处理基石】图像压缩有哪些经典算法?
  • C语言实战:简单易懂通讯录
  • youte-agent部署(windows)
  • Python实现点云法向量各种方向设定
  • Linnux IPC通信和RPC通信实现的方式
  • apache实现LAMP+apache(URL重定向)
  • MongoDB 与 GraphQL 结合:现代 API 开发新范式
  • k8s-临时容器学习
  • uni-app 根据用户不同身份显示不同的tabBar
  • ubuntu18.04安装PCL1.14
  • Ubuntu 系统下 Anaconda 完整安装与环境配置指南(附常见问题解决)
  • 网络链路分析笔记mtr/traceroute
  • 在 Ubuntu 系统中利用 conda 创建虚拟环境安装 sglang 大模型引擎的完整步骤、版本查看方法、启动指令及验证方式
  • 基带与射频的区别与联系
  • 《企业安全运营周报》模板 (极简实用版)​
  • opencv基于SIFT特征匹配的简单指纹识别系统实现
  • Node.js 操作 Elasticsearch (ES) 的指南
  • 使用tree命令导出文件夹/文件的目录树( Windows 和 macOS)
  • Spring缓存(二):解决缓存雪崩、击穿、穿透问题
  • LabVIEW加载 STL 模型至 3D 场景 源码见附件