强化学习到大模型训练理论概要(三)

Lecture 8 Human Feedback and Reinforcement Learning from Human Preferences

Pairwise Comparisons

  • 通常来说,比起手写一个奖励函数,人们更容易进行成对比较。
  • 通常比起提供标量奖励(例如"你有多喜欢这个广告?")要容易。

Bradley-Terry 模型 (1952)

  • 已经发现,在没有其他假设的情况下,潜在的奖励模型是不唯一的。
  • 现在集中于一个特定的结构模型。
  • 首先考虑一个较简单的 k 拉杆赌博机设置: <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K个动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> b 1 , b 2 , ... , b k b_1, b_2, \ldots, b_k </math>b1,b2,...,bk。没有状态/上下文。
  • 假设人类进行有噪声的成对比较,其关于更喜欢 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i b_i </math>bi 而非 <math xmlns="http://www.w3.org/1998/Math/MathML"> b j b_j </math>bj 的概率为:
    <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> P ( b i ≻ b j ) = exp ⁡ ( r ( b i ) ) exp ⁡ ( r ( b i ) ) + exp ⁡ ( r ( b j ) ) = p i j P(b_i \succ b_j) = \frac{\exp(r(b_i))}{\exp(r(b_i)) + \exp(r(b_j))} = p_{ij} </math>P(bi≻bj)=exp(r(bi))+exp(r(bj))exp(r(bi))=pij
  • 传递性: <math xmlns="http://www.w3.org/1998/Math/MathML"> p i k p_{ik} </math>pik 是由 <math xmlns="http://www.w3.org/1998/Math/MathML"> p i j p_{ij} </math>pij 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> p j k p_{jk} </math>pjk 确定的。

定义 Condorcet Winner 一个物品 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i b_i </math>bi 如果对于每个其他的物品 <math xmlns="http://www.w3.org/1998/Math/MathML"> b j b_j </math>bj,满足 <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( b i ≻ b j ) > 0.5 P(b_i \succ b_j) > 0.5 </math>P(bi≻bj)>0.5,则它是一个Condorcet胜者。 Copeland Winner 一个物品 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i b_i </math>bi 如果它在与所有其他物品的成对比较中胜出的次数最多,则它是一个Copeland胜者。物品的得分通过它击败的物品数量减去输给的物品数量计算而得。 Borda Winner 一个物品 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i b_i </math>bi 如果它最大化期望得分,则它是一个Borda胜者。如果 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i ≻ b j b_i \succ b_j </math>bi≻bj,得分为1;如果 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i = b j b_i = b_j </math>bi=bj,则得0.5;如果 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i ≺ b j b_i \prec b_j </math>bi≺bj,则为0。

