DeepSeekMath的理解7——附录2之SFT、DPO、PPO目标函数求导
文章目录
- 一、前言
- 二、DeepSeekMath
- A. 附录
-
- A.1.1. Supervised Fine-tuning
- 问题:SFT目标函数求导
- 第0步:目标函数 (6) 在说什么?
- 第1步:对 θ \theta θ 求梯度
- 第2步:为什么梯度系数是 1?
- 第3步: ∇ θ log π θ \nabla_{\theta} \log \pi_{\theta} ∇θlogπθ 到底是什么?
- 第4步:整个 SFT 训练在做什么?
- 一句话总结推导链
- A.1.4. Direct Preference Optimization (DPO)
- 问题:DPO目标函数求导
- 第0步:先写出DPO的目标函数
- 第1步:对 θ \theta θ 求导(链式法则)
- 第2步:内层导数 ∇ θ u \nabla_{\theta} u ∇θu
- 第3步:好答案的得分怎么求导?
- 第4步:把内外层合起来
- 第5步:梯度系数 G C GC GC 终于出现了!
- 第6步:这个 G C GC GC 到底在干什么?
- 一句话总结推导链
- A.1.5. Proximal Policy Optimization (PPO)
- 问题1:PPO目标函数求导
- 第0步:PPO 原始目标函数 (15)
- 第1步:简化假设 π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ
- 第2步:对 θ \theta θ 求导
- 第3步:为什么梯度系数是 A t A_t At?
- 关键问题:为什么 π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ 还能求导?
- 一句话总结
一、前言
仅供参考,未经实验验证。这篇我们对SFT、DPO、PPO目标函数求导进行推导,由于RFT和在线RFT的目标函数求导与SFT同理(GC都是常数),故跳过了这两个。
二、DeepSeekMath
A. 附录
A.1.1. Supervised Fine-tuning
The objective of Supervised Fine-tuning is maximizing the following objective:
J S F T ( θ ) = E [ q , o ∼ P s f t ( Q , O ) ] ( 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ log π θ ( o t ∣ q , o < t ) ) . ( 6 ) \mathcal{J}_{SFT}(\theta) = \mathbb{E}[q, o \sim P_{sft}(Q, O)] \left( \frac{1}{|o|} \sum_{t=1}^{|o|} \log \pi_{\theta}(o_t | q, o_{<t}) \right). \quad (6) JSFT(θ)=E[q,o∼Psft(Q,O)] ∣o∣1t=1∑∣o∣logπθ(ot∣q,o<t) .(6)
The gradient of J S F T ( θ ) \mathcal{J}_{SFT}(\theta) JSFT(θ) is:
∇ θ J S F T = E [ q , o ∼ P s f t ( Q , O ) ] ( 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ ∇ θ log π θ ( o t ∣ q , o < t ) ) . ( 7 ) \nabla_{\theta} \mathcal{J}_{SFT} = \mathbb{E}[q, o \sim P_{sft}(Q, O)] \left( \frac{1}{|o|} \sum_{t=1}^{|o|} \nabla_{\theta} \log \pi_{\theta}(o_t | q, o_{<t}) \right). \quad (7) ∇θJSFT=E[q,o∼Psft(Q,O)] ∣o∣1t=1∑∣o∣∇θlogπθ(ot∣q,o<t) .(7)
Data Source: The dataset employed for SFT. Reward Function: This can be regarded as human selection. Gradient Coefficient: always set to 1.
问题:SFT目标函数求导
这是所有方法中最基础的,但它是理解后续所有复杂公式的起点。我来一步一步推。
第0步:目标函数 (6) 在说什么?
J S F T ( θ ) = E q , o ∼ P s f t ( Q , O ) ( 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ log π θ ( o t ∣ q , o < t ) ) \mathcal{J}_{SFT}(\theta) = \mathbb{E}_{q, o \sim P_{sft}(Q, O)} \left( \frac{1}{|o|} \sum_{t=1}^{|o|} \log \pi_{\theta}(o_t | q, o_{<t}) \right) JSFT(θ)=Eq,o∼Psft(Q,O) ∣o∣1t=1∑∣o∣logπθ(ot∣q,o<t)
拆解每个符号
| 符号 | 含义 |
|---|---|
| E q , o ∼ P s f t \mathbb{E}_{q, o \sim P_{sft}} Eq,o∼Psft | 从人工标注的 SFT 数据集中采样问题 q q q 和答案 o o o |
| ∣ o ∣ \lvert o \rvert ∣o∣ | 答案 o o o 的 token 数量(序列长度) |
| o t o_t ot | 答案的第 t t t 个 token |
| o < t o_{<t} o<t | 答案中第 t t t 个 token 之前的所有 token(上下文) |
| π θ ( o t ∣ q , o < t ) \pi_{\theta}(o_t | q, o_{<t}) πθ(ot∣q,o<t) | 当前模型在给定问题和前文下,生成第 t t t 个 token 的概率 |
| log π θ ( … ) \log \pi_{\theta}(\dots) logπθ(…) | 对概率取 log(把乘法变加法,方便求导) |
直观理解
这是一个平均对数似然(Average Log-Likelihood):
1 ∣ o ∣ ∑ t = 1 ∣ o ∣ log π θ ( o t ∣ q , o < t ) \frac{1}{|o|} \sum_{t=1}^{|o|} \log \pi_{\theta}(o_t | q, o_{<t}) ∣o∣1t=1∑∣o∣logπθ(ot∣q,o<t)
-
对于一条标准答案 o o o,模型生成这个答案的联合概率是:
P ( o ∣ q ) = π θ ( o 1 ∣ q ) × π θ ( o 2 ∣ q , o 1 ) × π θ ( o 3 ∣ q , o 1 , o 2 ) × … P(o|q) = \pi_{\theta}(o_1|q) \times \pi_{\theta}(o_2|q,o_1) \times \pi_{\theta}(o_3|q,o_1,o_2) \times \dots P(o∣q)=πθ(o1∣q)×πθ(o2∣q,o1)×πθ(o3∣q,o1,o2)×… -
取 log 后,乘法变加法:
log P ( o ∣ q ) = ∑ t = 1 ∣ o ∣ log π θ ( o t ∣ q , o < t ) \log P(o|q) = \sum_{t=1}^{|o|} \log \pi_{\theta}(o_t | q, o_{<t}) logP(o∣q)=t=1∑∣o∣logπθ(ot∣q,o<t) -
除以 ∣ o ∣ |o| ∣o∣ 做平均:避免长序列的 log 概率天然更小(不公平比较)
所以目标函数就是:让模型生成标准答案的平均概率尽可能大。
第1步:对 θ \theta θ 求梯度
∇ θ J S F T = ∇ θ E q , o ∼ P s f t [ 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ log π θ ( o t ∣ q , o < t ) ] \nabla_{\theta} \mathcal{J}_{SFT} = \nabla_{\theta} \mathbb{E}_{q,o \sim P_{sft}} \left[ \frac{1}{|o|} \sum_{t=1}^{|o|} \log \pi_{\theta}(o_t | q, o_{<t}) \right] ∇θJSFT=∇θEq,o∼Psft ∣o∣1t=1∑∣o∣logπθ(ot∣q,o<t)
1.1 期望和梯度交换顺序
在数学上,期望(积分)和梯度在满足一定条件时可以交换顺序:
∇ θ E [ … ] = E [ ∇ θ … ] \nabla_{\theta} \mathbb{E}[\dots] = \mathbb{E}[\nabla_{\theta} \dots] ∇θE[…]=E[∇θ…]
所以梯度可以"钻"进期望里面:
= E q , o ∼ P s f t [ ∇ θ ( 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ log π θ ( o t ∣ q , o < t ) ) ] = \mathbb{E}_{q,o \sim P_{sft}} \left[ \nabla_{\theta} \left( \frac{1}{|o|} \sum_{t=1}^{|o|} \log \pi_{\theta}(o_t | q, o_{<t}) \right) \right] =Eq,o∼Psft ∇θ ∣o∣1t=1∑∣o∣logπθ(ot∣q,o<t)
1.2 常数提出来、求和与梯度交换
1 ∣ o ∣ \frac{1}{|o|} ∣o∣1 是答案长度,与模型参数 θ \theta θ 无关,是常数,可以提出括号
梯度是线性算子,可以穿过求和符号:
= E q , o ∼ P s f t [ 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ ∇ θ log π θ ( o t ∣ q , o < t ) ] = \mathbb{E}_{q,o \sim P_{sft}} \left[ \frac{1}{|o|} \sum_{t=1}^{|o|} \nabla_{\theta} \log \pi_{\theta}(o_t | q, o_{<t}) \right] =Eq,o∼Psft ∣o∣1t=1∑∣o∣∇θlogπθ(ot∣q,o<t)
这就是论文公式 (7)。
第2步:为什么梯度系数是 1?
对照统一框架:
∇ θ J = E [ 1 ∣ o ∣ ∑ t G C ⏟ 梯度系数 ⋅ ∇ θ log π θ ( o t ∣ … ) ] \nabla_{\theta} \mathcal{J} = \mathbb{E} \left[ \frac{1}{|o|} \sum_{t} \underbrace{GC}_{\text{梯度系数}} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t | \dots) \right] ∇θJ=E[∣o∣1t∑梯度系数 GC⋅∇θlogπθ(ot∣…)]
在 SFT 的梯度公式 (7) 中:
∇ θ J S F T = E [ 1 ∣ o ∣ ∑ t 1 ⏟ G C ⋅ ∇ θ log π θ ( o t ∣ q , o < t ) ] \nabla_{\theta} \mathcal{J}_{SFT} = \mathbb{E} \left[ \frac{1}{|o|} \sum_{t} \underbrace{1}_{GC} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t | q, o_{<t}) \right] ∇θJSFT=E[∣o∣1t∑GC 1⋅∇θlogπθ(ot∣q,o<t)]
每个 token 前面都乘了一个隐式的 1。
为什么?因为:
- 标准答案里的每一个 token 都被认为是"正确的"
- 没有筛选,没有打分,没有对错判断
- 所以每个 token 的"学习权重"完全相等,都是 1
第3步: ∇ θ log π θ \nabla_{\theta} \log \pi_{\theta} ∇θlogπθ 到底是什么?
这是深度学习中最常见的对数梯度(Log-Gradient)。展开看看:
∇ θ log π θ ( o t ∣ q , o < t ) = 1 π θ ( o t ∣ q , o < t ) ⋅ ∇ θ π θ ( o t ∣ q , o < t ) \nabla_{\theta} \log \pi_{\theta}(o_t | q, o_{<t}) = \frac{1}{\pi_{\theta}(o_t | q, o_{<t})} \cdot \nabla_{\theta} \pi_{\theta}(o_t | q, o_{<t}) ∇θlogπθ(ot∣q,o<t)=πθ(ot∣q,o<t)1⋅∇θπθ(ot∣q,o<t)
直观意义
- ∇ θ π θ \nabla_{\theta} \pi_{\theta} ∇θπθ:直接对概率求导,如果概率很小,导数可能也很小,导致梯度消失
- 除以 π θ \pi_{\theta} πθ 后:即使概率很小,只要方向正确,梯度仍然有力
- 这相当于一种自适应放大:对低概率的正确 token 给更大的相对更新力度
在 SFT 中的作用
对于标准答案中的每个 token o t o_t ot:
∇ θ log π θ ( o t ∣ q , o < t ) \nabla_{\theta} \log \pi_{\theta}(o_t | q, o_{<t}) ∇θlogπθ(ot∣q,o<t)
这个向量指向"让模型更可能生成 o t o_t ot"的方向。优化器沿着这个方向更新 θ \theta θ,使得下次见到同样的问题和前文时,模型输出 o t o_t ot 的概率会上升。
第4步:整个 SFT 训练在做什么?
用一句话说:
对于每一条人工写的标准答案,把答案里每个 token 的生成概率都往上推,推的力度完全相等(权重都是1)。
为什么 SFT 效果有限?
因为:
- 没有筛选:标准答案里的所有 token 都平等学习,包括一些"废话"token
- 没有对比:模型不知道"什么是不好的答案",只见过好的
- 没有探索:数据是固定的,不会随着模型变强而变难
这就是为什么后面需要 RFT、DPO、GRPO 等方法——它们都在解决 SFT 的这三个缺陷。
一句话总结推导链
目标函数 = 标准答案的平均 log 概率
求导 → 期望和梯度交换,常数 1 ∣ o ∣ \frac{1}{|o|} ∣o∣1 提出来,求和与梯度交换
结果 → 每个 token 的梯度是 ∇ θ log π θ \nabla_{\theta}\log\pi_{\theta} ∇θlogπθ,前面乘的系数是 1(因为没有筛选、没有奖励、没有惩罚,所有 token 平等学习)
A.1.4. Direct Preference Optimization (DPO)
The objective of DPO is:
J D P O ( θ ) = E [ q ∼ P s f t ( Q ) , o + , o − ∼ π s f t ( O ∣ q ) ] log σ ( β 1 ∣ o + ∣ ∑ t = 1 ∣ o + ∣ log π θ ( o t + ∣ q , o ≤ t + ) π r e f ( o t + ∣ q , o ≤ t + ) − β 1 ∣ o − ∣ ∑ t = 1 ∣ o − ∣ log π θ ( o t − ∣ q , o ≤ t − ) π r e f ( o t − ∣ q , o ≤ t − ) ) ( 12 ) \mathcal{J}_{DPO}(\theta) = \mathbb{E}[q \sim P_{sft}(Q), o^+, o^- \sim \pi_{sft}(O|q)] \log \sigma \left( \beta \frac{1}{|o^+|} \sum_{t=1}^{|o^+|} \log \frac{\pi_{\theta}(o_t^+|q, o_{\leq t}^+)}{\pi_{ref}(o_t^+|q, o_{\leq t}^+)} - \beta \frac{1}{|o^-|} \sum_{t=1}^{|o^-|} \log \frac{\pi_{\theta}(o_t^-|q, o_{\leq t}^-)}{\pi_{ref}(o_t^-|q, o_{\leq t}^-)} \right) \quad (12) JDPO(θ)=E[q∼Psft(Q),o+,o−∼πsft(O∣q)]logσ β∣o+∣1t=1∑∣o+∣logπref(ot+∣q,o≤t+)πθ(ot+∣q,o≤t+)−β∣o−∣1t=1∑∣o−∣logπref(ot−∣q,o≤t−)πθ(ot−∣q,o≤t−) (12)
The gradient of J D P O ( θ ) \mathcal{J}_{DPO}(\theta) JDPO(θ) is:
∇ θ J D P O ( θ ) = E [ q ∼ P s f t ( Q ) , o + , o − ∼ π s f t ( O ∣ q ) ] ( 1 ∣ o + ∣ ∑ t = 1 ∣ o + ∣ G C D P O ( q , o , t ) ∇ θ log π θ ( o t + ∣ q , o ≤ t + ) − 1 ∣ o − ∣ ∑ t = 1 ∣ o − ∣ G C D P O ( q , o , t ) ∇ θ log π θ ( o t − ∣ q , o ≤ t − ) ) ( 13 ) \begin{aligned} \nabla_{\theta} \mathcal{J}_{DPO}(\theta) = \mathbb{E}[q \sim P_{sft}(Q), o^+, o^- \sim \pi_{sft}(O|q)] & \left( \frac{1}{|o^+|} \sum_{t=1}^{|o^+|} GC_{DPO}(q, o, t) \nabla_{\theta} \log \pi_{\theta}(o_t^+|q, o_{\leq t}^+) \right. \\ & \left. - \frac{1}{|o^-|} \sum_{t=1}^{|o^-|} GC_{DPO}(q, o, t) \nabla_{\theta} \log \pi_{\theta}(o_t^-|q, o_{\leq t}^-) \right) \end{aligned} \quad (13) ∇θJDPO(θ)=E[q∼Psft(Q),o+,o−∼πsft(O∣q)] ∣o+∣1t=1∑∣o+∣GCDPO(q,o,t)∇θlogπθ(ot+∣q,o≤t+)−∣o−∣1t=1∑∣o−∣GCDPO(q,o,t)∇θlogπθ(ot−∣q,o≤t−) (13)
Data Source: question in SFT dataset with outputs sampled from SFT model. Reward Function: human preference in the general domain (can be ‘Rule’ in mathematical tasks). Gradient Coefficient:
G C D P O ( q , o , t ) = σ ( β log π θ ( o t − ∣ q , o ≤ t − ) π r e f ( o t − ∣ q , o ≤ t − ) − β log π θ ( o t + ∣ q , o ≤ t + ) π r e f ( o t + ∣ q , o ≤ t + ) ) ( 14 ) GC_{DPO}(q, o, t) = \sigma \left( \beta \log \frac{\pi_{\theta}(o_t^-|q, o_{\leq t}^-)}{\pi_{ref}(o_t^-|q, o_{\leq t}^-)} - \beta \log \frac{\pi_{\theta}(o_t^+|q, o_{\leq t}^+)}{\pi_{ref}(o_t^+|q, o_{\leq t}^+)} \right) \quad (14) GCDPO(q,o,t)=σ(βlogπref(ot−∣q,o≤t−)πθ(ot−∣q,o≤t−)−βlogπref(ot+∣q,o≤t+)πθ(ot+∣q,o≤t+))(14)
问题:DPO目标函数求导
第0步:先写出DPO的目标函数
DPO 的核心是比较"好答案"和"差答案"。
令:
- score + = 1 ∣ o + ∣ ∑ t log π θ ( o t + ) π r e f ( o t + ) \text{score}^+ = \frac{1}{|o^+|} \sum_{t} \log \frac{\pi_{\theta}(o_t^+)}{\pi_{ref}(o_t^+)} score+=∣o+∣1∑tlogπref(ot+)πθ(ot+) ← 好答案的"相对得分"
- score − = 1 ∣ o − ∣ ∑ t log π θ ( o t − ) π r e f ( o t − ) \text{score}^- = \frac{1}{|o^-|} \sum_{t} \log \frac{\pi_{\theta}(o_t^-)}{\pi_{ref}(o_t^-)} score−=∣o−∣1∑tlogπref(ot−)πθ(ot−) ← 差答案的"相对得分"
目标函数:
J D P O = log σ ( β ⋅ score + − β ⋅ score − ) \mathcal{J}_{DPO} = \log \sigma\left( \beta \cdot \text{score}^+ - \beta \cdot \text{score}^- \right) JDPO=logσ(β⋅score+−β⋅score−)
为了简化,我把括号里的东西记作一个中间变量:
u = β ⋅ score + − β ⋅ score − u = \beta \cdot \text{score}^+ - \beta \cdot \text{score}^- u=β⋅score+−β⋅score−
所以目标函数就是:
J = log σ ( u ) \mathcal{J} = \log \sigma(u) J=logσ(u)
第1步:对 θ \theta θ 求导(链式法则)
∇ θ J = d d u log σ ( u ) ⏟ 外层:对 u 求导 ⋅ ∇ θ u ⏟ 内层:对 θ 求导 \nabla_{\theta} \mathcal{J} = \underbrace{\frac{d}{du} \log \sigma(u)}_{\text{外层:对 } u \text{ 求导}} \cdot \underbrace{\nabla_{\theta} u}_{\text{内层:对 } \theta \text{ 求导}} ∇θJ=外层:对 u 求导 dudlogσ(u)⋅内层:对 θ 求导 ∇θu
外层导数: d d u log σ ( u ) \frac{d}{du} \log \sigma(u) dudlogσ(u)
这就是之前说的 sigmoid 性质:
d d u log σ ( u ) = 1 σ ( u ) ⋅ σ ′ ( u ) = 1 σ ( u ) ⋅ σ ( u ) ( 1 − σ ( u ) ) = 1 − σ ( u ) = σ ( − u ) \frac{d}{du} \log \sigma(u) = \frac{1}{\sigma(u)} \cdot \sigma'(u) = \frac{1}{\sigma(u)} \cdot \sigma(u)(1-\sigma(u)) = 1 - \sigma(u) = \sigma(-u) dudlogσ(u)=σ(u)1⋅σ′(u)=σ(u)1⋅σ(u)(1−σ(u))=1−σ(u)=σ(−u)
所以外层导数 = σ ( − u ) \sigma(-u) σ(−u)
log σ ( x ) \log \sigma(x) logσ(x) 的导数为什么是 1 − σ ( x ) 1 - \sigma(x) 1−σ(x)?
先记住 Sigmoid 的定义
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+e−x1
计算 log σ ( x ) \log \sigma(x) logσ(x) 的导数
log σ ( x ) = log ( 1 1 + e − x ) = − log ( 1 + e − x ) \log \sigma(x) = \log\left(\frac{1}{1+e^{-x}}\right) = -\log(1+e^{-x}) logσ(x)=log(1+e−x1)=−log(1+e−x)
对 x x x 求导(链式法则):
d d x [ − log ( 1 + e − x ) ] = − 1 1 + e − x ⋅ ( − e − x ) ⏟ 内层导数 = e − x 1 + e − x \frac{d}{dx}[-\log(1+e^{-x})] = -\frac{1}{1+e^{-x}} \cdot \underbrace{(-e^{-x})}_{\text{内层导数}} = \frac{e^{-x}}{1+e^{-x}} dxd[−log(1+e−x)]=−1+e−x1⋅内层导数 (−e−x)=1+e−xe−x
现在看 1 − σ ( x ) 1 - \sigma(x) 1−σ(x) 等于什么
1 − σ ( x ) = 1 − 1 1 + e − x = ( 1 + e − x ) − 1 1 + e − x = e − x 1 + e − x 1 - \sigma(x) = 1 - \frac{1}{1+e^{-x}} = \frac{(1+e^{-x}) - 1}{1+e^{-x}} = \frac{e^{-x}}{1+e^{-x}} 1−σ(x)=1−1+e−x1=1+e−x(1+e−x)−1=1+e−xe−x
对比两个结果
d d x log σ ( x ) = e − x 1 + e − x = 1 − σ ( x ) \frac{d}{dx}\log \sigma(x) = \frac{e^{-x}}{1+e^{-x}} = 1 - \sigma(x) dxdlogσ(x)=1+e−xe−x=1−σ(x)
而 σ ( − x ) = 1 1 + e x \sigma(-x) = \frac{1}{1+e^{x}} σ(−x)=1+ex1,分子分母同乘 e − x e^{-x} e−x 后也是 e − x 1 + e − x \frac{e^{-x}}{1+e^{-x}} 1+e−xe−x,所以:
d d x log σ ( x ) = 1 − σ ( x ) = σ ( − x ) \frac{d}{dx}\log \sigma(x) = 1 - \sigma(x) = \sigma(-x) dxdlogσ(x)=1−σ(x)=σ(−x)
一句话:Sigmoid 有个漂亮性质—— σ ( x ) + σ ( − x ) = 1 \sigma(x) + \sigma(-x) = 1 σ(x)+σ(−x)=1,所以 1 − σ ( x ) = σ ( − x ) 1-\sigma(x) = \sigma(-x) 1−σ(x)=σ(−x)。
第2步:内层导数 ∇ θ u \nabla_{\theta} u ∇θu
u = β ⋅ score + − β ⋅ score − u = \beta \cdot \text{score}^+ - \beta \cdot \text{score}^- u=β⋅score+−β⋅score−
∇ θ u = β ⋅ ∇ θ score + − β ⋅ ∇ θ score − \nabla_{\theta} u = \beta \cdot \nabla_{\theta} \text{score}^+ - \beta \cdot \nabla_{\theta} \text{score}^- ∇θu=β⋅∇θscore+−β⋅∇θscore−
现在分别算 ∇ θ score + \nabla_{\theta} \text{score}^+ ∇θscore+ 和 ∇ θ score − \nabla_{\theta} \text{score}^- ∇θscore−。
第3步:好答案的得分怎么求导?
score + = 1 ∣ o + ∣ ∑ t = 1 ∣ o + ∣ log π θ ( o t + ) π r e f ( o t + ) = 1 ∣ o + ∣ ∑ t [ log π θ ( o t + ) − log π r e f ( o t + ) ] \text{score}^+ = \frac{1}{|o^+|} \sum_{t=1}^{|o^+|} \log \frac{\pi_{\theta}(o_t^+)}{\pi_{ref}(o_t^+)} = \frac{1}{|o^+|} \sum_{t} \left[ \log \pi_{\theta}(o_t^+) - \log \pi_{ref}(o_t^+) \right] score+=∣o+∣1t=1∑∣o+∣logπref(ot+)πθ(ot+)=∣o+∣1t∑[logπθ(ot+)−logπref(ot+)]
对 θ \theta θ 求导:
∇ θ score + = 1 ∣ o + ∣ ∑ t [ ∇ θ log π θ ( o t + ) − ∇ θ log π r e f ( o t + ) ⏟ = 0 ] \nabla_{\theta} \text{score}^+ = \frac{1}{|o^+|} \sum_{t} \left[ \nabla_{\theta} \log \pi_{\theta}(o_t^+) - \underbrace{\nabla_{\theta} \log \pi_{ref}(o_t^+)}_{=0} \right] ∇θscore+=∣o+∣1t∑
∇θlogπθ(ot+)−=0
∇θlogπref(ot+)
为什么 ∇ θ log π r e f = 0 \nabla_{\theta} \log \pi_{ref} = 0 ∇θlogπref=0?
因为 π r e f \pi_{ref} πref 是参考模型(SFT 模型),它的参数固定死了,在训练过程中永远不会变。所以它对当前参数 θ \theta θ 的导数就是 0。
因此:
∇ θ score + = 1 ∣ o + ∣ ∑ t ∇ θ log π θ ( o t + ) \nabla_{\theta} \text{score}^+ = \frac{1}{|o^+|} \sum_{t} \nabla_{\theta} \log \pi_{\theta}(o_t^+) ∇θscore+=∣o+∣1t∑∇θlogπθ(ot+)
同理:
∇ θ score − = 1 ∣ o − ∣ ∑ t ∇ θ log π θ ( o t − ) \nabla_{\theta} \text{score}^- = \frac{1}{|o^-|} \sum_{t} \nabla_{\theta} \log \pi_{\theta}(o_t^-) ∇θscore−=∣o−∣1t∑∇θlogπθ(ot−)
第4步:把内外层合起来
∇ θ J = σ ( − u ) ⋅ [ β ⋅ 1 ∣ o + ∣ ∑ t ∇ θ log π θ ( o t + ) − β ⋅ 1 ∣ o − ∣ ∑ t ∇ θ log π θ ( o t − ) ] \nabla_{\theta} \mathcal{J} = \sigma(-u) \cdot \left[ \beta \cdot \frac{1}{|o^+|} \sum_{t} \nabla_{\theta} \log \pi_{\theta}(o_t^+) - \beta \cdot \frac{1}{|o^-|} \sum_{t} \nabla_{\theta} \log \pi_{\theta}(o_t^-) \right] ∇θJ=σ(−u)⋅[β⋅∣o+∣1t∑∇θlogπθ(ot+)−β⋅∣o−∣1t∑∇θlogπθ(ot−)]
把 σ ( − u ) \sigma(-u) σ(−u) 乘进去,拆成两项:
= 1 ∣ o + ∣ ∑ t β ⋅ σ ( − u ) ⏟ G C ⋅ ∇ θ log π θ ( o t + ) − 1 ∣ o − ∣ ∑ t β ⋅ σ ( − u ) ⏟ G C ⋅ ∇ θ log π θ ( o t − ) = \frac{1}{|o^+|} \sum_{t} \underbrace{\beta \cdot \sigma(-u)}_{GC} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t^+) - \frac{1}{|o^-|} \sum_{t} \underbrace{\beta \cdot \sigma(-u)}_{GC} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t^-) =∣o+∣1t∑GC β⋅σ(−u)⋅∇θlogπθ(ot+)−∣o−∣1t∑GC β⋅σ(−u)⋅∇θlogπθ(ot−)
第5步:梯度系数 G C GC GC 终于出现了!
G C = β ⋅ σ ( − u ) GC = \beta \cdot \sigma(-u) GC=β⋅σ(−u)
而 − u = β ⋅ score − − β ⋅ score + -u = \beta \cdot \text{score}^- - \beta \cdot \text{score}^+ −u=β⋅score−−β⋅score+,所以:
G C = β ⋅ σ ( β ⋅ score − − β ⋅ score + ) GC = \beta \cdot \sigma\left( \beta \cdot \text{score}^- - \beta \cdot \text{score}^+ \right) GC=β⋅σ(β⋅score−−β⋅score+)
展开就是:
G C = β ⋅ σ ( β ⋅ 1 ∣ o − ∣ ∑ t log π θ ( o t − ) π r e f ( o t − ) − β ⋅ 1 ∣ o + ∣ ∑ t log π θ ( o t + ) π r e f ( o t + ) ) GC = \beta \cdot \sigma\left( \beta \cdot \frac{1}{|o^-|} \sum_{t} \log \frac{\pi_{\theta}(o_t^-)}{\pi_{ref}(o_t^-)} - \beta \cdot \frac{1}{|o^+|} \sum_{t} \log \frac{\pi_{\theta}(o_t^+)}{\pi_{ref}(o_t^+)} \right) GC=β⋅σ(β⋅∣o−∣1t∑logπref(ot−)πθ(ot−)−β⋅∣o+∣1t∑logπref(ot+)πθ(ot+))
论文公式 (14) 基本就是这个形式(只是它把求和平均写成了单个 token 的形式,核心是一样的)。
第6步:这个 G C GC GC 到底在干什么?
G C = β ⋅ σ ( β ⋅ 差答案得分 − β ⋅ 好答案得分 ) GC = \beta \cdot \sigma(\beta \cdot \text{差答案得分} - \beta \cdot \text{好答案得分}) GC=β⋅σ(β⋅差答案得分−β⋅好答案得分)
三种情况:
| 情况 | 差答案得分 − 好答案得分 | G C GC GC 的值 | 意味着什么 |
|---|---|---|---|
| 模型还没学会 | 差答案得分 ≈ 好答案得分(比如 0) | σ ( 0 ) = 0.5 \sigma(0) = 0.5 σ(0)=0.5, G C ≈ 0.5 β GC \approx 0.5\beta GC≈0.5β | 中等力度调整 |
| 模型搞反了 | 差答案得分 > 好答案得分(正数) | σ ( 正数 ) → 1 \sigma(\text{正数}) \to 1 σ(正数)→1, G C → β GC \to \beta GC→β | 大力纠正! |
| 模型学得很好 | 差答案得分 << 好答案得分(负数) | σ ( 负数 ) → 0 \sigma(\text{负数}) \to 0 σ(负数)→0, G C → 0 GC \to 0 GC→0 | 几乎不动了 |
一句话总结推导链
目标函数 log σ ( u ) \log \sigma(u) logσ(u) → 链式法则 → 外层 σ ( − u ) \sigma(-u) σ(−u) × 内层 β [ ∇ 好 − ∇ 差 ] \beta[\nabla\text{好} - \nabla\text{差}] β[∇好−∇差] → 内层里 π r e f \pi_{ref} πref 导数为0 → 只剩下 ∇ θ log π θ \nabla_{\theta}\log\pi_{\theta} ∇θlogπθ → 把 σ ( − u ) \sigma(-u) σ(−u) 提出来当公共系数 → 这就是 G C GC GC。
它本质上是一个**“自适应刹车片”**:模型越学得好, G C GC GC 越接近 0,更新越轻;模型越搞错, G C GC GC 越接近 1,更新越重。
A.1.5. Proximal Policy Optimization (PPO)
The objective of PPO is:
J P P O ( θ ) = E [ q ∼ P s f t ( Q ) , o ∼ π θ o l d ( O ∣ q ) ] 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ min [ π θ ( o t ∣ q , o ≤ t ) π θ o l d ( o t ∣ q , o ≤ t ) A t , clip ( π θ ( o t ∣ q , o ≤ t ) π θ o l d ( o t ∣ q , o ≤ t ) , 1 − ϵ , 1 + ϵ ) A t ] . ( 15 ) \mathcal{J}_{PPO}(\theta) = \mathbb{E}[q \sim P_{sft}(Q), o \sim \pi_{\theta_{old}}(O|q)] \frac{1}{|o|} \sum_{t=1}^{|o|} \min \left[ \frac{\pi_{\theta}(o_t|q, o_{\leq t})}{\pi_{\theta_{old}}(o_t|q, o_{\leq t})} A_t, \text{clip} \left( \frac{\pi_{\theta}(o_t|q, o_{\leq t})}{\pi_{\theta_{old}}(o_t|q, o_{\leq t})}, 1 - \epsilon, 1 + \epsilon \right) A_t \right]. \quad (15) JPPO(θ)=E[q∼Psft(Q),o∼πθold(O∣q)]∣o∣1t=1∑∣o∣min[πθold(ot∣q,o≤t)πθ(ot∣q,o≤t)At,clip(πθold(ot∣q,o≤t)πθ(ot∣q,o≤t),1−ϵ,1+ϵ)At].(15)
To simplify the analysis, it is assumed that the model only has a single update following each exploration stage, thereby ensuring that π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ . In this case, we can remove the min and clip operation:
J P P O ( θ ) = E [ q ∼ P s f t ( Q ) , o ∼ π θ o l d ( O ∣ q ) ] 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ π θ ( o t ∣ q , o ≤ t ) π θ o l d ( o t ∣ q , o ≤ t ) A t . ( 16 ) \mathcal{J}_{PPO}(\theta) = \mathbb{E}[q \sim P_{sft}(Q), o \sim \pi_{\theta_{old}}(O|q)] \frac{1}{|o|} \sum_{t=1}^{|o|} \frac{\pi_{\theta}(o_t|q, o_{\leq t})}{\pi_{\theta_{old}}(o_t|q, o_{\leq t})} A_t. \quad (16) JPPO(θ)=E[q∼Psft(Q),o∼πθold(O∣q)]∣o∣1t=1∑∣o∣πθold(ot∣q,o≤t)πθ(ot∣q,o≤t)At.(16)
The gradient of J P P O ( θ ) \mathcal{J}_{PPO}(\theta) JPPO(θ) is:
∇ θ J P P O ( θ ) = E [ q ∼ P s f t ( Q ) , o ∼ π θ o l d ( O ∣ q ) ] 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ A t ∇ θ log π θ ( o t ∣ q , o ≤ t ) ( 17 ) \nabla_{\theta} \mathcal{J}_{PPO}(\theta) = \mathbb{E}[q \sim P_{sft}(Q), o \sim \pi_{\theta_{old}}(O|q)] \frac{1}{|o|} \sum_{t=1}^{|o|} A_t \nabla_{\theta} \log \pi_{\theta}(o_t|q, o_{\leq t}) \quad (17) ∇θJPPO(θ)=E[q∼Psft(Q),o∼πθold(O∣q)]∣o∣1t=1∑∣o∣At∇θlogπθ(ot∣q,o≤t)(17)
Data Source: question in SFT dataset with outputs sampled from policy model. Reward Function: reward model. Gradient Coefficient:
G C P P O ( q , o , t , π θ m ) = A t , ( 18 ) GC_{PPO}(q, o, t, \pi_{\theta_m}) = A_t, \quad (18) GCPPO(q,o,t,πθm)=At,(18)
where A t A_t At is the advantage, which is computed by applying Generalized Advantage Estimation (GAE) (Schulman et al., 2015), based on the rewards { r ≥ t } \{r_{\geq t}\} {r≥t} and a learned value function V ψ V_{\psi} Vψ .
问题1:PPO目标函数求导
好的,这个推导有几个关键细节需要注意,尤其是"为什么 π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ 还能求导"。
第0步:PPO 原始目标函数 (15)
J P P O = E 1 ∣ o ∣ ∑ t min [ r t ( θ ) A t ⏟ 无约束项 , clip ( r t ( θ ) , 1 − ϵ , 1 + ϵ ) A t ⏟ 有约束项 ] \mathcal{J}_{PPO} = \mathbb{E} \frac{1}{|o|} \sum_{t} \min \left[ \underbrace{r_t(\theta) A_t}_{\text{无约束项}}, \underbrace{\text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon) A_t}_{\text{有约束项}} \right] JPPO=E∣o∣1t∑min 无约束项 rt(θ)At,有约束项 clip(rt(θ),1−ϵ,1+ϵ)At
其中 r t ( θ ) = π θ ( o t ∣ q , o ≤ t ) π θ o l d ( o t ∣ q , o ≤ t ) r_t(\theta) = \frac{\pi_{\theta}(o_t|q,o_{\leq t})}{\pi_{\theta_{old}}(o_t|q,o_{\leq t})} rt(θ)=πθold(ot∣q,o≤t)πθ(ot∣q,o≤t) 叫重要性采样比率。
为什么要有 min 和 clip?
PPO 的核心思想是:别让策略更新步子太大。
- 如果 A t > 0 A_t > 0 At>0(这个动作好):想提升 r t ( θ ) r_t(\theta) rt(θ),但 clip 把它限制在 1 + ϵ 1+\epsilon 1+ϵ 以内
- 如果 A t < 0 A_t < 0 At<0(这个动作差):想降低 r t ( θ ) r_t(\theta) rt(θ),但 clip 把它限制在 1 − ϵ 1-\epsilon 1−ϵ 以内
min 的作用:取"无约束项"和"有约束项"中更保守的那个,防止策略突变。
第1步:简化假设 π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ
论文说:假设每次采样后只更新一次模型参数。
这意味着:
- 采样时用的策略是 π θ o l d \pi_{\theta_{old}} πθold
- 更新后的策略是 π θ \pi_{\theta} πθ
- 因为只更新了一步, θ \theta θ 和 θ o l d \theta_{old} θold 非常接近
在这个假设下:
r t ( θ ) = π θ π θ o l d ≈ 1 r_t(\theta) = \frac{\pi_{\theta}}{\pi_{\theta_{old}}} \approx 1 rt(θ)=πθoldπθ≈1
既然 r t ( θ ) ≈ 1 r_t(\theta) \approx 1 rt(θ)≈1,那么:
- clip ( 1 , 1 − ϵ , 1 + ϵ ) = 1 \text{clip}(1, 1-\epsilon, 1+\epsilon) = 1 clip(1,1−ϵ,1+ϵ)=1(clip 不起作用)
- 无约束项 = 1 ⋅ A t = A t = 1 \cdot A_t = A_t =1⋅At=At
- 有约束项 = 1 ⋅ A t = A t = 1 \cdot A_t = A_t =1⋅At=At
- min [ A t , A t ] = A t \min[A_t, A_t] = A_t min[At,At]=At
所以 min 和 clip 都可以安全去掉,得到简化版 (16):
J P P O = E 1 ∣ o ∣ ∑ t = 1 ∣ o ∣ π θ ( o t ∣ q , o ≤ t ) π θ o l d ( o t ∣ q , o ≤ t ) A t \mathcal{J}_{PPO} = \mathbb{E} \frac{1}{|o|} \sum_{t=1}^{|o|} \frac{\pi_{\theta}(o_t|q,o_{\leq t})}{\pi_{\theta_{old}}(o_t|q,o_{\leq t})} A_t JPPO=E∣o∣1t=1∑∣o∣πθold(ot∣q,o≤t)πθ(ot∣q,o≤t)At
第2步:对 θ \theta θ 求导
现在对简化后的目标函数求导:
∇ θ J P P O = E 1 ∣ o ∣ ∑ t ∇ θ ( π θ ( o t ) π θ o l d ( o t ) A t ) \nabla_{\theta} \mathcal{J}_{PPO} = \mathbb{E} \frac{1}{|o|} \sum_{t} \nabla_{\theta} \left( \frac{\pi_{\theta}(o_t)}{\pi_{\theta_{old}}(o_t)} A_t \right) ∇θJPPO=E∣o∣1t∑∇θ(πθold(ot)πθ(ot)At)
2.1 哪些是与 θ \theta θ 无关的常数?
| 符号 | 是否依赖 θ \theta θ | 原因 |
|---|---|---|
| 1 ∣ o ∣ \frac{1}{\lvert o \rvert} ∣o∣1 | ❌ 否 | 序列长度 |
| π θ o l d ( o t ) \pi_{\theta_{old}}(o_t) πθold(ot) | ❌ 否 | 采样时的旧策略,参数已固定 |
| A t A_t At | ❌ 否 | 优势函数,基于旧策略的采样轨迹和奖励计算 |
| π θ ( o t ) \pi_{\theta}(o_t) πθ(ot) | ✅ 是 | 当前策略,参数 θ \theta θ 正在优化 |
所以 1 π θ o l d \frac{1}{\pi_{\theta_{old}}} πθold1 和 A t A_t At 都是常数,可以提出来:
= E 1 ∣ o ∣ ∑ t A t π θ o l d ( o t ) ⋅ ∇ θ π θ ( o t ) = \mathbb{E} \frac{1}{|o|} \sum_{t} \frac{A_t}{\pi_{\theta_{old}}(o_t)} \cdot \nabla_{\theta} \pi_{\theta}(o_t) =E∣o∣1t∑πθold(ot)At⋅∇θπθ(ot)
2.2 对数梯度技巧
∇ θ π θ ( o t ) = π θ ( o t ) ⋅ ∇ θ log π θ ( o t ) \nabla_{\theta} \pi_{\theta}(o_t) = \pi_{\theta}(o_t) \cdot \nabla_{\theta} \log \pi_{\theta}(o_t) ∇θπθ(ot)=πθ(ot)⋅∇θlogπθ(ot)
代入:
= E 1 ∣ o ∣ ∑ t A t π θ o l d ( o t ) ⋅ π θ ( o t ) ⋅ ∇ θ log π θ ( o t ) = \mathbb{E} \frac{1}{|o|} \sum_{t} \frac{A_t}{\pi_{\theta_{old}}(o_t)} \cdot \pi_{\theta}(o_t) \cdot \nabla_{\theta} \log \pi_{\theta}(o_t) =E∣o∣1t∑πθold(ot)At⋅πθ(ot)⋅∇θlogπθ(ot)
= E 1 ∣ o ∣ ∑ t A t ⋅ π θ ( o t ) π θ o l d ( o t ) ⏟ r t ( θ ) ⋅ ∇ θ log π θ ( o t ) = \mathbb{E} \frac{1}{|o|} \sum_{t} A_t \cdot \underbrace{\frac{\pi_{\theta}(o_t)}{\pi_{\theta_{old}}(o_t)}}_{r_t(\theta)} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t) =E∣o∣1t∑At⋅rt(θ) πθold(ot)πθ(ot)⋅∇θlogπθ(ot)
2.3 再次代入简化假设
论文假设 π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ(在当前参数点处求值),所以:
π θ ( o t ) π θ o l d ( o t ) = 1 \frac{\pi_{\theta}(o_t)}{\pi_{\theta_{old}}(o_t)} = 1 πθold(ot)πθ(ot)=1
因此:
∇ θ J P P O = E 1 ∣ o ∣ ∑ t A t ⋅ ∇ θ log π θ ( o t ) \nabla_{\theta} \mathcal{J}_{PPO} = \mathbb{E} \frac{1}{|o|} \sum_{t} A_t \cdot \nabla_{\theta} \log \pi_{\theta}(o_t) ∇θJPPO=E∣o∣1t∑At⋅∇θlogπθ(ot)
这就是公式 (17)。
第3步:为什么梯度系数是 A t A_t At?
对照统一框架:
∇ θ J = E [ 1 ∣ o ∣ ∑ t G C ⏟ 梯度系数 ⋅ ∇ θ log π θ ( o t ) ] \nabla_{\theta} \mathcal{J} = \mathbb{E} \left[ \frac{1}{|o|} \sum_{t} \underbrace{GC}_{\text{梯度系数}} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t) \right] ∇θJ=E[∣o∣1t∑梯度系数 GC⋅∇θlogπθ(ot)]
PPO 的梯度:
∇ θ J P P O = E [ 1 ∣ o ∣ ∑ t A t ⏟ G C ⋅ ∇ θ log π θ ( o t ) ] \nabla_{\theta} \mathcal{J}_{PPO} = \mathbb{E} \left[ \frac{1}{|o|} \sum_{t} \underbrace{A_t}_{GC} \cdot \nabla_{\theta} \log \pi_{\theta}(o_t) \right] ∇θJPPO=E ∣o∣1t∑GC At⋅∇θlogπθ(ot)
A t A_t At 是什么?
A t A_t At 是优势函数(Advantage),表示:“在时刻 t t t 选择这个动作 o t o_t ot,比这个状态下的平均动作好多少”。
计算方式(GAE):
A t = ∑ k = 0 ∞ ( γ λ ) k δ t + k ⏟ GAE 公式 A_t = \underbrace{\sum_{k=0}^{\infty} (\gamma \lambda)^k \delta_{t+k}}_{\text{GAE 公式}} At=GAE 公式
k=0∑∞(γλ)kδt+k
其中 δ t = r t + γ V ( s t + 1 ) − V ( s t ) \delta_t = r_t + \gamma V(s_{t+1}) - V(s_t) δt=rt+γV(st+1)−V(st) 是时序差分误差。
直觉:
- A t > 0 A_t > 0 At>0:这个动作比预期好 → 提升它的概率
- A t < 0 A_t < 0 At<0:这个动作比预期差 → 降低它的概率
- ∣ A t ∣ |A_t| ∣At∣ 越大:调整力度越大
关键问题:为什么 π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ 还能求导?
你可能会问:如果两者相等,那 r t ( θ ) = 1 r_t(\theta) = 1 rt(θ)=1 是常数,对 θ \theta θ 求导不是应该为 0 吗?
答案是: π θ o l d = π θ \pi_{\theta_{old}} = \pi_{\theta} πθold=πθ 只在当前参数点成立( θ = θ o l d \theta = \theta_{old} θ=θold),但 π θ \pi_{\theta} πθ 仍然是 θ \theta θ 的函数。我们求的是在 θ = θ o l d \theta = \theta_{old} θ=θold 处的导数,而不是说 π θ \pi_{\theta} πθ 恒等于 π θ o l d \pi_{\theta_{old}} πθold。
类比:
- 函数 f ( x ) = x 2 f(x) = x^2 f(x)=x2 在 x = 1 x=1 x=1 处, f ( 1 ) = 1 f(1) = 1 f(1)=1
- 但 f ( x ) f(x) f(x) 仍然是 x x x 的函数,在 x = 1 x=1 x=1 处的导数是 f ′ ( 1 ) = 2 f'(1) = 2 f′(1)=2
同理:
- π θ π θ o l d \frac{\pi_{\theta}}{\pi_{\theta_{old}}} πθoldπθ 在 θ = θ o l d \theta = \theta_{old} θ=θold 处等于 1
- 但它对 θ \theta θ 的导数是 ∇ θ π θ π θ o l d \frac{\nabla_{\theta}\pi_{\theta}}{\pi_{\theta_{old}}} πθold∇θπθ,在 θ = θ o l d \theta = \theta_{old} θ=θold 处等于 ∇ θ log π θ \nabla_{\theta}\log\pi_{\theta} ∇θlogπθ
一句话总结
简化假设:只更新一次 → π θ ≈ π θ o l d \pi_{\theta} \approx \pi_{\theta_{old}} πθ≈πθold → min 和 clip 都不起作用 → 去掉它们。
求导: π θ o l d \pi_{\theta_{old}} πθold 和 A t A_t At 都是常数,提出来; ∇ θ π θ \nabla_{\theta}\pi_{\theta} ∇θπθ 用对数梯度技巧变成 π θ ∇ θ log π θ \pi_{\theta}\nabla_{\theta}\log\pi_{\theta} πθ∇θlogπθ;再代入 π θ = π θ o l d \pi_{\theta} = \pi_{\theta_{old}} πθ=πθold 消掉比值;最终得到 G C = A t GC = A_t GC=At。
A t A_t At 的直觉:奖励模型打分 + 价值网络预估 = 每个动作比"平均水平"好多少,好的多推、差的多压。
更多推荐



所有评论(0)