前言:大语言模型构建的四个主要阶段:预训练、有监督微调、奖励建模和强化学习
- 预训练:需要利用包含数千亿甚至数万亿单词的训练数据,并借助由数千块高性能 GPU 和高速网络组成的超级计算机,花费数十天完成深度神经网络参数的训练。这一阶段的核心难点在于如何构建训练数据以及如何高效地进行分布式训练。
- 有监督微调阶段:利用少量高质量的数据集,其中包含用户输入的提示词(Prompt)和对应的理想输出结果。提示词可以是问题、闲聊对话、任务指令等多种形式和任务。这个阶段是从语言模型向 对话模型转变的关键,其核心难点在于如何构建训练数据,包括训练数据内部多个任务之间的关系、训练数据与预训练之间的关系以及训练数据的规模。
- 奖励建模阶段:目标是构建一个文本质量对比模型,用于对有监督微调模型对于同一个提示词给出的多个不同输出结果进行质量排序。这一阶段的核心难点在于如何限定奖励模型的应用范围以及如何构建训练数据。
- 强化学习阶段:根据数十万提示词,利用前一阶段训练的奖励模型,对有监督微调模型对用户提示词补全结果的质量进 行评估,与语言模型建模目标综合得到更好的效果。这一阶段的难点在于解决强化学习方法稳定性不高、超参数众多以及模型收敛困难等问题。
本文章主要针对大模型第三四个训练阶段,对强化学习基础进行理论概要,帮助大家更好的理解强化学习在大模型训练中的作用。以stanford课程为基准,还是参考了很多David Silver的主页资料,毕竟UCL的课更为大家熟悉一些。 UCL github.com/Ewenwan/UCL... Stanford web.stanford.edu/class/cs234...
Lecture 1&2 Tabular MDP Planning
Tabular MDP (Markov Decision Process) Planning 是一种在有限状态和动作空间中求解最优策略的方法。它通过明确地表示状态、动作、转移概率和奖励来解决决策问题。
1. MDP 基础与马尔可夫假设
- 状态 (States) : <math xmlns="http://www.w3.org/1998/Math/MathML"> S S </math>S 表示所有可能的状态集合。
- 动作 (Actions) : <math xmlns="http://www.w3.org/1998/Math/MathML"> A A </math>A 示所有可能的动作集合。
- 转移概率 (Transition Probabilities) : <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( s ′ ∣ s , a ) P(s' | s, a) </math>P(s′∣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"> s ′ s' </math>s′ 的概率。
- 奖励 (Rewards) : <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"> R ( s , a , s ′ ) R(s, a, s') </math>R(s,a,s′) 表示在状态 <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 后的即时奖励。
马尔可夫假设的详细解释
在强化学习中,马尔可夫假设(Markov Assumption)是构建和分析环境模型的基础。它假设当前状态包含了所有过去历史信息的必要信息,从而使得未来的状态只依赖于当前状态,而不依赖于过去的任何其他状态。这种假设简化了问题的复杂性,并使得许多算法能够有效地工作。
马尔可夫假设的形式化定义
马尔可夫假设可以形式化地表示为: <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( s t + 1 ∣ s t , a t ) = P ( s t + 1 ∣ h t , a t ) P(s_{t+1} | s_t, a_t) = P(s_{t+1} | h_t, a_t) </math>P(st+1∣st,at)=P(st+1∣ht,at)
其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> s t s_t </math>st 是当前状态。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> a t a_t </math>at 是当前动作。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> s t + 1 s_{t+1} </math>st+1 是下一个状态。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> h t = ( a 1 , o 1 , r 1 , ... , a t , o t , r t ) h_t = (a_1, o_1, r_1, \ldots, a_t, o_t, r_t) </math>ht=(a1,o1,r1,...,at,ot,rt) 是历史信息,包括所有过去的观测、状态和动作序列。
根据马尔可夫假设,下一个状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t + 1 s_{t+1} </math>st+1 的概率分布仅依赖于当前状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t s_t </math>st 和当前动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a t a_t </math>at,而与过去的历史信息 <math xmlns="http://www.w3.org/1998/Math/MathML"> h t h_t </math>ht 无关。这意味着,在给定当前状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t s_t </math>st 和动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a t a_t </math>at 的情况下,下一个状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t + 1 s_{t+1} </math>st+1 的概率分布仅依赖于当前状态和动作,而与之前的状态和动作无关。
马尔可夫假设的应用
- 简化模型 :
- 通过马尔可夫假设,我们可以将复杂的序列决策问题简化为一系列独立的决策步骤。
- 这样可以大大减少需要存储和处理的信息量。
- 动态规划和强化学习算法 :
- 许多强化学习算法,如值迭代、策略迭代、Q-learning ,都基于马尔可夫假设。
- 这些算法通过更新状态价值函数或动作价值函数来找到最优策略。
- 预测和控制 :
- 在实际应用中,马尔可夫假设允许我们基于当前状态和动作来预测未来状态,从而进行有效的控制和决策。
2. 价值函数 (Value Function)
- 状态价值函数 (State Value Function) : <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s) 表示在策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 下,从状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 开始的期望累积奖励。
- 动作价值函数 (Action Value Function) : <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π ( s , a ) Q^\pi(s, a) </math>Qπ(s,a) 示在策略 <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 的期望累积奖励。
3. 贝尔曼方程 (Bellman Equations)
- 状态价值函数的贝尔曼方程 :
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V π ( s ) = R ( s ) + γ ∑ s ′ P ( s ′ ∣ s , π ( s ) ) V π ( s ′ ) V^\pi(s) = R(s) + \gamma \sum_{s'} P(s' | s, \pi(s)) V^\pi(s') </math>Vπ(s)=R(s)+γs′∑P(s′∣s,π(s))Vπ(s′)
详细公式: <math xmlns="http://www.w3.org/1998/Math/MathML"> G t = R t + γ R t + 1 + γ 2 R t + 2 + ⋯ + γ H − 1 R t + H − 1 G_t = R_{t} + \gamma R_{t+1} + \gamma^2 R_{t+2} + \cdots + \gamma^{H-1} R_{t+H-1} </math>Gt=Rt+γRt+1+γ2Rt+2+⋯+γH−1Rt+H−1 <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) = E [ G t ∣ s t = s ] = E [ R t + γ R t + 1 + γ 2 R t + 2 + ⋯ + γ H − 1 R t + H − 1 ∣ s t = s ] V(s) = \mathbb{E}[G_t | s_t = s] = \mathbb{E}[R_{t} + \gamma R_{t+1} + \gamma^2 R_{t+2} + \cdots + \gamma^{H-1} R_{t+H-1} | s_t = s] </math>V(s)=E[Gt∣st=s]=E[Rt+γRt+1+γ2Rt+2+⋯+γH−1Rt+H−1∣st=s]
- 动作价值函数的贝尔曼方程 :
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Q π ( s , a ) = R ( s , a ) + γ ∑ s ′ P ( s ′ ∣ s , a ) V π ( s ′ ) Q^\pi(s, a) = R(s, a) + \gamma \sum_{s'} P(s' | s, a) V^\pi(s') </math>Qπ(s,a)=R(s,a)+γs′∑P(s′∣s,a)Vπ(s′)
4. 最优策略 (Optimal Policy)
-
最优状态价值函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V ∗ ( s ) = max π V π ( s ) V^*(s) = \max_\pi V^\pi(s) </math>V∗(s)=πmaxVπ(s)
-
最优动作价值函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Q ∗ ( s , a ) = max π Q π ( s , a ) Q^*(s, a) = \max_\pi Q^\pi(s, a) </math>Q∗(s,a)=πmaxQπ(s,a)
-
最优策略 : <math xmlns="http://www.w3.org/1998/Math/MathML"> π ∗ ( s ) = arg max a V π ( s ) \pi^*(s) = \arg\max_{a} V^{\pi}(s) </math>π∗(s)=argmaxaVπ(s)
5. 值迭代 (Value Iteration)
Value Iteration Algorithm
值迭代是一种动态规划算法,用于在马尔可夫决策过程(MDP)中找到最优策略。它通过迭代更新状态价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) V(s) </math>V(s),直到收敛到最优价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V ∗ ( s ) V^*(s) </math>V∗(s).
值迭代的步骤
-
初始化:
- 初始化所有状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 的价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V 0 ( s ) V_0(s) </math>V0(s)。通常将其设置为零或某个任意值。
-
迭代:
- 对于每个状态 ( s ),使用贝尔曼最优方程更新价值函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V k + 1 ( s ) = max a [ R ( s , a ) + γ ∑ s ′ p ( s ′ ∣ s , a ) V k ( s ′ ) ] V_{k+1}(s) = \max_a \left[ R(s, a) + \gamma \sum_{s'} p(s' | s, a) V_k(s') \right] </math>Vk+1(s)=amax[R(s,a)+γs′∑p(s′∣s,a)Vk(s′)] - 重复对所有状态进行更新,直到收敛。
- 对于每个状态 ( s ),使用贝尔曼最优方程更新价值函数:
-
收敛:
- 过程继续进行,直到价值函数的变化小于某个小阈值 <math xmlns="http://www.w3.org/1998/Math/MathML"> ϵ \epsilon </math>ϵ 或者达到固定次数的迭代。
-
策略提取:
- 一旦价值函数收敛,可以通过选择每个状态下最大化价值函数的动作来得出最优策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π ∗ ( s ) \pi^*(s) </math>π∗(s):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> π ∗ ( s ) = arg max a [ R ( s , a ) + γ ∑ s ′ p ( s ′ ∣ s , a ) V ∗ ( s ′ ) ] \pi^*(s) = \arg\max_a \left[ R(s, a) + \gamma \sum_{s'} p(s' | s, a) V^*(s') \right] </math>π∗(s)=argamax[R(s,a)+γs′∑p(s′∣s,a)V∗(s′)]
- 一旦价值函数收敛,可以通过选择每个状态下最大化价值函数的动作来得出最优策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π ∗ ( s ) \pi^*(s) </math>π∗(s):
其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> V k ( s ) V_k(s) </math>Vk(s) 是第 ( k ) 迭代时的状态价值函数。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> R ( s , a ) R(s, a) </math>R(s,a) 是在状态 ( s ) 下采取动作 ( a ) 的即时奖励。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> p ( s ′ ∣ s , a ) p(s' | s, a) </math>p(s′∣s,a) 是从状态 ( s ) 通过动作 ( a ) 转移到状态 ( s' ) 的转移概率。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ 是折扣因子。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> V k ( s ′ ) V_k(s') </math>Vk(s′) 是第 ( k ) 迭代时的状态 ( s' ) 的价值。
6. 策略迭代 (Policy Iteration)
- 步骤 :
- 初始化策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π 0 \pi_0 </math>π0。
- 策略评估 (Policy Evaluation) :
- 定当前策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π k \pi_k </math>πk,计算对应的值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π k ( s ) V_{\pi_k}(s) </math>Vπk(s)。
- 使用贝尔曼期望方程进行迭代,直到值函数收敛。
- 策略改进 (Policy Improvement) :
- 根据当前值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π k ( s ) V_{\pi_k}(s) </math>Vπk(s),更新策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π k + 1 \pi_{k+1} </math>πk+1。
- 更新策略为在每个状态下选择具有最大值的动作。
- 重复步骤2和3,直到策略不再改变。
7. 收敛性
- Value Iteration :
- 每次迭代都保证值函数的上界,并最终收敛到最优值函数。
- Policy Iteration :
- 每次策略改进都确保策略的改进,并且策略评估保证了值函数的收敛。
8. 应用
- 动态规划 (Dynamic Programming): 用于求解小规模的 MDP 问题。
- 强化学习 (Reinforcement Learning): 作为基础算法,用于更复杂的环境中的近似方法。
9. 代码实现
-
值迭代:
pythonimport numpy as np # Define the environment parameters num_states = 5 num_actions = 2 discount_factor = 0.9 reward_matrix = np.zeros((num_states, num_actions)) transition_matrix = np.zeros((num_states, num_actions, num_states)) # Initialize the value function V = np.zeros(num_states) # Set up the reward matrix (example values) reward_matrix[0, 0] = 1 reward_matrix[1, 1] = 2 reward_matrix[2, 0] = -1 # Set up the transition matrix (example values) transition_matrix[0, 0, 1] = 1 transition_matrix[1, 1, 2] = 1 transition_matrix[2, 0, 3] = 1 # Value Iteration max_iterations = 1000 threshold = 1e-6 for k in range(max_iterations): delta = 0 for s in range(num_states): v = V[s] for a in range(num_actions): V[s] = max(V[s], reward_matrix[s, a] + discount_factor * np.sum(transition_matrix[s, a] * V)) delta = max(delta, abs(v - V[s])) if delta < threshold: break print("Final Value Function:", V)
-
策略迭代(model-based):
pythonimport numpy as np # Define the environment parameters num_states = 5 num_actions = 2 discount_factor = 0.9 reward_matrix = np.zeros((num_states, num_actions)) transition_matrix = np.zeros((num_states, num_actions, num_states)) # Initialize the value function and policy V = np.zeros(num_states) policy = np.zeros(num_states, dtype=int) # Set up the reward matrix (example values) reward_matrix[0, 0] = 1 reward_matrix[1, 1] = 2 reward_matrix[2, 0] = -1 # Set up the transition matrix (example values) transition_matrix[0, 0, 1] = 1 transition_matrix[1, 1, 2] = 1 transition_matrix[2, 0, 3] = 1 # Policy Iteration max_iterations = 1000 threshold = 1e-6 def policy_evaluation(policy, V): while True: delta = 0 for s in range(num_states): v = V[s] a = policy[s] #注意,这里的reward function和transition matrix是已知的,是model-based的policy evaluation V[s] = reward_matrix[s, a] + discount_factor * np.sum(transition_matrix[s, a] * V) delta = max(delta, abs(v - V[s])) if delta < threshold: break def policy_improvement(V): policy_stable = True for s in range(num_states): old_action = policy[s] best_value = float('-inf') for a in range(num_actions): value = reward_matrix[s, a] + discount_factor * np.sum(transition_matrix[s, a] * V) if value > best_value: best_value = value policy[s] = a #更新policy if old_action != policy[s]: policy_stable = False return policy_stable i = 0 while i == 0 or np.linalg.norm(policy - policy_old, ord=1) > 0: policy_old = policy.copy() # Policy Evaluation policy_evaluation(policy, V) # Policy Improvement policy_stable = policy_improvement(V) if policy_stable: break i += 1 print("Final Policy:", policy) print("Final Value Function:", V)
总结
- 值迭代 和 策略迭代 都是求解 MDP 的有效方法。
- 值迭代 直接更新值函数,而 策略迭代 交替进行策略评估和策略改进。
- 策略迭代 通常需要较少的迭代次数就能收敛,但每次迭代可能比较耗时。
- 值迭代 简单且易于实现,但在大规模问题中可能需要较多的迭代次数。
通过这些方法,可以有效地求解有限状态和动作空间中的最优策略。
Lecture 3 Policy Evaluation
1.Monte Carlo policy evaluation
Monte Carlo (MC) 方法在策略评估中通过收集完整的样本轨迹(episode)来估计价值函数。这种方法不需要环境满足马尔可夫假设,因为它直接利用整个轨迹的奖励信息。
基本原理 样本轨迹:每个样本轨迹是从初始状态开始直到终止状态的一系列状态-动作对。 回报计算:对于每个状态或状态-动作对,MC 方法使用所有从该状态或状态-动作对开始的样本轨迹的平均回报来更新价值函数。 收敛性:随着样本轨迹数量的增加,MC 方法的估计会逐渐收敛到真实的价值函数。 回报公式 对于一个给定的状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s t s_t </math>st,其回报 <math xmlns="http://www.w3.org/1998/Math/MathML"> G t G_t </math>Gt 定义为: <math xmlns="http://www.w3.org/1998/Math/MathML"> G t = r t + γ r t + 1 + γ 2 r t + 2 + ⋯ + γ T i − t r T i G_t = r_{t} + \gamma r_{t+1} + \gamma^2 r_{t+2} + \cdots + \gamma^{T_{i}-t} r_{T_{i}} </math>Gt=rt+γrt+1+γ2rt+2+⋯+γTi−trTi
其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> r t , r t + 1 , ... , r T r_{t}, r_{t+1}, \ldots, r_T </math>rt,rt+1,...,rT 是从时间步 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>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"> s s </math>s 的价值 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s) 定义为从该状态开始,按照策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 采取行动的期望回报。 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) = E τ ∼ π [ G t ∣ s t = s ] V^\pi(s) = E_{\tau \sim \pi} [G_t | s_t = s] </math>Vπ(s)=Eτ∼π[Gt∣st=s]
其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> G t G_t </math>Gt 是从时间步 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t 开始的折扣回报。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> τ \tau </math>τ 是一个完整的样本轨迹。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 是当前策略。 要点与限制
-
需要完整的样本轨迹:
- MC 方法依赖于完整的样本轨迹(episode),这意味着它必须从初始状态开始直到终止状态。
- 对于某些环境,特别是那些终止状态难以达到的环境,这可能是一个问题。
-
高方差:
- 由于 MC 方法依赖于单个样本轨迹的回报,因此估计值可能会有较高的方差。
- 特别是在奖励稀疏或变化较大的环境中,单个样本轨迹的回报可能不足以准确地估计价值函数。
-
不适合在线学习:
- MC 方法通常用于离线学习,因为它需要完整的样本轨迹才能进行更新。
- 在线学习场景中,即时更新和快速响应是必要的,而 MC 方法可能无法满足这些需求。
-
计算效率:
- 收集完整的样本轨迹可能需要大量的计算资源和时间。
- 对于大规模或复杂环境,这可能导致计算效率低下。
-
对探索的要求:
- MC 方法需要充分的探索以确保所有状态都能被访问到。
- 如果某些状态很少被访问到,那么它们的价值估计可能不准确。
-
非马尔可夫环境的处理:
- 虽然 MC 方法不需要环境满足马尔可夫假设,但在非马尔可夫环境中,它仍然需要通过完整的样本轨迹来捕捉历史信息。
- 如果历史信息非常复杂,MC 方法可能需要更多的样本轨迹来准确估计价值函数。
-
收敛速度:
- MC 方法的收敛速度相对较慢,特别是在样本数量较少的情况下。
- 需要大量的样本轨迹才能获得稳定的估计。
-
不适合部分可观测环境:
- 在部分可观测环境中,MC 方法可能需要额外的机制来处理隐藏状态。
- 这增加了算法的复杂性和实现难度。
Monte Carlo 方法的变种
Monte Carlo (MC) 方法有几种不同的变种,每种方法在处理样本轨迹和更新价值函数的方式上有所不同。以下是三种主要的 MC 方法。
1. First-visit Monte Carlo On Policy Evaluation(model-free)
原理:
- 在每个episode中,只考虑第一次访问某个状态的回报。
- 每个状态的价值是所有首次访问该状态的回报的平均值。 公式 : <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) = 1 N ( s ) ∑ i = 1 N ( s ) G i V(s) = \frac{1}{N(s)} \sum_{i=1}^{N(s)} G_i </math>V(s)=N(s)1∑i=1N(s)Gi 其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( s ) N(s) </math>N(s) 是状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 被首次访问的次数。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> G i G_i </math>Gi 是第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i 次首次访问状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 的回报。
2. Every-visit Monte Carlo On Policy Evaluation
原理:
- 在每个episode中,考虑每次访问某个状态的回报。
- 每个状态的价值是所有访问该状态的回报的平均值。 公式 : <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) = 1 N ( s ) ∑ i = 1 N ( s ) G i V(s) = \frac{1}{N(s)} \sum_{i=1}^{N(s)} G_i </math>V(s)=N(s)1∑i=1N(s)Gi 其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> N ( s ) N(s) </math>N(s) 是状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 访问的总次数。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> G i G_i </math>Gi 是第 <math xmlns="http://www.w3.org/1998/Math/MathML"> i i </math>i 访问状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 的回报。
3. Incremental Monte Carlo On Policy Evaluation
原理:
- 使用增量更新来逐步调整价值估计。
- 次访问一个状态时,根据新的回报更新该状态的价值。 公式 : <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) ← V ( s ) + α [ G − V ( s ) ] V(s) \leftarrow V(s) + \alpha [G - V(s)] </math>V(s)←V(s)+α[G−V(s)] 其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) V(s) </math>V(s) 是状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 当前的价值估计。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> G G </math>G 是从当前状态开始到终止状态的回报。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> α \alpha </math>α 是学习率,通常是一个介于0和1之间的常数。
2. Temporal Difference (TD) Learning (model-free)
Temporal Difference (TD) learning 是一种强化学习方法,它通过比较当前状态的预测值和下一个状态的实际值来更新价值函数。TD 方法介于 Monte Carlo 方法和动态规划之间,因为它不需要完整的样本轨迹,也不需要环境的完整动态模型(即状态转移概率和奖励函数),是model-free的。
基本原理
TD 学习的基本思想是使用当前状态的预测值和下一个状态的预测值之间的差值(称为 TD 误差)来更新当前状态的价值估计。
公式
假设我们有一个状态 ( s ),采取动作 ( a ) 后到达状态 ( s' ),并获得奖励 ( r )。TD 学习的目标是更新状态 ( s ) 的价值估计 ( V(s) )。
-
TD(0) 更新公式:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V ( s ) ← V ( s ) + α [ r + γ V ( s ′ ) − V ( s ) ] V(s) \leftarrow V(s) + \alpha [r + \gamma V(s') - V(s)] </math>V(s)←V(s)+α[r+γV(s′)−V(s)]
其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ) V(s) </math>V(s) 是状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 当前的价值估计。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> r r </math>r 是从状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 到状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s ′ s' </math>s′ 的即时奖励。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ 是折扣因子,通常在 0 和 1 之间,用于表示未来奖励的重要性。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> V ( s ′ ) V(s') </math>V(s′) 是状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s ′ s' </math>s′ 的价值估计。
- <math xmlns="http://www.w3.org/1998/Math/MathML"> α \alpha </math>α 是学习率,通常是一个介于 0 和 1 之间的常数。
-
TD 误差:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> δ = r + γ V ( s ′ ) − V ( s ) \delta = r + \gamma V(s') - V(s) </math>δ=r+γV(s′)−V(s)
这个误差反映了当前状态的预测值与下一个状态的实际值之间的差异。
-
更新规则:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V ( s ) ← V ( s ) + α δ V(s) \leftarrow V(s) + \alpha \delta </math>V(s)←V(s)+αδ
3. Certainty Equivalence with Dynamic Programming
Certainty Equivalence 是一种简化决策过程的方法,是一种在动态规划中处理不确定性的方法。它假设当前的模型是准确的,并基于这个模型进行决策。这种方法在控制理论和强化学习中非常常见。它假设当前的决策是基于对未来的最佳预测,即使这些预测可能是不准确的。这种方法通过将不确定性转化为确定性问题来简化决策过程。
核心是 Model-based option for policy evaluation without true models。
-
After each ((s, a, r, s')) tuple
- Recompute maximum likelihood MDP model for ((s, a))
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> P ^ ( s ′ ∣ s , a ) = 1 N ( s , a ) ∑ k = 1 K ∑ t = 1 T k − 1 1 ( s k , t = s , a k , t = a , s k , t + 1 = s ′ ) \hat{P}(s' | s, a) = \frac{1}{N(s, a)} \sum_{k=1}^{K} \sum_{t=1}^{T_k-1} \mathbf{1}(s_{k,t} = s, a_{k,t} = a, s_{k,t+1} = s') </math>P^(s′∣s,a)=N(s,a)1k=1∑Kt=1∑Tk−11(sk,t=s,ak,t=a,sk,t+1=s′)
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> r ^ ( s , a ) = 1 N ( s , a ) ∑ k = 1 K ∑ t = 1 T k − 1 1 ( s k , t = s , a k , t = a ) r t , k \hat{r}(s, a) = \frac{1}{N(s, a)} \sum_{k=1}^{K} \sum_{t=1}^{T_k-1} \mathbf{1}(s_{k,t} = s, a_{k,t} = a) r_{t,k} </math>r^(s,a)=N(s,a)1k=1∑Kt=1∑Tk−11(sk,t=s,ak,t=a)rt,k - Compute <math xmlns="http://www.w3.org/1998/Math/MathML"> V π V^\pi </math>Vπ using MLE MDP
- Recompute maximum likelihood MDP model for ((s, a))
-
Cost : Updating MLE model and MDP planning at each update <math xmlns="http://www.w3.org/1998/Math/MathML"> ( O ( ∣ S ∣ 3 ) (O(|S|^3) </math>(O(∣S∣3) for analytic matrix solution, <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( ∣ S ∣ 2 ∣ A ∣ ) O(|S|^2|A|) </math>O(∣S∣2∣A∣) for iterative methods)
-
Very data efficient and very computationally expensive
-
Consistent (will converge to right estimate for Markov models)
-
Can also easily be used for off-policy evaluation (which we will shortly define and discuss)
4. Batch Policy Evaluation
Batch Policy Evaluation批量策略评估是一种使用从环境中收集的一批数据来评估策略的方法。这种方法不需要与环境进行在线交互,可以在离线状态下进行,因此在基于历史数据评估策略时非常高效。
-
数据收集:
- 集一批数据,形式为 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′),其中 <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 r </math>r 是收到的奖励, <math xmlns="http://www.w3.org/1998/Math/MathML"> s ′ s' </math>s′ 是下一个状态。
- 这些数据可以使用任何策略收集,不一定是正在评估的策略,可以使用重要性采样比率(Importance Sampling Ratio, ISR)等方法纠偏不同策略的差异。
-
策略评估:
- 使用收集的数据来估计价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s) 或动作价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π ( s , a ) Q^\pi(s, a) </math>Qπ(s,a)。
- 评估可以使用多种方法,如蒙特卡洛方法、时序差分(TD)学习或基于模型的方法。
估计状态价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s)
-
蒙特卡洛估计:
- 对于每个状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s,收集所有从 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 开始的轨迹,并计算平均回报。
- 状态价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s) 估计为从状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s 开始的平均回报。
-
时序差分(TD)学习:
- 使用贝尔曼方程更新价值函数:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V π ( s ) ← V π ( s ) + α [ r + γ V π ( s ′ ) − V π ( s ) ] V^\pi(s) \leftarrow V^\pi(s) + \alpha [r + \gamma V^\pi(s') - V^\pi(s)] </math>Vπ(s)←Vπ(s)+α[r+γVπ(s′)−Vπ(s)] - 其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> α \alpha </math>α 是学习率, <math xmlns="http://www.w3.org/1998/Math/MathML"> γ \gamma </math>γ 是折扣因子, <math xmlns="http://www.w3.org/1998/Math/MathML"> s ′ s' </math>s′ 是下一个状态。
- 使用贝尔曼方程更新价值函数:
-
基于模型的方法:
- 从数据中估计转移概率 <math xmlns="http://www.w3.org/1998/Math/MathML"> P ( s ′ ∣ s , a ) P(s' | s, a) </math>P(s′∣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" display="block"> V π ( s ) = ∑ a π ( a ∣ s ) [ R ( s , a ) + γ ∑ s ′ P ( s ′ ∣ s , a ) V π ( s ′ ) ] V^\pi(s) = \sum_a \pi(a | s) \left[ R(s, a) + \gamma \sum_{s'} P(s' | s, a) V^\pi(s') \right] </math>Vπ(s)=a∑π(a∣s)[R(s,a)+γs′∑P(s′∣s,a)Vπ(s′)]
总结
- 批量策略评估 使用从环境中收集的一批数据来评估策略。
- 数据收集 :收集 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′) 形式的数据。
- 策略评估:使用蒙特卡洛方法、时序差分(TD)学习或基于模型的方法来估计价值函数。
- 优点 :
- 离线评估:不需要与环境进行在线交互。
- 高效:当使用大量数据时,可以非常高效。
- 缺点 :
- 偏差:如果数据收集策略与被评估的策略差异较大,可能会产生偏差。
- 样本效率:可能需要大量数据才能获得准确的估计。
Lecture 4 Model Free Control + Function Approximation
在上一节,我们介绍了 Policy evaluation with no knowledge of how the world works ( MDP model not given )。这一节,将介绍 Model Free Control 和 Function Approximation。
Policy Improvement
1. Generalized Policy Improvement
Generalized Policy Improvement (GPI) 是一种在强化学习中广泛使用的策略改进方法。它结合了策略评估(Policy Evaluation)和策略改进(Policy Improvement)的迭代过程,以逐步优化策略。GPI 的核心思想是通过交替进行策略评估和策略改进来不断逼近最优策略。
GPI 的基本步骤
-
策略评估(Policy Evaluation):
- 估计当前策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π 的价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s)。
- 一步可以使用蒙特卡洛方法、时序差分(TD)学习或基于模型的方法。
-
策略改进(Policy Improvement):
- 根据当前的价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s),更新策略 <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"> Q π ( s , a ) Q^\pi(s, a) </math>Qπ(s,a) 最大。
-
重复上述步骤:
- 通过不断迭代策略评估和策略改进,逐步逼近最优策略。
2. 策略探索问题: ϵ-Greedy Policies
在强化学习中,ϵ-greedy policies 是一种常用的策略选择方法,用于平衡探索(exploration)和利用(exploitation)。这种方法在选择动作时,以一定的概率选择当前已知最优的动作(即贪心动作),以一定的概率随机选择其他动作。这种策略有助于算法在早期阶段探索环境,而在后期阶段利用已有的知识。
-
贪心动作 Greedy Action:
- 贪心动作是指在给定状态下选择具有最大期望回报的动作。
- 用 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ( s , a ) Q(s, a) </math>Q(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"> a ∗ a^* </math>a∗ 可以表示为: <math xmlns="http://www.w3.org/1998/Math/MathML"> a ∗ = arg max a Q ( s , a ) a^* = \arg\max_a Q(s, a) </math>a∗=argmaxaQ(s,a)
-
随机动作 Random Action:
- 随机动作是指从所有可能的动作中随机选择一个动作,即以一定概率随机选择其他动作。
-
ϵ 参数:
- ϵ 是一个介于 0 和 1 之间的参数,表示选择随机动作的概率,控制探索与利用的平衡。
- 1 - 表示选择贪心动作的概率。
数学表达
对于每个状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s,动作的选择可以定义为:
- 以概率 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 − ϵ 1 - \epsilon </math>1−ϵ 选择贪心动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a ∗ a^* </math>a∗:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> a ∗ = arg max a Q ( s , a ) a^* = \arg\max_a Q(s, a) </math>a∗=argamaxQ(s,a) - 以概率 <math xmlns="http://www.w3.org/1998/Math/MathML"> ϵ \epsilon </math>ϵ 机选择一个动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> a ∼ Uniform ( A ) a \sim \text{Uniform}(A) </math>a∼Uniform(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"> Q ( s , a ) Q(s, a) </math>Q(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" display="block"> π ( a ∣ s ) = { 1 − ϵ + ϵ ∣ A ∣ if a = arg max a Q ( s , a ) ϵ ∣ A ∣ otherwise \pi(a | s) = \begin{cases} 1 - \epsilon + \frac{\epsilon}{|A|} & \text{if } a = \arg\max_a Q(s, a) \\ \frac{\epsilon}{|A|} & \text{otherwise} \end{cases} </math>π(a∣s)={1−ϵ+∣A∣ϵ∣A∣ϵif a=argmaxaQ(s,a)otherwise
这里, <math xmlns="http://www.w3.org/1998/Math/MathML"> ∣ A ∣ |A| </math>∣A∣ 表示动作空间的大小。
通过使用 ϵ-greedy 策略,可以在早期阶段有效地探索环境,并在后期阶段利用已有的知识。这使得强化学习算法能够在复杂环境中找到近似最优的策略。
Monotonic ϵ-greedy Policy Improvement 定理
Monotonic ϵ-greedy Policy Improvement Theorem: For any ϵ-greedy policy <math xmlns="http://www.w3.org/1998/Math/MathML"> π i \pi_i </math>πi, the ϵ-greedy policy w.r.t. <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π i Q^{\pi_i} </math>Qπi, <math xmlns="http://www.w3.org/1998/Math/MathML"> π i + 1 \pi_{i+1} </math>πi+1 is a monotonic improvement <math xmlns="http://www.w3.org/1998/Math/MathML"> V π i + 1 ≥ V π i V^{\pi_{i+1}} \geq V^{\pi_i} </math>Vπi+1≥Vπi
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Q π i ( s , π i + 1 ( s ) ) = ∑ a ∈ A π i + 1 ( a ∣ s ) Q π i ( s , a ) Q^{\pi_i}(s, \pi_{i+1}(s)) = \sum_{a \in A} \pi_{i+1}(a|s) Q^{\pi_i}(s, a) </math>Qπi(s,πi+1(s))=a∈A∑πi+1(a∣s)Qπi(s,a)
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> = ( ϵ / ∣ A ∣ ) [ ∑ a ∈ A Q π i ( s , a ) ] + ( 1 − ϵ ) max a Q π i ( s , a ) = (\epsilon / |A|) \left[ \sum_{a \in A} Q^{\pi_i}(s, a) \right] + (1 - \epsilon) \max_a Q^{\pi_i}(s, a) </math>=(ϵ/∣A∣)[a∈A∑Qπi(s,a)]+(1−ϵ)amaxQπi(s,a)
Value Function Calculation:
- Substituting the definition of <math xmlns="http://www.w3.org/1998/Math/MathML"> π i + 1 ( a ∣ s ) \pi_{i+1}(a|s) </math>πi+1(a∣s) into the value function update equation:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V π i + 1 ( s ) = ∑ a ∈ A π i + 1 ( a ∣ s ) Q π i ( s , a ) V^{\pi_{i+1}}(s) = \sum_{a \in A} \pi_{i+1}(a|s) Q^{\pi_i}(s, a) </math>Vπi+1(s)=a∈A∑πi+1(a∣s)Qπi(s,a) - This can be expanded as:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V π i + 1 ( s ) = ( 1 − ϵ + ϵ ∣ A ∣ ) max a Q π i ( s , a ) + ∑ a ≠ arg max a Q π i ( s , a ) ϵ ∣ A ∣ Q π i ( s , a ) V^{\pi_{i+1}}(s) = \left(1 - \epsilon + \frac{\epsilon}{|A|}\right) \max_a Q^{\pi_i}(s, a) + \sum_{a \neq \arg\max_a Q^{\pi_i}(s, a)} \frac{\epsilon}{|A|} Q^{\pi_i}(s, a) </math>Vπi+1(s)=(1−ϵ+∣A∣ϵ)amaxQπi(s,a)+a=argmaxaQπi(s,a)∑∣A∣ϵQπi(s,a) - Simplifying the expression:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V π i + 1 ( s ) = ( 1 − ϵ ) max a Q π i ( s , a ) + ϵ ∣ A ∣ ∑ a ∈ A Q π i ( s , a ) V^{\pi_{i+1}}(s) = (1 - \epsilon) \max_a Q^{\pi_i}(s, a) + \frac{\epsilon}{|A|} \sum_{a \in A} Q^{\pi_i}(s, a) </math>Vπi+1(s)=(1−ϵ)amaxQπi(s,a)+∣A∣ϵa∈A∑Qπi(s,a)
3. Monte Carlo Online Control ( On Policy )
计算流程:
4. GLIE条件:Greedy in the Limit of Infinite Exploration
GLIE 是 "Greedy in the Limit with Infinite Exploration" 的缩写,它是一种在强化学习中用于策略改进和探索的条件。GLIE 条件确保了在无限次迭代或探索过程中,算法能够收敛到最优策略。
定义 GLIE 条件包括两个主要部分:
-
贪心性 (Greedy):
- 在每个时间步 <math xmlns="http://www.w3.org/1998/Math/MathML"> t t </math>t,策略 <math xmlns="http://www.w3.org/1998/Math/MathML"> π t \pi_t </math>πt 选择的动作是基于当前估计的价值函数 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q t Q_t </math>Qt 心选择的。
- 对于每个状态 <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s, <math xmlns="http://www.w3.org/1998/Math/MathML"> π t ( s ) \pi_t(s) </math>πt(s) 选择的是使 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q t ( s , a ) Q_t(s, a) </math>Qt(s,a) 最大的动作 <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a。
-
无限探索 (Infinite Exploration):
- 对于每个状态 <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,随着时间的推移,该状态-动作对被访问的次数趋于无穷大。
- 这意味着所有可能的状态-动作对都会被充分探索。
数学表示 假设我们有一个状态空间 (S) 和动作空间 (A),以及一个价值函数 (Q_t(s, a))。GLIE 件可以表示为:
- 贪心性:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> lim t → ∞ π t ( a ∣ s ) = { 1 if a = arg max a Q t ( s , a ) 0 otherwise \lim_{t \to \infty} \pi_t(a|s) = \begin{cases} 1 & \text{if } a = \arg\max_a Q_t(s, a) \\ 0 & \text{otherwise} \end{cases} </math>t→∞limπt(a∣s)={10if a=argmaxaQt(s,a)otherwise - 无限探索:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∑ t = 1 ∞ π t ( a ∣ s ) > 0 for all s ∈ S , a ∈ A \sum_{t=1}^{\infty} \pi_t(a|s) > 0 \quad \text{for all } s \in S, a \in A </math>t=1∑∞πt(a∣s)>0for all s∈S,a∈A
应用 GLIE 条件通常用于保证强化学习算法(如 Q-learning,GLIE Monte-Carlo control)的收敛性。通过满足 GLIE 条件,算法能够在无限次迭代中收敛到最优策略。
示例 一个常见的满足 GLIE 条件的策略是 ϵ-greedy 略。在这种策略中:
- 贪心性:
- 大多数情况下选择当前估计的最佳动作。
- 即 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( 1 − ϵ ) (1 - \epsilon) </math>(1−ϵ) 的概率选择贪心动作。
- 无限探索:
- 以小概率 <math xmlns="http://www.w3.org/1998/Math/MathML"> ϵ \epsilon </math>ϵ 随机选择其他动作,确保所有动作都被探索。
- 随着时间的推移, <math xmlns="http://www.w3.org/1998/Math/MathML"> ϵ \epsilon </math>ϵ 逐渐减小,但始终保持大于零,以确保无限次探索。
总结
- 贪心性:确保在大多数情况下选择当前最优的动作。
- 无限探索:确保所有状态-动作对都被充分探索。
通过满足 GLIE 条件,强化学习算法可以在无限次迭代中收敛到最优策略,从而保证算法的稳定性和有效性。
4. SARSA ( On Policy )
SARSA (State-Action-Reward-State-Action)是一种强化学习算法,它基于当前策略更新 Q 值。与 Q-learning(off-policy)不同,SARSA 是on-policy。这意味着 SARSA 使用相同的策略来生成动作并更新 Q 值。
Properties of SARSA with ϵ-greedy policies有一些关键特性:
-
Result builds on stochastic approximation
- 这个结果基于随机逼近理论。随机逼近是一种数学方法,用于处理随机过程中的优化问题。
-
Relies on step sizes decreasing at the right rate
- 依赖于步长以正确的速率减小。这意味着学习率(步长)需要随着迭代次数的增加而逐渐减小,以确保算法收敛。
-
Relies on Bellman backup contraction property
- 依赖于 Bellman 备份收缩性质。Bellman 备份是指在强化学习中更新价值函数的过程,而收缩性质保证了这个过程是稳定的,并且最终会收敛到最优解。
-
Relies on bounded rewards and value function
- 依赖于有界的奖励和价值函数。这意味着奖励和价值函数必须在某个有限范围内,以确保算法的稳定性和收敛性。
5. Q-Learning ( Off Policy ): Learning the Optimal State-Action Value
On-policy vs Off-policy
-
On-policy learning:
- 在策略学习直接使用当前策略生成的数据来更新策略。这意味着算法只使用那些根据当前策略生成的样本。
- 例如,SARSA 法就是一种典型的在策略学习方法,它使用实际采取的动作来更新 Q 。
-
Off-policy learning:
- 离策略学习使用从其他策略生成的数据来更新当前策略。这意味着算法可以使用任何策略生成的数据来改进当前策略。
- 例如,Q-learning 是一种典型的离策略学习方法,它使用最佳动作的价值来更新 Q 值,而不仅仅是当前策略选择的动作。
Q-Learning计算流程
Model Free Value Function Approximation
Motivation for Function Approximation
-
Avoid explicitly storing or learning the following for every single state and action:
- Dynamics
- Reward model
- Value
- State-action value
- Policy
-
Want more compact representation that generalizes across states and actions:
- Reduce memory needed to store <math xmlns="http://www.w3.org/1998/Math/MathML"> ( P , R ) / V / Q / π (P, R)/V/Q/\pi </math>(P,R)/V/Q/π
- Reduce computation needed to compute <math xmlns="http://www.w3.org/1998/Math/MathML"> ( P , R ) / V / Q / π (P, R)/V/Q/\pi </math>(P,R)/V/Q/π
- Reduce experience needed to find a good <math xmlns="http://www.w3.org/1998/Math/MathML"> ( P , R ) / V / Q / π (P, R)/V/Q/\pi </math>(P,R)/V/Q/π
State Action Value Function Approximation for Policy Evaluation with an Oracle
-
First assume we could query any state <math xmlns="http://www.w3.org/1998/Math/MathML"> s s </math>s and action <math xmlns="http://www.w3.org/1998/Math/MathML"> a a </math>a and an oracle would return the true value for <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π ( s , a ) Q^\pi(s, a) </math>Qπ(s,a)
-
Similar to supervised learning: assume given <math xmlns="http://www.w3.org/1998/Math/MathML"> ( ( s , a ) , Q π ( s , a ) ) ((s, a), Q^\pi(s, a)) </math>((s,a),Qπ(s,a)) pairs
-
The objective is to find the best approximate representation of <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π Q^\pi </math>Qπ given a particular parameterized function <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ^ ( s , a ; w ) \hat{Q}(s, a; w) </math>Q^(s,a;w)
Stochastic Gradient Descent
-
Goal : Find the parameter vector <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w that minimizes the loss between a true value function <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π ( s , a ) Q^\pi(s, a) </math>Qπ(s,a) and its approximation <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ^ ( s , a ; w ) \hat{Q}(s, a; \mathbf{w}) </math>Q^(s,a;w).
-
Generally use mean squared error (MSE) to define the loss:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> J ( w ) = E π [ ( Q π ( s , a ) − Q ^ ( s , a ; w ) ) 2 ] J(\mathbf{w}) = \mathbb{E}_\pi \left[ \left( Q^\pi(s, a) - \hat{Q}(s, a; \mathbf{w}) \right)^2 \right] </math>J(w)=Eπ[(Qπ(s,a)−Q^(s,a;w))2]
-
Can use gradient descent to find a local minimum
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ w = − α ∇ w J ( w ) \Delta \mathbf{w} = -\alpha \nabla_\mathbf{w} J(\mathbf{w}) </math>Δw=−α∇wJ(w)
-
Stochastic gradient descent (SGD) uses a finite number of samples to compute an approximate gradient:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> ∇ w J ( w ) = ∇ w E π [ ( Q π ( s , a ) − Q ^ ( s , a ; w ) ) 2 ] \nabla_w J(w) = \nabla_w E_\pi \left[ (Q^\pi(s, a) - \hat{Q}(s, a; w))^2 \right] </math>∇wJ(w)=∇wEπ[(Qπ(s,a)−Q^(s,a;w))2]
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> = − 2 E π [ ( Q π ( s , a ) − Q ^ ( s , a ; w ) ] ∇ w Q ^ ( s , a ; w ) = -2 E_\pi \left[ (Q^\pi(s, a) - \hat{Q}(s, a; w) \right]\nabla_w \hat{Q}(s, a; w) </math>=−2Eπ[(Qπ(s,a)−Q^(s,a;w)]∇wQ^(s,a;w)
- Expected SGD is the same as the full gradient update
1. Policy Evaluation
回顾我们之前学过的 model-free policy evaluation:
- Following a fixed policy <math xmlns="http://www.w3.org/1998/Math/MathML"> π \pi </math>π (or had access to prior data)
- Goal is to estimate <math xmlns="http://www.w3.org/1998/Math/MathML"> V π V^\pi </math>Vπ and/or <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π Q^\pi </math>Qπ
- Maintained a lookup table to store estimates <math xmlns="http://www.w3.org/1998/Math/MathML"> V π V^\pi </math>Vπ and/or <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π Q^\pi </math>Qπ
- Updated these estimates after each episode (Monte Carlo methods) or after each step (TD methods)
而现在在VFA中: Now in value function approximation(VFA), change the estimate update step to include fitting the function approximator
2. Monte Carlo Value Function Approximation
- Return <math xmlns="http://www.w3.org/1998/Math/MathML"> G t G_t </math>Gt is an unbiased but noisy sample of the true expected return <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π ( s t , a t ) Q^\pi(s_t, a_t) </math>Qπ(st,at).
- Therefore can reduce MC VFA to doing supervised learning on a set of (state, action, return) pairs: <math xmlns="http://www.w3.org/1998/Math/MathML"> ⟨ ( s 1 , a 1 ) , G 1 ⟩ , ⟨ ( s 2 , a 2 ) , G 2 ⟩ , ... , ⟨ ( s T , a T ) , G T ⟩ \langle (s_1, a_1), G_1 \rangle, \langle (s_2, a_2), G_2 \rangle, \ldots, \langle (s_T, a_T), G_T \rangle </math>⟨(s1,a1),G1⟩,⟨(s2,a2),G2⟩,...,⟨(sT,aT),GT⟩
- Substitute <math xmlns="http://www.w3.org/1998/Math/MathML"> G t G_t </math>Gt for the true <math xmlns="http://www.w3.org/1998/Math/MathML"> Q π ( s t , a t ) Q^\pi(s_t, a_t) </math>Qπ(st,at) when fit function approximator
3. Temporal Difference TD(0) Policy Evaluation
-
Uses bootstrapping and sampling to approximate true value <math xmlns="http://www.w3.org/1998/Math/MathML"> V π V^\pi </math>Vπ
-
Updates estimate <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s) after each transition <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′):
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> V π ( s ) = V π ( s ) + α ( r + γ V π ( s ′ ) − V π ( s ) ) V^\pi(s) = V^\pi(s) + \alpha(r + \gamma V^\pi(s') - V^\pi(s)) </math>Vπ(s)=Vπ(s)+α(r+γVπ(s′)−Vπ(s))
- Target is <math xmlns="http://www.w3.org/1998/Math/MathML"> r + γ V π ( s ′ ) r + \gamma V^\pi(s') </math>r+γVπ(s′),
- a biased estimate of the true value <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s)
-
In value function approximation:
- Target is <math xmlns="http://www.w3.org/1998/Math/MathML"> r + γ V ^ π ( s ′ ; w ) r + \gamma \hat{V}^\pi(s'; \mathbf{w}) </math>r+γV^π(s′;w)
- a biased estimate of the true value <math xmlns="http://www.w3.org/1998/Math/MathML"> V π ( s ) V^\pi(s) </math>Vπ(s)
-
3 forms of approximation:
- Sampling
- Bootstrapping
- Value function approximation
-
Can reduce doing TD(0) learning with value function approximation to supervised learning on a set of (state, return) pairs: <math xmlns="http://www.w3.org/1998/Math/MathML"> ⟨ s 1 , r 1 + γ V ^ π ( s 2 ; w ) ⟩ , ⟨ s 2 , r 2 + γ V ^ ( s 3 ; w ) ⟩ , ... \langle s_1, r_1 + \gamma \hat{V}^\pi(s_2; \mathbf{w}) \rangle, \langle s_2, r_2 + \gamma \hat{V}(s_3; \mathbf{w}) \rangle, \ldots </math>⟨s1,r1+γV^π(s2;w)⟩,⟨s2,r2+γV^(s3;w)⟩,...
-
Find weights to minimize mean squared error
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> J ( w ) = E π [ ( r j + γ V ^ π ( s j + 1 ; w ) − V ^ ( s j ; w ) ) 2 ] J(\mathbf{w}) = \mathbb{E}\pi[(r_j + \gamma \hat{V}^\pi(s{j+1}; \mathbf{w}) - \hat{V}(s_j; \mathbf{w}))^2] </math>J(w)=Eπ[(rj+γV^π(sj+1;w)−V^(sj;w))2]
-
Use stochastic gradient descent, as in MC methods
Control using Value Function Approximation
1. Control using Value Function Approximation
-
Use value function approximation to represent state-action values
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Q ^ π ( s , a ; w ) ≈ Q π ( s , a ) \hat{Q}^\pi(s, a; \mathbf{w}) \approx Q^\pi(s, a) </math>Q^π(s,a;w)≈Qπ(s,a)
-
Interleave:
- Approximate policy evaluation using value function approximation
- Perform <math xmlns="http://www.w3.org/1998/Math/MathML"> ϵ \epsilon </math>ϵ-greedy policy improvement
-
"Deadly Triad" can lead to oscillations or lack of convergence
- Function approximation
- Bootstrapping
- Off-policy learning ( e.g. Q -learning)
Incremental Model-Free Control Approaches
-
Similar to policy evaluation, true state-action value function for a state is unknown and so substitute a target value for true <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ( s t , a t ) Q(s_t, a_t) </math>Q(st,at)
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ w = α ( Q ( s t , a t ) − Q ^ ( s t , a t ; w ) ) ∇ w Q ^ ( s t , a t ; w ) \Delta \mathbf{w} = \alpha (Q(s_t, a_t) - \hat{Q}(s_t, a_t; \mathbf{w})) \nabla_{\mathbf{w}} \hat{Q}(s_t, a_t; \mathbf{w}) </math>Δw=α(Q(st,at)−Q^(st,at;w))∇wQ^(st,at;w)
-
Monte Carlo methods : use a return <math xmlns="http://www.w3.org/1998/Math/MathML"> G t G_t </math>Gt as a substitute target
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ w = α ( G t − Q ^ ( s t , a t ; w ) ) ∇ w Q ^ ( s t , a t ; w ) \Delta \mathbf{w} = \alpha (G_t - \hat{Q}(s_t, a_t; \mathbf{w})) \nabla_{\mathbf{w}} \hat{Q}(s_t, a_t; \mathbf{w}) </math>Δw=α(Gt−Q^(st,at;w))∇wQ^(st,at;w)
-
SARSA : Use TD target <math xmlns="http://www.w3.org/1998/Math/MathML"> r + γ Q ^ ( s ′ , a ′ ; w ) r + \gamma \hat{Q}(s', a'; \mathbf{w}) </math>r+γQ^(s′,a′;w) which leverages the current function approximation value
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ w = α ( r + γ Q ^ ( s ′ , a ′ ; w ) − Q ^ ( s , a ; w ) ) ∇ w Q ^ ( s , a ; w ) \Delta \mathbf{w} = \alpha (r + \gamma \hat{Q}(s', a'; \mathbf{w}) - \hat{Q}(s, a; \mathbf{w})) \nabla_{\mathbf{w}} \hat{Q}(s, a; \mathbf{w}) </math>Δw=α(r+γQ^(s′,a′;w)−Q^(s,a;w))∇wQ^(s,a;w)
-
Q-learning : Uses related TD target <math xmlns="http://www.w3.org/1998/Math/MathML"> r + γ max a ′ Q ^ ( s ′ , a ′ ; w ) r + \gamma \max_{a'} \hat{Q}(s', a'; \mathbf{w}) </math>r+γmaxa′Q^(s′,a′;w)
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ w = α ( r + γ max a ′ Q ^ ( s ′ , a ′ ; w ) − Q ^ ( s , a ; w ) ) ∇ w Q ^ ( s , a ; w ) \Delta \mathbf{w} = \alpha (r + \gamma \max_{a'} \hat{Q}(s', a'; \mathbf{w}) - \hat{Q}(s, a; \mathbf{w})) \nabla_{\mathbf{w}} \hat{Q}(s, a; \mathbf{w}) </math>Δw=α(r+γa′maxQ^(s′,a′;w)−Q^(s,a;w))∇wQ^(s,a;w)
2. Deep Q-Learning
Q-Learning with Neural Networks
-
Q-learning收敛性:
- Q-learning在使用表格表示(tabular representation)时收敛到最优的 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ∗ ( s , a ) Q^*(s, a) </math>Q∗(s,a)。
-
价值函数近似:
- 在价值函数近似中,Q-learning通过最小化均方误差(MSE)损失来逼近目标 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q Q </math>Q ,而不是真实的 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q Q </math>Q 值。
- 使用随机梯度下降(stochastic gradient descent)来实现这一点。
-
Q-learning与价值函数近似的问题:
- Q-learning在使用价值函数近似(VFA)时可能会发散。
-
导致问题的两个主要因素:
- 样本之间的相关性(Correlations between samples)
- 非平稳的目标(Non-stationary targets)
-
Deep Q-Learning (DQN) 解决方案:
- 经验回放(Experience replay)
- 固定Q-目标(Fixed Q-targets)
Experience Replay 经验回放
Experience Replay(经验回放)是强化学习中一种重要的技术,尤其是在使用神经网络进行价值函数近似时。它通过存储和重用历史数据来打破样本之间的相关性,从而提高学习的稳定性和效率。以下是Experience Replay的主要特点和工作原理: 主要特点
-
存储经验:
- 智能体与环境交互产生的经验 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′) 存储在一个经验池(Experience Replay Buffer)中。
- 经验池通常是一个固定大小的队列,新经验会覆盖最旧的经验。
-
随机采样:
- 在每次更新网络时,从经验池中随机抽取一批经验进行训练。
- 种随机采样打破了样本之间的顺序相关性,使得每次更新更加独立。
-
批量训练:
- 使用批量梯度下降(Batch Gradient Descent)方法,从经验池中抽取一个批量的数据进行训练。
- 批量大小的选择会影响学习的稳定性和效率。
工作原理
-
经验收集:
- 智能体在与环境交互时,将每次交互产生的经验 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′) 储到经验池中。
-
随机采样:
- 在每次更新网络时,从经验池中随机抽取一个批量的经验。
- 例如,假设经验池中有 <math xmlns="http://www.w3.org/1998/Math/MathML"> N N </math>N 条经验,每次更新时抽取 <math xmlns="http://www.w3.org/1998/Math/MathML"> B B </math>B 条经验进行训练。
-
计算损失:
- 对于每条经验 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′),计算目标值 <math xmlns="http://www.w3.org/1998/Math/MathML"> y i y_i </math>yi 和当前网络的预测值 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ^ ( s , a ; w ) \hat{Q}(s, a; \mathbf{w}) </math>Q^(s,a;w)。
- 目标值 <math xmlns="http://www.w3.org/1998/Math/MathML"> y i y_i </math>yi 可以是:
- Monte Carlo 方法: <math xmlns="http://www.w3.org/1998/Math/MathML"> y i = G t y_i = G_t </math>yi=Gt
- SARSA 方法: <math xmlns="http://www.w3.org/1998/Math/MathML"> y i = r + γ Q ^ ( s ′ , a ′ ; w ) y_i = r + \gamma \hat{Q}(s', a'; \mathbf{w}) </math>yi=r+γQ^(s′,a′;w)
- Q-learning 方法: <math xmlns="http://www.w3.org/1998/Math/MathML"> y i = r + γ max a ′ Q ^ ( s ′ , a ′ ; w ) y_i = r + \gamma \max_{a'} \hat{Q}(s', a'; \mathbf{w}) </math>yi=r+γmaxa′Q^(s′,a′;w)
-
参数更新:
- 使用均方误差(MSE)损失函数来最小化预测值与目标值之间的差异。
- 更新公式为:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ w = α ( y i − Q ^ ( s , a ; w ) ) ∇ w Q ^ ( s , a ; w ) \Delta \mathbf{w} = \alpha (y_i - \hat{Q}(s, a; \mathbf{w})) \nabla_{\mathbf{w}} \hat{Q}(s, a; \mathbf{w}) </math>Δw=α(yi−Q^(s,a;w))∇wQ^(s,a;w)
优点
- 减少相关性 :
- 随机采样打破了样本之间的顺序相关性,提高了学习的稳定性。
- 提高数据利用率 :
- 经验可以被多次重用,增加了数据的有效利用。
- 平滑学习过程 :
- 通过混合不同时间点的经验,减少了学习过程中的波动。
示例应用 Deep Q-Network (DQN):
- DQN 中使用经验回放来解决样本之间的相关性问题。
- 每次更新时从经验池中随机抽取一批经验进行训练。
Experience Replay 是一种有效的技术,通过存储和重用历史数据来打破样本之间的相关性,从而提高强化学习的稳定性和效率。它在许多深度强化学习算法中得到了广泛应用,特别是在 Deep Q-Network (DQN) 中。
Fixed Q-Target (固定Q目标)
Fixed Q-Target 是强化学习中另一种重要的技术,尤其是在使用神经网络进行价值函数近似时。它通过引入一个固定的、缓慢更新的目标网络来解决非平稳目标(non-stationary targets)的问题,从而提高学习的稳定性和收敛性。以下是 Fixed Q-Target 的主要特点和工作原理:
主要特点
-
在线网络 (Online Network):
- 用于选择动作 和计算当前状态下的价值估计。
- 参数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w
-
目标网络 (Target Network):
- 用于计算目标值 <math xmlns="http://www.w3.org/1998/Math/MathML"> y i y_i </math>yi。
- 参数为 <math xmlns="http://www.w3.org/1998/Math/MathML"> w − \mathbf{w^-} </math>w−
- 目标网络的参数每隔一定时间从在线网络复制过来。
-
目标值计算:
- 使用目标网络来计算目标值,而不是使用在线网络。
- 这样可以减少目标值的波动,使学习过程更加稳定。
工作原理
-
经验收集:
- 能体与环境交互,将每次交互产生的经验 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′) 存储到经验池中。
-
随机采样:
- 在每次更新网络时,从经验池中随机抽取一批经验进行训练。
-
目标值计算:
- 对于每条经验 <math xmlns="http://www.w3.org/1998/Math/MathML"> ( s , a , r , s ′ ) (s, a, r, s') </math>(s,a,r,s′),计算目标值 <math xmlns="http://www.w3.org/1998/Math/MathML"> y i y_i </math>yi。
- 目标值 <math xmlns="http://www.w3.org/1998/Math/MathML"> y i y_i </math>yi 通常定义为:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> y i = r + γ max a ′ Q ^ ( s ′ , a ′ ; w − ) y_i = r + \gamma \max_{a'} \hat{Q}(s', a'; \mathbf{w^-}) </math>yi=r+γa′maxQ^(s′,a′;w−)
其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> Q ^ ( s ′ , a ′ ; θ − ) \hat{Q}(s', a'; \theta^-) </math>Q^(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"> Q ^ ( s , a ; w ) \hat{Q}(s, a; \mathbf{w}) </math>Q^(s,a;w)。
- 使用均方误差(MSE)损失函数来最小化预测值与目标值之间的差异:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> L ( θ ) = E ( s , a , r , s ′ ) ∼ D [ ( y i − Q ^ ( s , a ; w ) ) 2 ] L(\theta) = \mathbb{E}_{(s, a, r, s') \sim D} \left[ (y_i - \hat{Q}(s, a; \mathbf{w}))^2 \right] </math>L(θ)=E(s,a,r,s′)∼D[(yi−Q^(s,a;w))2]
-
参数更新:
- 使用梯度下降方法更新在线网络的参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> Δ θ = α ∇ θ [ ( y i − Q ^ ( s , a ; w ) ) 2 ] \Delta \theta = \alpha \nabla_\theta \left[ (y_i - \hat{Q}(s, a; \mathbf{w}))^2 \right] </math>Δθ=α∇θ[(yi−Q^(s,a;w))2]
- 使用梯度下降方法更新在线网络的参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w:
-
目标网络更新:
- 目标网络的参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> w − \mathbf{w^-} </math>w− 每隔一定时间(例如每 <math xmlns="http://www.w3.org/1998/Math/MathML"> C C </math>C 步)从在线网络的参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w 复制过来:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"> θ − ← θ \theta^- \leftarrow \theta </math>θ−←θ
- 目标网络的参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> w − \mathbf{w^-} </math>w− 每隔一定时间(例如每 <math xmlns="http://www.w3.org/1998/Math/MathML"> C C </math>C 步)从在线网络的参数 <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w 复制过来:
优点
- 减少非平稳目标问题 :
- 通过固定目标网络的参数,减少了目标值的波动,使学习过程更加稳定。
- 提高收敛性 :
- 定目标网络的参数有助于更快地收敛到最优策略。
- 降低过拟合风险 :
- 通过缓慢更新目标网络,降低了过拟合的风险。
示例应用
- Deep Q-Network (DQN):
- DQN 中使用固定Q目标来解决非平稳目标的问题。
- 每次更新时从经验池中随机抽取一批经验进行训练,并使用目标网络计算目标值。
Fixed Q-Target 通过引入一个固定的、缓慢更新的目标网络来解决非平稳目标的问题,从而提高强化学习的稳定性和收敛性。这种方法在许多深度强化学习算法中得到了广泛应用,特别是在 Deep Q-Network (DQN) 中。