历史上,用于k臂或对抗 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( k = 2 (k=2 </math>(k=2)赌博机的算法主要集中在寻找Copeland胜者。

1. Preference learning

Fitting the Parameters of a Bradley-Terry Model

  • 首先考虑 <math xmlns="http://www.w3.org/1998/Math/MathML"> k k </math>k 臂赌博机问题: <math xmlns="http://www.w3.org/1998/Math/MathML"> K K </math>K 个行动 <math xmlns="http://www.w3.org/1998/Math/MathML"> b 1 , b 2 , ... , b k b_1, b_2, \ldots, b_k </math>b1,b2,...,bk。没有状态/上下文。
  • 假设人类进行噪声的成对比较,偏好 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i ≻ b j b_i \succ b_j </math>bi≻bj 的概率为

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> P ( b i ≻ b j ) = exp ⁡ ( r ( b i ) ) exp ⁡ ( r ( b i ) ) + exp ⁡ ( r ( b j ) ) = p i j P(b_i \succ b_j) = \frac{\exp(r(b_i))}{\exp(r(b_i)) + \exp(r(b_j))} = p_{ij} </math>P(bi≻bj)=exp(r(bi))+exp(r(bj))exp(r(bi))=pij

  • 假设有 <math xmlns="http://www.w3.org/1998/Math/MathML"> N N </math>N 个形式为 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( b i , b j , μ ) (b_i, b_j, \mu) </math>(bi,bj,μ) 的元组,其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ ( 1 ) = 1 \mu(1)=1 </math>μ(1)=1 表示人类认为 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i ≻ b j b_i \succ b_j </math>bi≻bj, <math xmlns="http://www.w3.org/1998/Math/MathML"> μ ( 1 ) = 0.5 \mu(1)=0.5 </math>μ(1)=0.5 表示人类认为 <math xmlns="http://www.w3.org/1998/Math/MathML"> b i = b j b_i = b_j </math>bi=bj,否则 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ ( 1 ) = 0 \mu(1)=0 </math>μ(1)=0 表示 <math xmlns="http://www.w3.org/1998/Math/MathML"> b j ≻ b i b_j \succ b_i </math>bj≻bi
  • 用交叉熵最大化似然函数

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> loss = − ∑ ( b i , b j , μ ) ∈ D μ ( 1 ) log ⁡ P ( b i ≻ b j ) + μ ( 2 ) log ⁡ P ( b j ≻ b j ) \text{loss} = - \sum_{(b_i, b_j, \mu) \in \mathcal{D}} \mu(1) \log P(b_i \succ b_j) + \mu(2) \log P(b_j \succ b_j) </math>loss=−(bi,bj,μ)∈D∑μ(1)logP(bi≻bj)+μ(2)logP(bj≻bj)

将偏好用于强化学习中的奖励建模

  • 可以对轨迹进行类似处理。
  • 考虑两条轨迹, <math xmlns="http://www.w3.org/1998/Math/MathML"> τ 1 ( s 0 , a 7 , s 14 , ... ) \tau^1(s_0, a_7, s_{14}, \ldots) </math>τ1(s0,a7,s14,...) 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> τ 2 ( s 0 , a 6 , s 12 , ... ) \tau^2(s_0, a_6, s_{12}, \ldots) </math>τ2(s0,a6,s12,...)。
  • 令 <math xmlns="http://www.w3.org/1998/Math/MathML"> R 1 = ∑ i = 0 T − 1 r i 1 R^1 = \sum_{i=0}^{T-1} r_i^1 </math>R1=∑i=0T−1ri1 为轨迹 <math xmlns="http://www.w3.org/1998/Math/MathML"> τ 1 \tau^1 </math>τ1 的(潜在、未观察到的)奖励和,对 <math xmlns="http://www.w3.org/1998/Math/MathML"> R 2 R^2 </math>R2 进行类似处理。
  • 定义人类偏好 <math xmlns="http://www.w3.org/1998/Math/MathML"> τ 1 ≻ τ 2 \tau^1 \succ \tau^2 </math>τ1≻τ2 的概率为

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> P ^ [ τ 1 ≻ τ 2 ] = exp ⁡ ∑ i = 0 t − 1 r i 1 exp ⁡ ∑ i = 0 t − 1 r i 1 + exp ⁡ ∑ i = 0 t − 1 r i 2 \hat{P}[\tau^1 \succ \tau^2] = \frac{\exp \sum_{i=0}^{t-1} r_i^1}{\exp \sum_{i=0}^{t-1} r_i^1 + \exp \sum_{i=0}^{t-1} r_i^2} </math>P^[τ1≻τ2]=exp∑i=0t−1ri1+exp∑i=0t−1ri2exp∑i=0t−1ri1

  • 使用学习到的奖励模型,并用该模型进行近端策略优化(PPO)。

2. From Backflips to ChatGPT

图中显示一条被标记为"Human baseline"的水平线,代表单个人类的平均判断准确性。 在"Human baseline"上方,另一条线表示Ensemble of Humans(多个判断者的集成)的准确性,通常高于单个个体判断。 随着数据量增加和模型规模增大,各模型的验证准确性通常逐步逼近人类单个个体的水平。

Lecture 9 DPO - Direct Preference Optimization: A New RLHF Approach

如果想研究公式,推荐论文: arxiv.org/abs/2404.10... Direct Preference Optimization (DPO) 是一种用于优化模型的策略的方法,通过直接优化模型输出中的偏好选择来实现。在强化学习与偏好学习的背景下,DPO关注的是如何在不依赖显式奖励或回报函数的情况下,利用偏好数据提升策略性能。 DPO的核心思想是通过人类偏好数据或对决策结果的偏好选择,直接对策略进行优化,而不是通过传统的奖励信号来指导学习过程。这一方式可以避免某些奖励设计上的困难或不良行为(例如 reward hacking)。 核心技术概念

  1. 偏好数据
    • 利用人类偏好数据,这些数据可以是人工标注的对决策输出结果的对比偏好,或者是从用户交互中获取的隐式反馈。
  2. 偏好模型
    • 构建一个偏好模型来捕捉和表达这些人类偏好,并将其转化为可学习的信号。
  3. 优化目标
    • 优化的目标函数基于偏好模型,通常旨在最大化用户总体偏好的可能性。
    • 直接优化偏好而非基于传统奖励或回报函数。 以下公式推导,关注reward model 是如何抵消的。

RLHF: Learning a reward model from human feedback

RLHF: Learning a policy that optimizes the reward

Direct Preference Optimization

How Efficiently does DPO Trade off Reward & KL?

Lecture 10 Offline RL

为什么我们不能只用Q-Learning?

  • Q-Learning是一种 Off-Policy 强化学习算法
    • Can be used with data different than the state--action pairs would visit under the optimal Q state action values
  • But deadly triad of bootstrapping , function approximation and off policy, and can fail

挑战和局限性

  • 引导(Bootstrapping)

    • Q-Learning使用Bootstrapping来更新估计值。Bootstrap(引导)是指一种使用当前估计来更新策略或价值函数的方法。具体来说,在引导过程中,期望值的计算部分依赖于现有的估计值 或是对未来回报的预测 。这种方法中的关键在于它不必等到整个序列结束才进行更新,而是可以使用当前的估计进行渐进更新。这意味着它利用当前估计的未来值来更新当前的值。而这种引导策略在某些情况下可能引入较大的误差。
  • 函数逼近(Function Approximation)

    • 在高维度状态空间中,我们通常会利用函数逼近技术(如神经网络)来代替传统Q表。然而,函数逼近可能会导致估计值的误差放大,因为逼近函数可能会欠拟合或过拟合数据。
  • 异策略(Off-Policy)

    • 尽管异策略能够灵活使用不同来源的数据,但这也可能引入策略不一致性的问题。当前学习策略和目标策略之间的差异可能使得策略更新不稳定。

致命三合会(Deadly Triad)

  • Bootstrapping、Function Approximation 和 Off-Policy
    • 这三者的结合被称为"致命三合会"(Deadly Triad)。当一起使用时,算法可能会陷入不稳定甚至发散的状态。
    • 例如,Q-Learning在使用函数逼近时,如果引导方法和异策略引入的误差无法被预见和控制,算法可能得不到收敛,或者错误地更新Q值,最终导致失败。

Offline batch policy evaluation

Batch Policy Evaluation: Estimate Performance of a Specific Decision Policy

<math xmlns="http://www.w3.org/1998/Math/MathML"> ∫ s ∈ S 0 V ^ π ( s , D ) d s \int_{s \in S_0} \hat{V}^\pi(s, \mathcal{D}) ds </math>∫s∈S0V^π(s,D)ds 公式表示使用数据集 <math xmlns="http://www.w3.org/1998/Math/MathML"> D \mathcal{D} </math>D 来估计策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 的价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V ^ π ( s , D ) \hat{V}^\pi(s, \mathcal{D}) </math>V^π(s,D),并对初始状态集合 <math xmlns="http://www.w3.org/1998/Math/MathML"> S 0 S_0 </math>S0 进行积分以整体评估策略的性能。这是策略评估(Policy Evaluation)的核心思想。

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> D \mathcal{D} </math>D: 包含 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 个轨迹 <math xmlns="http://www.w3.org/1998/Math/MathML"> τ \tau </math>τ 的数据集,这些轨迹是基于行为策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π b \pi_b </math>πb 生成的。
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π: 策略,用于将状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 映射到动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a。
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> S 0 S_0 </math>S0: 初始状态的集合。
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> V ^ π ( s , D ) \hat{V}^\pi(s, \mathcal{D}) </math>V^π(s,D): 使用数据集 <math xmlns="http://www.w3.org/1998/Math/MathML"> D \mathcal{D} </math>D 来估计状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 的价值函数。

在策略评估中使用高效的采样方法可以显著提高评估质量,特别是在数据有限的情况下具有实际应用的优势。所以我们需要 offline RL。

1. Using models

  • 改进现有数据的动态和奖励模型,未必会导致未来更好的策略 → 由于模型不当指定导致的偏差:
    • 即使我们在现有数据上构建了更精细的动态和奖励模型,这些改进未必能推导出在未见环境或未来状况下表现更优的策略。其原因在于模型可能存在不当指定,即模型错误地假设或参数化了环境动态。

2. Using model-free methods

Fitted Q Evaluation

无模型价值函数逼近中的一种方法,即拟合Q值评估(Fitted Q Evaluation,FQI)。这种方法通常在强化学习中用于估算Q值函数,特别是在数据有限或状态空间巨大时。

  1. 数据集

    • <math xmlns="http://www.w3.org/1998/Math/MathML"> D = ( s i , a i , r i , s i + 1 ) ∀ i \mathcal{D} = (s_i, a_i, r_i, s_{i+1}) \forall i </math>D=(si,ai,ri,si+1)∀i
      • 这是用于学习的经验数据集,每个元素代表一个转移片段,包括状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s i s_i </math>si、动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a i a_i </math>ai、奖励 <math xmlns="http://www.w3.org/1998/Math/MathML"> r i r_i </math>ri、以及下一状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s i + 1 s_{i+1} </math>si+1。
  2. 目标更新公式

    • <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ~ π ( s i , a i ) = r i + γ V θ π ( s i + 1 ) \tilde{Q}^\pi(s_i, a_i) = r_i + \gamma V^\pi_\theta(s_{i+1}) </math>Q~π(si,ai)=ri+γVθπ(si+1)
      • 这里的 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ~ π ( s i , a i ) \tilde{Q}^\pi(s_i, a_i) </math>Q~π(si,ai)是目标Q值,结合即时奖励 <math xmlns="http://www.w3.org/1998/Math/MathML"> r i r_i </math>ri和折扣未来值 <math xmlns="http://www.w3.org/1998/Math/MathML"> γ V θ π ( s i + 1 ) \gamma V^\pi_\theta(s_{i+1}) </math>γVθπ(si+1)计算。这表示从经验数据中更新得来的估测Q值,是拟合过程的目标。
  3. 误差最小化

    • <math xmlns="http://www.w3.org/1998/Math/MathML"> arg ⁡ min ⁡ θ ∑ i ( Q θ π ( s i , a i ) − Q ~ π ( s i , a i ) ) 2 \arg\min_\theta \sum_i (Q^\pi_\theta(s_i, a_i) - \tilde{Q}^\pi(s_i, a_i))^2 </math>argminθ∑i(Qθπ(si,ai)−Q~π(si,ai))2
      • 目标是在所有经验项中,最小化差异平方和。这一优化问题用于调整参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> θ \theta </math>θ,使得估计的Q函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ~ π \tilde{Q}^\pi </math>Q~π尽可能接近目标Q值。

计算流程: Fitted Q Evaluation (FQE) 和 Deep Q-Network (DQN) 都是用于强化学习中的Q值函数逼近方法,但它们在方法论、应用场景和实现机制上有一些显著的区别。FQE 是一种静态的Q值估计方法,用以验证和分析已有策略在离线展现中的表现,而DQN 则是一种动态的策略优化方法,目标是在复杂环境中逼近最优策略。以下是对这两种方法的详细对比:

Fitted Q Evaluation (FQE)

  1. 方法性质
    • FQE 是一种离线方法,主要用于策略评估而不是策略优化。它使用一个固定的数据集进行Q值函数的拟合。
  2. 使用场景
    • 适用于离线场景中存在大量收集数据的情况。它侧重于在已有轨迹中精确估计一个固定策略的价值。
  3. 数据处理
    • FQE 通过拟合累计奖励和未来价值来逐个批次优化 Q 值。没有策略更新部分,主要目的是在给定的策略下计算长期回报。
  4. 模型和计算
    • 通常不使用神经网络进行训练,可能利用简单的线性回归或其他机器学习模型来拟合Q值。
  5. 局限性
    • 由于是纯评估工具,无法在评估过程中改进策略,适用于策略验证和模型调试。

Deep Q-Network (DQN)

  1. 方法性质
    • DQN 是一种在线方法,结合深度学习以实现大规模问题的强化学习。它通过神经网络逼近Q值函数,不断对策略进行优化改进。
  2. 使用场景
    • 适用于需要在线学习和策略改进的实时交互场景,例如游戏环境、机器人控制等。
  3. 数据/经验重播
    • DQN 使用经验回放(Experience Replay)技术,从存储的记忆池中随机抽样进行训练,打破时间关联性,提高样本效率。
  4. 模型和计算
    • 使用深度神经网络逼近和更新Q函数,可以处理高维度的状态和动作空间。
  5. 创新特性
    • DQN 引入了目标网络(Target Network)用以稳定训练过程,以及经验回放缓冲区来提高数据利用率与训练的收敛性。
  6. 局限性
    • 计算资源要求较高;在数据和策略上可能需要更多步长来取得稳定的策略优化。

总结

  • 目标和适用性
    • FQE 主要用于已有数据集上的策略评估,适合离线环境,而 DQN 在实时策略优化中表现突出。
  • 实现复杂度
    • DQN 更为复杂且广泛使用神经网络,适用于处理复杂问题,而 FQE 可以是更简单的实现形式,仅需处理静态数据。
  • 数据交互
    • FQE 依赖于固有数据集进行评估,无需实时互动;DQN 则需要与环境实时交互获取新的经验数据。

Model Free Policy Evaluation 的挑战

    1. 依赖于马尔可夫假设: 强化学习的许多方法,包括无模型方法,都假设环境遵循马尔可夫决策过程(MDP),即当前状态和动作决定了下一个状态和奖励,与过去状态无关。如果现实环境不符合这一假设,无模型评估可能表现不佳。
    1. 依赖模型精准指定: 即使是无模型方法,也常常需要依赖某些模型假设,或至少依赖对价值函数Q的逼近。然而,如果模型被错误指定(misspecified),即假设或参数化不正确,模型可能无法提供准确的评估结果,且在此情况下往往缺乏计算保证。

3. Use importance sampling

Off Policy Evaluation With Minimal Assumptions

在最小假设下进行离线策略评估(Off-Policy Evaluation)的挑战和愿望。

  1. 无需依赖模型或马尔可夫假设的方法
    • 理想情况下,我们希望找到不依赖于模型的精确性或马尔可夫假设的评估方法。马尔可夫假设(即环境下一状态只取决当前状态和动作,不受历史影响)常用在强化学习方法中,但在现实中可能难以完美满足。
  2. 蒙特卡罗方法的参考
    • 蒙特卡罗方法 通过样本路径估计的方式,已经成功在在线策略评估中应用。通过累积采样数据,它不依赖模型假设从而进行策略评估。
    • 期望找到类似的无依赖方法 ,用于离线策略评估中。
  3. 愿望:类似的无模型评估方法
    • 希望模仿蒙特卡罗方法,为离线策略评估设计类似的技术,使得无需制定精确模型便能评估策略的优劣。
  4. 挑战:数据分布不匹配
    • 现实中的数据分布和策略通常会有差异。即使在蒙特卡罗方法中,采样策略与目标策略不一致,可能造成严重偏差。
    • 离线策略评估的困难主要源于数据分布不匹配,数据集可能并非来自于我们试图评估的目标策略,进而导致评估结果失准。
Importance Sampling 重要性采样

重要性采样(Importance Sampling)是一种无偏估计方法。 重要性采样的核心公式和解释

  1. 目标期望的表示 <math xmlns="http://www.w3.org/1998/Math/MathML"> E p [ r ] \mathbb{E}_p[r] </math>Ep[r]:

    • <math xmlns="http://www.w3.org/1998/Math/MathML"> E p [ r ] \mathbb{E}_p[r] </math>Ep[r] 表示在分布 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) p(x) </math>p(x) 下对 <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( x ) r(x) </math>r(x) 的期望。
    • 公式如下:
      <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> E p [ r ] = ∑ x p ( x ) r ( x ) \mathbb{E}_p[r] = \sum_x p(x) r(x) </math>Ep[r]=x∑p(x)r(x)
  2. 重要性采样的转换

    • 通过引入辅助分布 <math xmlns="http://www.w3.org/1998/Math/MathML"> q ( x ) q(x) </math>q(x) 来重写目标期望:
      <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> E p [ r ] = ∑ x p ( x ) q ( x ) q ( x ) r ( x ) \mathbb{E}_p[r] = \sum_x \frac{p(x)}{q(x)} q(x) r(x) </math>Ep[r]=x∑q(x)p(x)q(x)r(x)
    • 这里, <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) q ( x ) \frac{p(x)}{q(x)} </math>q(x)p(x) 称为重要性权重。
  3. 样本均值估计

    • 用从 <math xmlns="http://www.w3.org/1998/Math/MathML"> q ( x ) q(x) </math>q(x) 分布中抽取的样本进行估计:
      <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> E p [ r ] ≈ 1 N ∑ i = 1 , x ∼ q N p ( x i ) q ( x i ) r ( x i ) \mathbb{E}p[r] \approx \frac{1}{N} \sum{i=1,x \sim q}^N \frac{p(x_i)}{q(x_i)} r(x_i) </math>Ep[r]≈N1i=1,x∼q∑Nq(xi)p(xi)r(xi)
    • 其中从 <math xmlns="http://www.w3.org/1998/Math/MathML"> q ( x ) q(x) </math>q(x) 中抽样得到的样本数量为 <math xmlns="http://www.w3.org/1998/Math/MathML"> N N </math>N,每个样本的权重 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x i ) q ( x i ) \frac{p(x_i)}{q(x_i)} </math>q(xi)p(xi) 调整了不同分布的影响。

重要性采样的条件

  1. 采样分布的覆盖条件
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> q ( x ) > 0 q(x) > 0 </math>q(x)>0 for all <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x such that <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) > 0 p(x) > 0 </math>p(x)>0
      • 采样分布 <math xmlns="http://www.w3.org/1998/Math/MathML"> q ( x ) q(x) </math>q(x) 在 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( x ) p(x) </math>p(x) 有非零概率的所有 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x 上也必须是非零的。这确保了每个可能的 <math xmlns="http://www.w3.org/1998/Math/MathML"> x x </math>x 都被考虑,从而确保全面覆盖和重叠。
  2. 无隐含混杂因素
    • No hidden confounding
      • 没有隐藏的混淆因素,即变量之间的潜在依赖关系或干扰因素影响评估结果的情况应健全。这确保估计的无偏性和可信度。 使用重要性采样进行强化学习策略评估:不需要知道动态模型!

这张幻灯片简要总结了重要性采样(Importance Sampling)的特性和优点,具体内容如下:

重要性采样 (Importance Sampling)主要要点

  1. 不依赖于马尔可夫假设
    • Does not rely on Markov assumption
      • 强化学习中的一些方法依赖于马尔可夫假设,即未来状态只取决于当前状态和动作,而与过去无关。重要性采样则不需要这种假设,能够在更广泛的条件下应用。
  2. 需要最小的假设
    • Requires minimal assumptions
      • 重要性采样方法对数据分布和模型结构的要求较低,只需要已知两个分布之间的关系(即原始分布和目标分布),方便实际应用中的实现。
  3. 提供无偏估计
    • Provides unbiased estimator
      • 在合适的条件下,重要性采样可以提供对目标分布期望的无偏估计。这意味着通过足够多的样本,估计值会准确反映真实期望值。
  4. 类似于蒙特卡罗估计,但纠正了分布不匹配问题
    • Similar to Monte Carlo estimator but corrects for distribution mismatch
      • 蒙特卡罗方法通过直接采样估计期望值,但在采样分布与目标分布不匹配时会产生偏差。重要性采样通过调整样本权重,纠正分布不匹配的问题,提供更精确的估计。
逐决策重要性采样 (PDIS)

"逐决策重要性采样"(Per Decision Importance Sampling, PDIS),这是一种用于策略评估的增强采样技术。该技术利用了问题域中的时间结构,这与策略梯度方法相似。

  • 利用领域的时间结构
    • Leverage temporal structure of the domain 提示了使用这种技术可以充分利用决策过程中的时间序列特性。

普通重要性采样公式 <math xmlns="http://www.w3.org/1998/Math/MathML"> I S ( D ) IS(D) </math>IS(D) 公式如下:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> I S ( D ) = 1 n ∑ i = 1 n ( ∏ t = 1 L π e ( a t ∣ s t ) π b ( a t ∣ s t ) ) ( ∑ t = 1 L γ t R t i ) IS(D) = \frac{1}{n} \sum_{i=1}^{n} \left( \prod_{t=1}^{L} \frac{\pi_e(a_t \mid s_t)}{\pi_b(a_t \mid s_t)} \right) \left( \sum_{t=1}^{L} \gamma^t R_t^i \right) </math>IS(D)=n1i=1∑n(t=1∏Lπb(at∣st)πe(at∣st))(t=1∑LγtRti)

  • 解释
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> I S ( D ) IS(D) </math>IS(D):代表重要性采样估计的期望值。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 n ∑ i = 1 n \frac{1}{n} \sum_{i=1}^{n} </math>n1∑i=1n:对所有 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 条轨迹做平均。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> ∏ t = 1 L π e ( a t ∣ s t ) π b ( a t ∣ s t ) \prod_{t=1}^{L} \frac{\pi_e(a_t \mid s_t)}{\pi_b(a_t \mid s_t)} </math>∏t=1Lπb(at∣st)πe(at∣st):时间段内每个动作在评估策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π e \pi_e </math>πe 与行为策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π b \pi_b </math>πb 下的比率,这些比率的乘积给予整体轨迹的权重。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> ∑ t = 1 L γ t R t i \sum_{t=1}^{L} \gamma^t R_t^i </math>∑t=1LγtRti:计算时间折扣累积分数回报, <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ 是折扣因子,保证长期累积回报的稳定性。

逐决策重要性采样公式 <math xmlns="http://www.w3.org/1998/Math/MathML"> P S I D ( D ) PSID(D) </math>PSID(D) 公式如下:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> P S I D ( D ) = ∑ t = 1 L γ t 1 n ∑ i = 1 n ( ∏ τ = 1 t π e ( a τ ∣ s τ ) π b ( a τ ∣ s τ ) ) R t i PSID(D) = \sum_{t=1}^{L} \gamma^t \frac{1}{n} \sum_{i=1}^{n} \left( \prod_{\tau=1}^{t} \frac{\pi_e(a_{\tau} \mid s_{\tau})}{\pi_b(a_{\tau} \mid s_{\tau})} \right) R_t^i </math>PSID(D)=t=1∑Lγtn1i=1∑n(τ=1∏tπb(aτ∣sτ)πe(aτ∣sτ))Rti

  • 解释
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> P S I D ( D ) PSID(D) </math>PSID(D):代表逐决策重要性采样的估计值。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> ∑ t = 1 L γ t \sum_{t=1}^{L} \gamma^t </math>∑t=1Lγt:对每个时间点 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t 执行求和,同时使用折扣因子 <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 n ∑ i = 1 n \frac{1}{n} \sum_{i=1}^{n} </math>n1∑i=1n:对 (n) 条轨迹数据计算均值。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> ∏ τ = 1 t π e ( a τ ∣ s τ ) π b ( a τ ∣ s τ ) \prod_{\tau=1}^{t} \frac{\pi_e(a_{\tau} \mid s_{\tau})}{\pi_b(a_{\tau} \mid s_{\tau})} </math>∏τ=1tπb(aτ∣sτ)πe(aτ∣sτ):仅计算从初始时刻到当前时刻 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t 的权重累计,这与普通重要性采样方法的区别在于它逐步积累。
    • <math xmlns="http://www.w3.org/1998/Math/MathML"> R t i R_t^i </math>Rti:在时刻 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t 处的即时回报。

总结:

  • 优势:逐决策重要性采样(PDIS)能够更好地利用时间结构,使得在更长时间跨度上的决策做出调整时更为灵敏。这种方法提高了估计的效率和准确性,特别是在连续时间决策和动态环境中。
  • 应用领域:由于该方法与策略梯度方法类似,其灵活性使得它在强化学习和动态决策问题中具有广泛应用潜力。
Importance Sampling Variance
  • 重要性采样,就像蒙特卡罗估计一样,通常具有高方差
  • 在序列决策过程中,对于策略回报的估计,重要性采样特别具有高方差

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> = ∑ i = 1 , τ i ∼ π b N R τ i ∏ t = 1 H i p ( a i t ∣ π , s i t ) p ( a i t ∣ π b , s i t ) = \sum_{i=1, \tau_i \sim \pi_b}^{N} R_{\tau_i} \prod_{t=1}^{H_i} \frac{p(a_{it} \mid \pi, s_{it})}{p(a_{it} \mid \pi_b, s_{it})} </math>=i=1,τi∼πb∑NRτit=1∏Hip(ait∣πb,sit)p(ait∣π,sit)

  • 方差通常会随着时间跨度呈指数级增长

    • a. 集中不等式(如Hoeffding不等式)随变量的最大范围而变化
    • b. 变量的最大范围取决于重要性权重的乘积
  • 可选检查你的理解:对于一个长度为 H 的单个轨迹,其最大回报为1,并且如果对于每个时间步 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( a ∣ s , π b ) = 0.1 p(a|s, \pi_b) = 0.1 </math>p(a∣s,πb)=0.1 且 <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( a ∣ s , π ) = 1 p(a|s, \pi) = 1 </math>p(a∣s,π)=1,那么单个轨迹的重要性加权回报的最大值是多少? Solution: 1 / (.1)^H = 10^H

    • 重要性权重为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0.1 = 10 \frac{1}{0.1} = 10 </math>0.11=10 每一步,因此对于一个H步轨迹,总的权重乘积为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 H 10^H </math>10H。
    • 如果最大回报为1,最大重要性加权回报为 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 0 H × 1 = 1 0 H 10^H \times 1 = 10^H </math>10H×1=10H。

Offline policy learning / optimization

<math xmlns="http://www.w3.org/1998/Math/MathML"> arg ⁡ max ⁡ π ∫ s ∈ S 0 V ^ π ( s , D )   d s \arg\max_{\pi} \int_{s \in S_0} \hat{V}^{\pi}(s, D) \, ds </math>argmaxπ∫s∈S0V^π(s,D)ds

符号说明

  • <math xmlns="http://www.w3.org/1998/Math/MathML"> D D </math>D:数据集 <math xmlns="http://www.w3.org/1998/Math/MathML"> D D </math>D 包含来自行为策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π b \pi_b </math>πb 的 <math xmlns="http://www.w3.org/1998/Math/MathML"> n n </math>n 条轨迹
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π:策略,将状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 映射到动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> S 0 S_0 </math>S0:初始状态的集合
  • <math xmlns="http://www.w3.org/1998/Math/MathML"> V ^ π ( s , D ) \hat{V}^{\pi}(s, D) </math>V^π(s,D):使用数据集 <math xmlns="http://www.w3.org/1998/Math/MathML"> D D </math>D 来估计状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 下策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 的价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) V(s) </math>V(s)

2020年前的离线策略优化

  • 算法通常假设样本重叠
    • 离线策略估计:针对感兴趣的策略
    • 离线策略优化:包括所有策略的优化,包括最优策略(批量强化学习中的"集中性"假设)
  • 在很多情况下,这种假设不太可能成立
  • 许多真实数据集并不包含完整的随机探索
  • 假设存在样本重叠而实际不存在时可能引起问题
    • 我们可能会得到一个估计表现很高的策略,但在实际部署时表现却很差

尽最大努力利用现有资源:在未完全覆盖数据的情况下进行离线策略优化

  • 将离线策略优化限制在有数据重叠的区域
    • 我们以前见过类似的想法:例如KL约束或PPO剪辑
  • 计算上可行的算法
  • 简单的思想:对于状态-动作空间中没有足够重叠/支持的区域,假设悲观结果

近期保守的批量强化学习方法是不充分的。

在数据不足的状态-动作空间中使用悲观值(pessimistic value)

  1. 过滤函数(Filtration function)

    • 定义
      <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ζ ( s , a ; μ ^ , b ) = 1 ( μ ^ ( s , a ) > b ) \zeta(s, a; \hat{\mu}, b) = 1(\hat{\mu}(s, a) > b) </math>ζ(s,a;μ^,b)=1(μ^(s,a)>b)
    • 解释
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> ζ ( s , a ; μ ^ , b ) \zeta(s, a; \hat{\mu}, b) </math>ζ(s,a;μ^,b):这是一个过滤函数,决定是否将状态-动作对 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a ) (s, a) </math>(s,a)的估值视为可靠。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> μ ^ ( s , a ) \hat{\mu}(s, a) </math>μ^(s,a):这是对于状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s和动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a的估计值。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b:这是一个阈值,用于判断估计值是否足够高。
      • 1( <math xmlns="http://www.w3.org/1998/Math/MathML"> ⋅ \cdot </math>⋅):这是指示函数,当括号内的条件为真时,取值1,条件为假时,取值0。
      • 条件判断:只有当 <math xmlns="http://www.w3.org/1998/Math/MathML"> μ ^ ( s , a ) \hat{\mu}(s, a) </math>μ^(s,a)大于阈值 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b时,过滤函数才输出1,表示在这种情况下,这对状态-动作对的估计值被认为是可靠的。 统计不确定性的处理
    • b can account for statistical uncertainty due to finite samples
      • 阈值 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b可以考虑由于样本有限带来的统计不确定性。
      • 在实际操作中,如果 <math xmlns="http://www.w3.org/1998/Math/MathML"> b b </math>b设置得更高,则表示在估计值必须更高才能被认为是可靠的,这对数据不充分的区域使用更加保守的态度。
  2. 贝尔曼算子和贝尔曼评估算子(Bellman operator and Bellman evaluation operator)

    • 贝尔曼算子 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> T \mathcal{T} </math>T)定义:

      <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> T f ( s , a ) = r ( s , a ) + γ E s ′ [ max ⁡ a ′ ζ ( s ′ , a ′ ) f ( s ′ , a ′ ) ] \mathcal{T}f(s, a) = r(s, a) + \gamma \mathbb{E}{s'} \left[ \max{a'} \zeta(s', a') f(s', a') \right] </math>Tf(s,a)=r(s,a)+γEs′[a′maxζ(s′,a′)f(s′,a′)]

      • <math xmlns="http://www.w3.org/1998/Math/MathML"> T f ( s , a ) \mathcal{T}f(s, a) </math>Tf(s,a):这是一个贝尔曼算子,用于计算当前状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 和动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a 下的值函数。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( s , a ) r(s, a) </math>r(s,a):当前状态-动作对的即时奖励。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ:折扣因子,用于权衡未来奖励与当前奖励。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> E s ′ \mathbb{E}_{s'} </math>Es′:取期望值,表示对下一状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s ′ s' </math>s′ 的期望。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> max ⁡ a ′ \max_{a'} </math>maxa′:在所有可能的后继动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ′ a' </math>a′ 中取最大值。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> ζ ( s ′ , a ′ ) \zeta(s', a') </math>ζ(s′,a′):过滤函数,仅在有足够数据支持的状态-动作对上取值1,否则为0。
      • <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( s ′ , a ′ ) f(s', a') </math>f(s′,a′):在状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s ′ s' </math>s′ 和动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ′ a' </math>a′ 下的值函数。
    • 贝尔曼评估算子 ( <math xmlns="http://www.w3.org/1998/Math/MathML"> T π \mathcal{T}^\pi </math>Tπ)定义:

      <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> T π f ( s , a ) = r ( s , a ) + γ E s ′ ∼ P , a ′ ∼ π [ ζ ( s ′ , a ′ ) f ( s ′ , a ′ ) ] \mathcal{T}^\pi f(s, a) = r(s, a) + \gamma \mathbb{E}_{s' \sim P, a' \sim \pi} \left[ \zeta(s', a') f(s', a') \right] </math>Tπf(s,a)=r(s,a)+γEs′∼P,a′∼π[ζ(s′,a′)f(s′,a′)]

      • 类似地, <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( s , a r(s, a </math>r(s,a) 是即时奖励, <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ是折扣因子。
      • 不同的是这里的期望是对转移概率 <math xmlns="http://www.w3.org/1998/Math/MathML"> P P </math>P 和策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 进行的,表示在算法中考虑策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 下的转移和选择。
      • 同样地, <math xmlns="http://www.w3.org/1998/Math/MathML"> ζ ( s ′ , a ′ ) \zeta(s', a') </math>ζ(s′,a′) 过滤函数确保只考虑有足够数据支持的情况。

悲观估计的实现

  • 解释
    • 在公式中, <math xmlns="http://www.w3.org/1998/Math/MathML"> ζ ( s ′ , a ′ ) \zeta(s', a') </math>ζ(s′,a′)使得对于数据不足的状态-动作对(即 <math xmlns="http://www.w3.org/1998/Math/MathML"> ζ ( s ′ , a ′ ) = 0 \zeta(s', a') = 0 </math>ζ(s′,a′)=0),该部分计算结果为0。
    • 假设即时奖励 <math xmlns="http://www.w3.org/1998/Math/MathML"> r ( s , a ) ≥ 0 r(s, a) \geq 0 </math>r(s,a)≥0,因此 <math xmlns="http://www.w3.org/1998/Math/MathML"> T f ( s , a ) \mathcal{T}f(s, a) </math>Tf(s,a) 中对应的数据不足部分将不会增加值函数的值。
    • 这种机制确保了在数据不足的情况下,对应的估计值是悲观的,防止乐观的高估。

实际意义

  • 保守的策略优化
    • 通过过滤函数,仅使用有足够数据支持的状态-动作对来更新值函数,确保策略优化过程更为稳健,降低了在未观察区域内出现高风险决策的可能性。
  • 稳健的策略评估
    • 贝尔曼算子和贝尔曼评估算子的引入,通过保守估计提高了策略评估的可靠性,避免对数据不足区域的过度乐观预测。

应用场景

  • 有限数据环境
    • 在强化学习应用中,尤其是在数据不完全的环境下,如医疗诊断决策、无人驾驶等领域,通过边缘化行为支持的策略优化可以显著提高策略的安全性和有效性。
  • 深度强化学习
    • 在深度强化学习中,这种方法帮助减少模型对不确定区域的过度依赖,从而使得训练结果更为稳健可靠。

边缘化行为支持的策略优化方法(Marginalized Behavior Supported (MBI) Policy Optimization),通过跨贝尔曼算子和贝尔曼评估算子的应用,实现对数据不足区域的保守估计,从而提高策略优化的安全性和稳健性。

在良好支持的策略类中寻找最佳

PI:策略迭代 VI:值迭代

具有安全约束的离线强化学习算法

算法步骤

  1. 输入要求
    • 行为约束(g):期望的行为约束条件
    • 置信水平和数据:置信水平和数据
  2. 处理流程
    • 给定有限的决策策略集合,对于每个策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i:
      • 计算每个约束的泛化界限
      • 如果所有约束都通过,并达到所需的置信水平,则标记为 <math xmlns="http://www.w3.org/1998/Math/MathML"> Safe ( i ) = true \text{Safe}(i) = \text{true} </math>Safe(i)=true
  3. 性能评估和策略选择
    • 估计安全策略的表现 <math xmlns="http://www.w3.org/1998/Math/MathML"> f f </math>f
    • 返回最佳的安全策略,若安全的策略集合为空,则无解

注释:

  • 泛化界限指的是在训练外的实际场景中,策略如何表现的一种评估方法。
  • 如果没有满足安全条件的策略,算法将不会输出任何策略,从而避免潜在的风险。
相关推荐
珠江上上上8 分钟前
支持向量机原理
人工智能·深度学习·算法·机器学习·支持向量机·数据挖掘
珠江上上上12 分钟前
支持向量机相关文献
算法·机器学习·支持向量机
雾月5528 分钟前
LeetCode781 森林中的兔子
数据结构·算法
萌の鱼36 分钟前
leetcode 402. 移掉 K 位数字
数据结构·c++·算法·leetcode
waterHBO36 分钟前
Leetcode 算法题 26 删除重复值。
python·算法·leetcode
Excuse_lighttime2 小时前
选择排序
java·开发语言·数据结构·算法·排序算法
Excuse_lighttime2 小时前
插入排序和希尔排序
java·开发语言·数据结构·算法·排序算法
☆cwlulu2 小时前
一句话总结一种排序算法,精炼
数据结构·算法·排序算法
王老师青少年编程2 小时前
【如何掌握CSP-J 信奥赛中的排序算法】
c++·算法·排序算法·csp·信奥赛
爱是小小的癌2 小时前
数据结构与算法之排序算法-快速排序(分治)
java·开发语言·数据结构·算法·排序算法