Score Centering under Policy Mismatch

Policy-gradient methods multiply the gradient of an action’s log-probability by its reward. When the policy that generated the data differs from the policy being optimized, this update can have a nonzero expectation even if every action receives the same reward. This log-probability gradient is called the score. Score Centering (SC) subtracts its mean under the sampling policy to address this effect.

The motivating requirement is simple: if reward does not distinguish between actions, the gradient of expected reward is zero. Exact SC restores this cancellation under the sampling policy; importance sampling reweights the update to recover an expectation under the target policy. These are different properties. We use a binary-action example to explain their relationship, then discuss language-model updates. The discussion builds on Marek and Ryabinin’s Score Centering paper; the interactive examples evaluate analytic expressions.

Policy gradient 方法将动作的对数概率梯度乘以奖励,得到一次采样对应的更新。当生成数据的策略与正在优化的策略不同时,即使所有动作获得相同奖励,这个更新的期望也可能非零。这个对数概率梯度称为 score。Score Centering(SC)减去它在采样策略下的均值,以修正这一现象。

其出发点是:如果奖励不区分不同动作,期望奖励的梯度应当为零。精确 SC 使这种抵消关系在采样策略下成立;importance sampling 则通过重新加权,将更新的期望转换到目标策略下。 两者对应不同的性质。下面用一个二元动作的例子说明它们的关系,再讨论语言模型中的更新。本文以 Marek 与 Ryabinin 的 Score Centering 论文为基础,交互示例中的数值均由解析公式计算。

1. Policy Mismatch and the Mean Score

1.1 The Target Gradient and the Sampling Policy

Start with one decision and finitely many possible actions. Let $\theta$ be the model parameters, $a$ a possible action, and $R(a)$ its reward, which does not depend directly on $\theta$. The current policy assigns probability $p_\theta(a)$ to action $a$. The sampling policy assigns probability $q(a)$ and generates the observed action $y\sim q$. An older checkpoint or different numerical precision can make these policies differ. We hold $q$ fixed during differentiation.

Write $p$ as shorthand for the distribution $p_\theta$. An expectation is a probability-weighted average: outcomes that occur more often contribute more. For any function $f$ of the action, $\mathbb E_p[f]=\sum_a p_\theta(a)f(a)$; $\mathbb E_q[f]$ uses $q(a)$ instead. Each sum runs over all possible actions. Our objective $J(\theta)$ is the current policy’s expected reward:

\[J(\theta)=\mathbb E_{a\sim p_\theta}[R(a)] =\sum_a p_\theta(a)R(a).\]

The score $s_p(a)$ is the gradient of an action’s log-probability with respect to the parameters. Let $g_p$ denote the gradient of the reward objective:

\[\begin{aligned} s_p(a)&=\nabla_\theta\log p_\theta(a),\\ g_p&=\nabla_\theta J(\theta)=\mathbb E_p[R\,s_p]. \end{aligned}\]

Inside an expectation, $R$ and $s_p$ are evaluated at the same random action. We assume these derivatives and expectations exist. The desired gradient $g_p$ weights each action’s score by both its reward and its probability under the current policy. This follows by differentiating the sum defining $J$: rewards stay fixed, and $\nabla_\theta p_\theta(a)=p_\theta(a)\nabla_\theta\log p_\theta(a)$.

A sample from $q$ instead gives the uncorrected direction $\widehat g_{\rm raw}=R(y)s_p(y)$, whose expectation is $g_{\rm raw}=\mathbb E_q[\widehat g_{\rm raw}]$. The hat marks a direction computed from one random sample; $g_{\rm raw}$ denotes its mean. Throughout the article, an “update” means an ascent direction before multiplication by the learning rate. Because $g_{\rm raw}$ averages under $q$, it generally differs from the target gradient $g_p$.

先考虑一次决策,动作有有限种可能。令 $\theta$ 表示模型参数,$a$ 表示一个可能的动作,$R(a)$ 表示它的奖励,且奖励不直接依赖 $\theta$。当前策略为动作 $a$ 赋予概率 $p_\theta(a)$;采样策略为它赋予概率 $q(a)$,并据此生成实际观测到的动作 $y\sim q$。生成过程使用较旧的 checkpoint 或不同数值精度,都可能使两种策略不同。求导时将 $q$ 视为固定。

下文用 $p$ 简写分布 $p_\theta$。期望就是按概率加权的平均值:某个结果出现得越频繁,它在平均值中的权重就越大。对于动作的任意函数 $f$,$\mathbb E_p[f]=\sum_a p_\theta(a)f(a)$;$\mathbb E_q[f]$ 则使用 $q(a)$ 加权。求和遍历所有可能动作。我们要最大化的目标 $J(\theta)$,就是当前策略下的期望奖励:

\[J(\theta)=\mathbb E_{a\sim p_\theta}[R(a)] =\sum_a p_\theta(a)R(a).\]

Score $s_p(a)$ 是动作的对数概率相对于参数的梯度。用 $g_p$ 表示期望奖励目标的梯度:

\[\begin{aligned} s_p(a)&=\nabla_\theta\log p_\theta(a),\\ g_p&=\nabla_\theta J(\theta)=\mathbb E_p[R\,s_p]. \end{aligned}\]

期望中的 $R$ 和 $s_p$ 都在同一个随机动作上取值,并假设这些导数和期望存在。目标梯度 $g_p$ 同时按照动作的奖励和它在当前策略下的概率,对 score 加权。这个关系来自对 $J$ 的求和式直接求导:奖励保持固定,而 $\nabla_\theta p_\theta(a)=p_\theta(a)\nabla_\theta\log p_\theta(a)$。

从 $q$ 采到的一个动作则给出未经修正的方向 $\widehat g_{\rm raw}=R(y)s_p(y)$,其期望为 $g_{\rm raw}=\mathbb E_q[\widehat g_{\rm raw}]$。帽子表示由单个随机样本计算的方向,$g_{\rm raw}$ 表示它的均值。下文的“更新”均指尚未乘以学习率的梯度上升方向。由于 $g_{\rm raw}$ 在 $q$ 下求平均,它通常不等于目标梯度 $g_p$。

1.2 A Binary-Action Example with Constant Reward

Consider an action $y\in\lbrace 0,1\rbrace$. Write $p_1=p_\theta(1)$ and $q_1=q(1)$ for the current and sampling policies’ probabilities of action 1. Their probabilities of action 0 are $1-p_1$ and $1-q_1$. Assume both probabilities lie strictly between zero and one.

To describe a parameter update, we need a parameter that determines the action probabilities. In this example the model has one real-valued parameter $\theta$. It determines $p_1$ through

\[p_1=\sigma(\theta)=\frac{1}{1+e^{-\theta}},\qquad \theta=\log\frac{p_1}{1-p_1}.\]

The ratio $p_1/(1-p_1)$ is the odds of action 1 relative to action 0. Its natural logarithm is the logit. Equal probabilities give odds one and logit zero; a positive logit favors action 1, and a negative logit favors action 0. Increasing $\theta$ increases $p_1$. The function $\sigma$, called the logistic function, converts the unconstrained real parameter into a probability. We update $\theta$, while the sampling probability $q_1$ stays fixed.

The score tells us how the sampled action’s log-probability changes when $\theta$ increases. Since $\mathrm d p_1/\mathrm d\theta=p_1(1-p_1)$, the chain rule gives

\[\begin{aligned} s_p(1)&=\frac{\mathrm d\log p_1}{\mathrm d\theta} =\frac{1}{p_1}p_1(1-p_1)=1-p_1,\\ s_p(0)&=\frac{\mathrm d\log(1-p_1)}{\mathrm d\theta} =-\frac{1}{1-p_1}p_1(1-p_1)=-p_1. \end{aligned}\]

The signs agree with the probability changes: increasing $\theta$ makes action 1 more likely and action 0 less likely. Both cases can be written as $s_p(y)=y-p_1$.

We can now compute the average score under the current policy. Averaging means weighting each score by how often its action is sampled. For $p_1=0.6$, action 1 has score $0.4$ and occurs with probability $0.6$; action 0 has score $-0.6$ and occurs with probability $0.4$. The contributions are $0.24$ and $-0.24$. In general,

\[\mathbb E_{y\sim p_\theta}[s_p(y)] =p_1(1-p_1)+(1-p_1)(-p_1)=0.\]

The positive score occurs more often in this example, but the less frequent negative score has a larger magnitude. Their probability-weighted contributions balance exactly. Now sample with $q_1=0.8$ while keeping the current probability $p_1=0.6$. The scores remain $0.4$ and $-0.6$, but their frequencies change: the average is $0.8\cdot0.4+0.2\cdot(-0.6)=0.2$. In symbols,

\[\mathbb E_{y\sim q}[s_p(y)] =q_1(1-p_1)+(1-q_1)(-p_1)=q_1-p_1.\]

Finally, give both actions reward one: $R(0)=R(1)=1$. The objective $J(\theta)$ is the current policy’s expected reward, so

\[J(\theta)=\mathbb E_{y\sim p_\theta}[R(y)] =p_1\cdot1+(1-p_1)\cdot1=1.\]

Changing the action probabilities cannot change this reward. Thus $J(\theta)$ has gradient zero. Yet the raw expected update remains $g_{\rm raw}=0.2$: with a positive learning rate it increases $\theta$ and hence $p_1$, without improving expected reward. This is an example of the drift term defined in Section 2.2.

The zero mean under the current policy extends beyond two actions. Multiplying a log-probability gradient by its probability gives the probability gradient itself. These gradients sum to zero because the probabilities always sum to one:

\[\mathbb E_p[s_p] =\sum_a p_\theta(a)\nabla_\theta\log p_\theta(a) =\nabla_\theta\sum_a p_\theta(a) =\nabla_\theta 1=0.\]

This calculation assumes positive differentiable action probabilities. Using a different distribution $q$ as the weights no longer gives the derivative of that normalized sum.

考虑动作 $y\in\lbrace 0,1\rbrace$。记 $p_1=p_\theta(1)$、$q_1=q(1)$,分别表示当前策略和采样策略选择动作 1 的概率。选择动作 0 的概率相应为 $1-p_1$ 和 $1-q_1$。假设这两个概率都严格介于零和一之间。

要说明参数如何更新,需要先明确参数与动作概率之间的关系。这个例子的模型只有一个实数参数 $\theta$,它通过下面的关系决定概率 $p_1$:

\[p_1=\sigma(\theta)=\frac{1}{1+e^{-\theta}},\qquad \theta=\log\frac{p_1}{1-p_1}.\]

比值 $p_1/(1-p_1)$ 表示选择动作 1 与选择动作 0 的概率之比,称为 odds;对它取自然对数,就得到 logit。两种动作等概率时,比值为一,logit 为零;logit 为正表示更偏向动作 1,为负则表示更偏向动作 0。增大 $\theta$ 会提高 $p_1$。函数 $\sigma$ 称为 logistic 函数,它将可取任意实数的参数转换成概率。我们更新的是 $\theta$,采样概率 $q_1$ 在这次更新中保持不变。

Score 描述增大 $\theta$ 时,采样动作的对数概率如何变化。由 $\mathrm d p_1/\mathrm d\theta=p_1(1-p_1)$,利用链式法则分别求导:

\[\begin{aligned} s_p(1)&=\frac{\mathrm d\log p_1}{\mathrm d\theta} =\frac{1}{p_1}p_1(1-p_1)=1-p_1,\\ s_p(0)&=\frac{\mathrm d\log(1-p_1)}{\mathrm d\theta} =-\frac{1}{1-p_1}p_1(1-p_1)=-p_1. \end{aligned}\]

正负号与概率的变化一致:增大 $\theta$ 会提高动作 1 的概率,降低动作 0 的概率。两种情况可以合写为 $s_p(y)=y-p_1$。

现在可以计算当前策略下的 score 平均值。求平均,就是按每个动作出现的概率,对它的 score 加权。当 $p_1=0.6$ 时,动作 1 的 score 为 $0.4$,出现概率为 $0.6$;动作 0 的 score 为 $-0.6$,出现概率为 $0.4$。两项加权贡献分别为 $0.24$ 和 $-0.24$。一般地,

\[\mathbb E_{y\sim p_\theta}[s_p(y)] =p_1(1-p_1)+(1-p_1)(-p_1)=0.\]

这个例子中,正的 score 出现得更频繁,但较少出现的负 score 绝对值更大,按概率加权后恰好平衡。现在保持当前概率 $p_1=0.6$,改用 $q_1=0.8$ 采样。两个 score 仍为 $0.4$ 和 $-0.6$,改变的是它们出现的频率:平均值变成 $0.8\cdot0.4+0.2\cdot(-0.6)=0.2$。写成公式就是

\[\mathbb E_{y\sim q}[s_p(y)] =q_1(1-p_1)+(1-q_1)(-p_1)=q_1-p_1.\]

最后,令两个动作的奖励都为一,即 $R(0)=R(1)=1$。目标 $J(\theta)$ 是当前策略下的期望奖励,因此

\[J(\theta)=\mathbb E_{y\sim p_\theta}[R(y)] =p_1\cdot1+(1-p_1)\cdot1=1.\]

无论怎样改变动作概率,奖励都不会改变,所以 $J(\theta)$ 的梯度为零。但未经修正的期望更新仍为 $g_{\rm raw}=0.2$:使用正学习率,它会增大 $\theta$,进而提高 $p_1$,却不会提高期望奖励。这是第 2.2 节将定义的漂移项(drift term)的一个例子。

当前策略下的 score 均值为零,并不限于二元动作。将对数概率的梯度乘以对应概率,得到的正是概率本身的梯度;所有概率之和始终为一,因此这些梯度的和为零:

\[\mathbb E_p[s_p] =\sum_a p_\theta(a)\nabla_\theta\log p_\theta(a) =\nabla_\theta\sum_a p_\theta(a) =\nabla_\theta 1=0.\]

这里假设各动作概率为正且可微。若改用另一个分布 $q$ 的概率作为权重,就不再是对这个归一化的概率和求导。

2. What Score Centering Corrects

2.1 Centering under the Sampling Policy

We can derive the centering correction from the constant-reward requirement, rather than introduce it as a formula. Keep the single-decision objective from Section 1; any state or prefix is held fixed and omitted from the notation. If every action has reward $R(a)=1$, then $J(\theta)=\mathbb E_{a\sim p_\theta}[R(a)]=1$. Changing any model parameter cannot improve this objective, so its gradient must be zero in every parameter direction.

The raw update uses the current policy’s score $s_p(a)=\nabla_\theta\log p_\theta(a)$, but the action is drawn from $q$. With constant reward, its expectation is

\[\mathbb E_{a\sim q}[R(a)s_p(a)]=\mathbb E_{a\sim q}[s_p(a)],\]

which need not be zero when the sampling and current policies differ. Even though the reward expresses no preference between actions, the uncorrected update can have a systematic direction.

To remove that direction, consider subtracting the same vector $c$ from every sampled action’s score. The vector may depend on the fixed policies and context, but not on which action is drawn. For constant reward, the desired condition is

\[\mathbb E_{a\sim q}[s_p(a)-c] =\mathbb E_{a\sim q}[s_p(a)]-c=0.\]

Thus, among such action-independent subtractions, the required choice is exactly the sampling-policy mean:

\[c=\bar s=\mathbb E_{a\sim q}[s_p(a)].\]

This gives SC. For an observed action $y\sim q$ and a general reward $R(y)$, its single-sample update is

\[\widehat g_{\rm SC}=R(y)\bigl(s_p(y)-\bar s\bigr).\]

The bar denotes the score mean; the hat denotes a sampled update. With an exact $\bar s$, constant reward gives zero expected update. This does not require every individual sample or finite batch to produce zero. In the binary example, subtracting $\bar s=q_1-p_1$ from $y-p_1$ gives $y-q_1$, whose mean is zero because $\mathbb E_q[y]=q_1\cdot1+(1-q_1)\cdot0=q_1$.

Matching the zero gradient for constant reward does not establish unbiasedness for general rewards. For $R=1$, exact SC does recover the correct gradient in expectation. When rewards vary with the action, however, the sample frequencies still come from $q$, and centering alone need not recover the gradient under $p_\theta$. Sections 2.2–2.3 make this distinction explicit.

可以从恒定奖励时应满足的条件出发,推导中心化修正。沿用第 1 节的单次决策目标,将状态或前缀视为固定条件并省略。如果所有动作的奖励都是 $R(a)=1$,那么 $J(\theta)=\mathbb E_{a\sim p_\theta}[R(a)]=1$。改变任何一个模型参数,都不能提高这个目标,因此目标梯度在每个参数方向上都应为零。

但未经修正的更新使用当前策略的 score $s_p(a)=\nabla_\theta\log p_\theta(a)$,动作却来自 $q$。在恒定奖励下,其期望为

\[\mathbb E_{a\sim q}[R(a)s_p(a)]=\mathbb E_{a\sim q}[s_p(a)],\]

当采样策略与当前策略不同时,这个量未必为零。也就是说,奖励虽然没有表达对不同动作的偏好,未经修正的更新仍可能具有非零的平均方向。

为了去掉这个方向,可以考虑从每个采样动作的 score 中减去同一个向量 $c$。这个向量可以依赖固定的策略和上下文,但不能依赖本次具体采到了哪个动作。恒定奖励时,我们希望它满足

\[\mathbb E_{a\sim q}[s_p(a)-c] =\mathbb E_{a\sim q}[s_p(a)]-c=0.\]

因此,在这种对所有动作减去同一向量的修正中,需要减去的恰好就是采样策略下的 score 均值:

\[c=\bar s=\mathbb E_{a\sim q}[s_p(a)].\]

这就得到 SC。对于观测动作 $y\sim q$ 及一般的奖励 $R(y)$,它的单样本更新为

\[\widehat g_{\rm SC}=R(y)\bigl(s_p(y)-\bar s\bigr).\]

横线表示 score 均值,帽子表示采样更新。当 $\bar s$ 精确时,恒定奖励对应的期望更新为零;这并不要求每个样本或每个有限批次的更新都为零。 在二元例子中,从 $y-p_1$ 减去 $\bar s=q_1-p_1$,得到 $y-q_1$;由于 $\mathbb E_q[y]=q_1\cdot1+(1-q_1)\cdot0=q_1$,其均值为零。

在恒定奖励下恢复正确的零梯度,并不等于在一般奖励下也无偏。 对 $R=1$,精确 SC 的期望确实等于目标梯度;但当奖励随动作变化时,样本的出现频率仍由 $q$ 决定,仅靠中心化未必能恢复 $p_\theta$ 下的梯度。第 2.2–2.3 节将进一步说明这一区别。

2.2 Covariance and Reward Offsets

The covariance between reward and score measures their joint variation under the same sampled action. For a vector-valued score, it is computed component by component:

\[\operatorname{Cov}\nolimits_{q}(R,s_p) =\mathbb E_q\bigl[(R-\mathbb E_q[R])(s_p-\bar s)\bigr].\]

For one parameter component, an action with both above-average reward and above-average score contributes positively; one with opposite deviations contributes negatively. The covariance averages these contributions using the action probabilities under $q$. Here $\bar s=\mathbb E_q[s_p]$ is the mean defined in Section 2.1. Expanding this expression separates the raw expected update into two terms:

\[\begin{aligned} \mathbb E_q[R\,s_p] &=\mathbb E_q[R]\,\bar s+\operatorname{Cov}\nolimits_{q}(R,s_p),\\ \mathbb E_q[\widehat g_{\rm SC}]&=\operatorname{Cov}\nolimits_{q}(R,s_p). \end{aligned}\]

The drift term $\mathbb E_q[R]\bar s$ depends on the mean reward and mean score. The covariance term describes how reward differences across actions are associated with score differences. SC removes the former and retains the latter. Adding a constant to every reward therefore leaves the SC expectation unchanged, while changing the raw expectation by that constant times $\bar s$.

A reward baseline is a value subtracted from the reward before weighting the score. Taking the action-independent value $b=\mathbb E_q[R]$ gives the same expected covariance in this single-decision setting, but the sampled updates $R(s_p-\bar s)$ and $(R-b)s_p$ generally differ. This $b$ is the exact population mean, not a mean estimated from a finite group of responses; Section 4.2 discusses group reward centering.

奖励与 score 的协方差(covariance)描述两者在同一个采样动作上如何共同变化。Score 为向量时,对其各分量分别计算:

\[\operatorname{Cov}\nolimits_{q}(R,s_p) =\mathbb E_q\bigl[(R-\mathbb E_q[R])(s_p-\bar s)\bigr].\]

对某个参数分量而言,如果一个动作的奖励和 score 都高于各自均值,两者偏差的乘积为正;如果一个高于均值、另一个低于均值,乘积为负。协方差再按照动作在 $q$ 下的概率,对这些乘积求平均。这里 $\bar s=\mathbb E_q[s_p]$ 是第 2.1 节定义的均值。展开这个式子,可以将未经修正的期望更新分成两项:

\[\begin{aligned} \mathbb E_q[R\,s_p] &=\mathbb E_q[R]\,\bar s+\operatorname{Cov}\nolimits_{q}(R,s_p),\\ \mathbb E_q[\widehat g_{\rm SC}]&=\operatorname{Cov}\nolimits_{q}(R,s_p). \end{aligned}\]

漂移项 $\mathbb E_q[R]\bar s$ 取决于奖励均值和 score 均值;协方差项描述不同动作的奖励差异与 score 差异之间的关联。SC 去掉前者,保留后者。因此,给所有奖励加上同一个常数,SC 的期望不变,而未经修正的期望会改变“该常数乘 $\bar s$”。

奖励基线(reward baseline)指在对 score 加权之前,从奖励中减去的数值。取不依赖具体动作的 $b=\mathbb E_q[R]$,也能在这个单步设定中得到相同的期望协方差,但采样更新 $R(s_p-\bar s)$ 与 $(R-b)s_p$ 通常不同。这里的 $b$ 是精确的总体均值,并非从有限条回答估计出的组均值;第 4.2 节会讨论组内奖励中心化。

2.3 The Scope of the Correction

The SC paper, Section 4, distinguishes the covariance under the sampler from the covariance under the current policy. After centering, the expected update remains $\operatorname{Cov}\nolimits_{q}(R,s_p)$; the target gradient is $\operatorname{Cov}\nolimits_{p}(R,s_p)$, since $\mathbb E_p[s_p]=0$. An estimate is unbiased for $g_p=\nabla_\theta J$ when its expectation equals that target gradient. These two covariances generally differ when $p\ne q$: SC subtracts a mean, but the actions still occur with frequencies determined by $q$, so their reward–score associations are still averaged with those frequencies. SC therefore restores the zero-mean score property without generally recovering an unbiased target-gradient estimate. When the sampling and current policies agree, $p=q$, the setting is called on-policy. The exact correction is zero and SC leaves the on-policy update unchanged.

SC 原文第 4 节区分了采样策略与当前策略下的协方差。中心化后的期望更新仍为 $\operatorname{Cov}\nolimits_{q}(R,s_p)$;由于 $\mathbb E_p[s_p]=0$,目标梯度则为 $\operatorname{Cov}\nolimits_{p}(R,s_p)$。估计量对 $g_p=\nabla_\theta J$ 无偏,指其期望等于这个目标梯度。当 $p\ne q$ 时,上述两个协方差通常不同:SC 减去了一个均值,但动作仍按 $q$ 的概率出现,奖励与 score 的关联也仍按这些概率求平均。因此,SC 恢复了 score 的零均值性质,但一般不构成目标梯度的无偏估计。当采样策略与当前策略一致,即 $p=q$ 时,称为 on-policy。此时精确修正项为零,SC 保持原有的 on-policy 更新不变。

A binary-action example with constant reward $R=1$. Adjust $p_1=p_\theta(1)$ and $q_1=q(1)$, the two policies' probabilities of action 1. The raw expected update is $q_1-p_1$; exact SC and exact IS both give zero, matching the gradient of the constant objective. The figure evaluates analytic expectations.
奖励恒为 $R=1$ 的二元动作例子。调整两种策略选择动作 1 的概率 $p_1=p_\theta(1)$ 和 $q_1=q(1)$:未经修正的期望更新为 $q_1-p_1$,精确 SC 与精确 IS 的期望均为零,与常数目标的梯度一致。图中展示解析期望。

3. Score Centering and Importance Sampling

3.1 Correcting the Action Distribution

Importance sampling (IS) assigns each sampled action the weight $w(a)=p(a)/q(a)$. When an action is less probable under $q$ than under $p$, its contribution receives a larger weight; the reverse holds when it is more probable. For the binary example, action 1 accounts for probability $0.8$ under the sampler; multiplying by $0.6/0.8$ gives an effective contribution weight of $0.6$. Action 0 similarly gives $0.2\cdot(0.4/0.2)=0.4$. These are exactly the current policy’s probabilities. In general,

\[\begin{aligned} \mathbb E_q[wR\,s_p] &=\sum_a q(a)\frac{p(a)}{q(a)}R(a)s_p(a)\\ &=\mathbb E_p[R\,s_p]=g_p. \end{aligned}\]

The support of a policy is the set of actions to which it assigns positive probability. A sufficient support condition for this identity is $q(a)>0$ whenever $p(a)>0$. If this support condition fails, an action that contributes to the target average can never appear in the sampled data; no weight on the observed actions can restore its missing contribution. We also require the weighted update to have a finite expected magnitude. Even with adequate support, large probability ratios can cause large sample-to-sample fluctuations, or high variance. Clipping replaces weights above a chosen limit by that limit. This bounds their magnitude but generally changes the expected update, introducing bias relative to $g_p$.

SC and IS therefore make different corrections. SC makes the score mean zero under the sampling policy. Exact IS converts the reward-weighted score expectation from the sampling policy to the target policy. The sampling distribution itself remains $q$ in both cases.

Importance sampling(重要性采样,IS)为每个采样动作赋予权重 $w(a)=p(a)/q(a)$。如果一个动作在 $q$ 下的概率低于在 $p$ 下的概率,就提高它在更新中的权重;反之则降低。沿用二元例子:动作 1 在采样中占比为 $0.8$,乘以权重 $0.6/0.8$ 后,有效贡献权重变为 $0.6$;动作 0 则有 $0.2\cdot(0.4/0.2)=0.4$。这正好还原了当前策略的两个概率。一般地,加权后的期望满足

\[\begin{aligned} \mathbb E_q[wR\,s_p] &=\sum_a q(a)\frac{p(a)}{q(a)}R(a)s_p(a)\\ &=\mathbb E_p[R\,s_p]=g_p. \end{aligned}\]

策略的支持集是它赋予正概率的动作集合。这个恒等式的一个充分条件是:只要 $p(a)>0$,就有 $q(a)>0$。如果支持集条件不满足,某个本应计入目标平均值的动作永远不会出现在采样数据中,仅对已观测动作加权无法补回它的贡献。此外,加权更新的大小需要具有有限期望。即使满足支持集条件,较大的概率比仍可能使不同样本的更新差异很大,即方差较高。截断将超过指定上限的权重替换为该上限;它限制了权重大小,但通常也改变期望更新,使其相对 $g_p$ 产生偏差。

因此,SC 与 IS 修正的是不同性质。SC 使 score 在采样策略下的均值为零;精确 IS 将奖励加权 score 的期望从采样策略转换到目标策略。 两种情况下,样本本身仍来自 $q$。

3.2 Centering a Weighted Score

The SC paper, Appendix A.2, applies centering to weighted scores. Let $u(a)$ denote a chosen action weight: it may be one, the exact IS ratio $w(a)=p(a)/q(a)$, or a clipped version of that ratio. Define $c_u$ as the mean of the weighted score under $q$, and let $\widehat g_u$ be the resulting single-sample update for $y\sim q$:

\[\begin{aligned} c_u&=\mathbb E_{a\sim q}[u(a)s_p(a)],\\ \widehat g_u&=R(y)\bigl(u(y)s_p(y)-c_u\bigr). \end{aligned}\]

For fixed policies and weights, $c_u$ is the same vector whichever action is sampled. Subtracting it makes a constant reward give zero expected update. With exact weights $u=w$, the assumptions of Section 3.1 give $c_u=\mathbb E_p[s_p]=0$. Clipping, or masking selected weights to zero, can make this mean nonzero. The scalar-loss implementation in Section 4.3 treats these weights as fixed coefficients during differentiation.

Why the order of weighting and centering matters

A quantity with mean zero need not retain mean zero after multiplication by different action-dependent weights. The correction must therefore use the mean of the weighted score actually used in the update. Recall $\bar s=\mathbb E_q[s_p]$, the unweighted score mean, and $w(a)=p(a)/q(a)$, the exact IS weight. Centering the score first and then multiplying by $w$ gives

\[\mathbb E_q[wR(s_p-\bar s)] =g_p-\mathbb E_p[R]\,\bar s.\]

Here $g_p=\nabla_\theta J$ is the target reward gradient from Section 1.1. The additional term is generally nonzero. Thus this order differs from centering the weighted score, and “SC + IS” must specify which estimator is used.

SC 原文附录 A.2将中心化用于加权 score。令 $u(a)$ 表示选定的动作权重:它可以恒为一,可以取精确 IS 比值 $w(a)=p(a)/q(a)$,也可以取截断后的比值。定义 $c_u$ 为加权 score 在 $q$ 下的均值,并用 $\widehat g_u$ 表示动作 $y\sim q$ 对应的单样本更新:

\[\begin{aligned} c_u&=\mathbb E_{a\sim q}[u(a)s_p(a)],\\ \widehat g_u&=R(y)\bigl(u(y)s_p(y)-c_u\bigr). \end{aligned}\]

固定策略和权重后,无论采到哪个动作,$c_u$ 都是同一个向量。减去它,就使恒定奖励对应的期望更新为零。使用精确权重 $u=w$ 时,在第 3.1 节的条件下有 $c_u=\mathbb E_p[s_p]=0$。截断权重,或通过掩码将选定权重置零,可能使该均值非零。第 4.3 节的标量损失实现会在求导时将这些权重视为固定系数。

加权与中心化的顺序为何重要

一个量的平均值为零,不表示再按动作乘上不同权重后,平均值仍为零。因此,修正时需要计算最终实际使用的加权 score 的均值。回顾未加权的 score 均值 $\bar s=\mathbb E_q[s_p]$,以及精确 IS 权重 $w(a)=p(a)/q(a)$。如果先对 score 中心化,再乘以 $w$,得到

\[\mathbb E_q[wR(s_p-\bar s)] =g_p-\mathbb E_p[R]\,\bar s.\]

这里 $g_p=\nabla_\theta J$ 是第 1.1 节定义的目标奖励梯度。额外一项一般非零,因此这个顺序与“对加权 score 中心化”不同;讨论“SC + IS”时,需要明确所用的估计量。

4. Score Centering in Language Models

4.1 Token Corrections and Sequence-Level Gradients

A token is a unit of generated text. Fix a prompt $x$, and write a complete response as $\tau=(y_1,\ldots,y_T)$, where $y_t$ is the token at position $t$ and $T$ is the number of generated tokens. The prefix before position $t$ is $h_t=(x,y_1,\ldots,y_{t-1})$. It includes the prompt and all previously generated tokens.

At a fixed prefix $h$, the distributions $p_\theta(a\mid h)$ and $q(a\mid h)$ give the next-token probabilities. The conditional score and its sampling-policy mean are

\[\begin{aligned} s_p(a,h)&=\nabla_\theta\log p_\theta(a\mid h),\\ \bar s(h)&=\sum_a q(a\mid h)s_p(a,h). \end{aligned}\]

SC subtracts $\bar s(h)$ from the sampled token’s score. Exact token IS instead weights it by $p_\theta(a\mid h)/q(a\mid h)$. Both operations concern a fixed prefix; neither alone corrects how often that prefix is reached or which tokens are generated afterward.

For example, a prefix may occur with probability $0.9$ under the current policy and $0.5$ under the sampling policy. Even if their next-token distributions at that prefix are identical, making the local ratios one, the prefix is still underrepresented in the sampled data. If the reward is assigned only after completing the response, the expected reward can also depend on which policy generates the remaining tokens.

The objective $J$ from Section 1.1 now applies to complete responses: $J(\theta)=\mathbb E_{\tau\sim p_\theta}[R(\tau)]$, where $R(\tau)$ is the reward for the whole response. We suppress the fixed prompt $x$ in $p_\theta(\tau)$ and $q(\tau)$. The probability of a complete response is the product of its conditional token probabilities. Taking the logarithm turns this product into a sum; differentiating the log-probability therefore gives the sum of the token scores. Applying IS to the entire response gives the following identity (SC paper, Section 2.2):

\[\nabla_\theta J(\theta) =\mathbb E_{\tau\sim q}\!\left[ \frac{p_\theta(\tau)}{q(\tau)}R(\tau) \sum_{t=1}^{T}\nabla_\theta\log p_\theta(y_t\mid h_t) \right].\] \[\frac{p_\theta(\tau)}{q(\tau)} =\prod_{t=1}^{T}\frac{p_\theta(y_t\mid h_t)}{q(y_t\mid h_t)}.\]

Each factor uses the probability of the observed token at its own prefix; the product gives the probability ratio for the full response. Include any sampled end-of-response token in this factorization. The identity assumes a parameter-independent reward, the support and finite-expectation conditions from Section 3.1 applied to responses, and probabilities matching the actual sampling procedure. A single token ratio multiplying the terminal reward generally does not equal this full-response correction. Computing ratios in log space improves numerical evaluation but does not remove sample-to-sample variation in their products.

Token 是生成文本的一个单位。固定输入 prompt $x$,将完整回答记为 $\tau=(y_1,\ldots,y_T)$,其中 $y_t$ 是第 $t$ 个 token,$T$ 是生成的 token 数。位置 $t$ 之前的前缀为 $h_t=(x,y_1,\ldots,y_{t-1})$,包括 prompt 和此前生成的全部 token。

固定一个前缀 $h$,$p_\theta(a\mid h)$ 和 $q(a\mid h)$ 分别给出下一个 token 的概率。条件 score 及其在采样策略下的均值为

\[\begin{aligned} s_p(a,h)&=\nabla_\theta\log p_\theta(a\mid h),\\ \bar s(h)&=\sum_a q(a\mid h)s_p(a,h). \end{aligned}\]

SC 从采样 token 的 score 中减去 $\bar s(h)$;精确 token IS 则使用 $p_\theta(a\mid h)/q(a\mid h)$ 加权。两种操作都针对固定前缀,本身并未同时修正此前缀的出现概率和后续 token 的生成分布。

例如,一个前缀在当前策略下出现的概率为 $0.9$,在采样策略下为 $0.5$。即使两者在这个前缀下的下一 token 分布相同、局部概率比全部为一,该前缀在采样数据中仍然不足。如果奖励在回答完成后才给出,后续 token 由哪种策略生成,也可能改变期望奖励。

第 1.1 节的目标 $J$ 现在用于完整回答:$J(\theta)=\mathbb E_{\tau\sim p_\theta}[R(\tau)]$,其中 $R(\tau)$ 为整条回答的奖励。为简洁起见,$p_\theta(\tau)$ 和 $q(\tau)$ 省略了固定 prompt $x$。SC 原文第 2.2 节给出了完整序列 IS 恒等式:整条回答的概率是各步条件概率的乘积,取对数将乘积变成求和,再对参数求导便得到 token score 之和;对整条回答使用 IS,便得到下面的式子

\[\nabla_\theta J(\theta) =\mathbb E_{\tau\sim q}\!\left[ \frac{p_\theta(\tau)}{q(\tau)}R(\tau) \sum_{t=1}^{T}\nabla_\theta\log p_\theta(y_t\mid h_t) \right].\] \[\frac{p_\theta(\tau)}{q(\tau)} =\prod_{t=1}^{T}\frac{p_\theta(y_t\mid h_t)}{q(y_t\mid h_t)}.\]

每个因子使用观测 token 在其对应前缀下的概率;乘积就是整条回答的概率比。若回答通过采样结束 token 终止,也要将其计入。这一恒等式要求奖励不直接依赖参数,将第 3.1 节的支持集和有限期望条件应用到完整回答,并使用实际采样过程的概率。仅用单个 token 概率比乘以终止奖励,通常不等于这个完整回答修正。在 log 空间计算可以改善数值运算,但不会消除这些乘积在不同样本之间的波动。

4.2 Group Reward Centering and Conditional Score Centering

The SC paper, Section 4, distinguishes prompt-level reward centering from prefix-level score centering. Suppose a group contains $G$ responses to the same prompt, indexed by $i=1,\ldots,G$. Let $R_i$ be response $i$’s reward. Define the group’s mean reward $\bar R$ and centered reward $A_i$ by

\[\bar R=\frac1G\sum_{i=1}^{G}R_i,\qquad A_i=R_i-\bar R.\]

Here $A_i$ is the reward-baseline difference, often called an advantage, used to weight response $i$’s token scores. By construction, $\sum_i A_i=0$. If all responses share the same prefix $h$ and the same policies, their exact score correction is the same vector $\bar s(h)$ from Section 4.1. Its equally weighted group contribution cancels:

\[\sum_{i=1}^{G}A_i\bar s(h) =\bar s(h)\sum_{i=1}^{G}A_i=0.\]

This applies at the shared initial prefix. Later, let $h_{i,t}$ denote response $i$’s prefix before token $t$. These prefixes and their corrections $\bar s(h_{i,t})$ can differ, so the correction cannot in general be factored out of the sum. Group reward centering therefore does not generally make prefix-conditional score centering redundant.

SC 原文第 4 节区分了 prompt 级别的奖励中心化与前缀级别的 score 中心化。设同一 prompt 有一组 $G$ 条回答,用 $i=1,\ldots,G$ 编号,$R_i$ 表示第 $i$ 条回答的奖励。定义组内奖励均值 $\bar R$ 和中心化奖励 $A_i$:

\[\bar R=\frac1G\sum_{i=1}^{G}R_i,\qquad A_i=R_i-\bar R.\]

这里 $A_i$ 是奖励减去基线后的差值,常称为 advantage,用于对第 $i$ 条回答的 token score 加权。按定义有 $\sum_i A_i=0$。如果各条回答共享前缀 $h$,并使用相同策略,精确的 score 修正项就是第 4.1 节定义的同一个向量 $\bar s(h)$。等权求和时,它的组内贡献抵消:

\[\sum_{i=1}^{G}A_i\bar s(h) =\bar s(h)\sum_{i=1}^{G}A_i=0.\]

共享的初始前缀满足这一条件。对后续位置,令 $h_{i,t}$ 表示第 $i$ 条回答在 token $t$ 之前的前缀。不同回答的前缀及修正项 $\bar s(h_{i,t})$ 可能不同,因此一般不能将修正项提到求和符号外。组内奖励中心化通常不能使逐前缀的条件 score 中心化变得多余。

4.3 Implementing SC with a Surrogate Loss

The scalar-loss formulation follows the SC paper, Appendix A.3. A surrogate loss is a scalar expression constructed to produce a specified update by differentiation. Fix a prefix $h$, and abbreviate $p_\theta(a\mid h)$ and $q(a\mid h)$ as $p_\theta(a)$ and $q(a)$ in this subsection. Let $y$ be the sampled next token, and $R$ the reward of the completed response containing it.

Define $\operatorname{sg}$, or stop-gradient, to return its input value but treat it as constant during differentiation. A local SC loss is

\[L_{\rm SC}=-\operatorname{sg}(R) \left[\log p_\theta(y)-\sum_a\operatorname{sg}(q(a))\log p_\theta(a)\right],\]

where the sum runs over the vocabulary. One backward pass computes its parameter gradient. The minus sign makes gradient descent on this loss implement the SC ascent direction:

\[-\nabla_\theta L_{\rm SC} =R\bigl(s_p(y,h)-\bar s(h)\bigr).\]

Thus the vocabulary sum can be differentiated as one scalar expression; no separate parameter-gradient vector must be stored for each action. For an advantage-weighted update, replace $R$ by a specified coefficient such as $A_i=R_i-\bar R$ from Section 4.2 and stop its gradient. For weighted SC from Section 3.2, use detached coefficients $u(y)$ in the sampled term and $q(a)u(a)$ in the correction term. “Detached” means treated as constant by stop-gradient. Rewards and weights are coefficients in the specified update: differentiating these coefficients as well would apply the product rule and introduce terms absent from that update.

This loss implements a local correction at the fixed prefix. The distinction from a complete-response gradient remains the one described in Section 4.1.

这里的标量损失形式依据SC 原文附录 A.3。Surrogate loss 是为通过求导产生指定更新而构造的标量损失。固定前缀 $h$,本小节将 $p_\theta(a\mid h)$ 和 $q(a\mid h)$ 简写为 $p_\theta(a)$ 和 $q(a)$。令 $y$ 表示采样到的下一个 token,$R$ 表示包含这个 token 的完整回答所获得的奖励。

定义 $\operatorname{sg}$ 为停止梯度(stop-gradient)操作:返回输入数值,但在求导时将其视为常数。局部 SC 损失为

\[L_{\rm SC}=-\operatorname{sg}(R) \left[\log p_\theta(y)-\sum_a\operatorname{sg}(q(a))\log p_\theta(a)\right],\]

求和遍历整个词表。一次反向传播计算该损失对参数的梯度;负号使损失的梯度下降对应 SC 的梯度上升方向:

\[-\nabla_\theta L_{\rm SC} =R\bigl(s_p(y,h)-\bar s(h)\bigr).\]

因此,可以将词表求和作为一个标量表达式求导,无需为每个动作分别保存参数梯度向量。如果使用 advantage 加权更新,就用指定系数替换 $R$,例如第 4.2 节的 $A_i=R_i-\bar R$,并停止其梯度。对于第 3.2 节的加权 SC,采样项使用 detach 后的系数 $u(y)$,修正项使用 detach 后的系数 $q(a)u(a)$;detach 即通过停止梯度将系数视为常数。奖励和权重在指定更新中只是乘数;若继续对这些系数求导,乘积法则会产生更新公式中没有的额外项。

这个损失实现的是固定前缀处的局部修正。它与完整回答梯度的区别,仍是第 4.1 节讨论的区别。

Implementation details: annotated code实现细节:代码与注释
Illustrative code using the full action distribution for a single decision or fixed prefix. Coefficients multiplying log-probabilities are detached so that differentiation produces the specified update. The code implements local estimators.
示例代码使用完整动作分布,适用于单次决策或固定前缀。与对数概率相乘的系数均停止梯度,使求导结果对应指定更新。代码实现的是局部估计量。
Implementation details: a top-k approximation实现细节:top-k 近似

The official implementation stores only the $k$ highest-probability actions under the sampling policy, where $k$ is the number of recorded actions. Work at one fixed prefix and omit it from the notation as in Section 4.3. Let $H$ be the recorded action set. The omitted, or tail, probability masses are

\[P_{\mathrm{tail}}=1-\sum_{a\in H}p_\theta(a),\qquad Q_{\mathrm{tail}}=1-\sum_{a\in H}q(a).\]

The subscript “tail” refers to actions outside $H$, not response length. When $P_{\mathrm{tail}}>0$, approximate the unrecorded sampling probabilities in proportion to the current policy:

\[\widehat q(a)= \begin{cases} q(a),&a\in H,\\ \dfrac{Q_{\mathrm{tail}}}{P_{\mathrm{tail}}}p_\theta(a),&a\notin H. \end{cases}\]

The factor $Q_{\mathrm{tail}}/P_{\mathrm{tail}}$ makes the approximated tail probabilities sum to the recorded tail mass $Q_{\mathrm{tail}}$. To compute the resulting score mean without enumerating tail actions, use the current policy’s zero mean score:

\[\sum_{a\notin H}p_\theta(a)s_p(a,h) =-\sum_{a\in H}p_\theta(a)s_p(a,h).\]

The head and tail together sum to zero, so the unknown tail contribution can be replaced by the negative head contribution. Combining it with the recorded head gives the approximate score mean $\widehat{\bar s}$:

\[\widehat{\bar s}=\sum_{a\in H} \left[q(a)-\frac{Q_{\mathrm{tail}}}{P_{\mathrm{tail}}}p_\theta(a)\right]s_p(a,h).\]

Here the hat denotes an approximation, rather than a random single-sample update. Subtracting this mean centers the score under $\widehat q$, but not necessarily under the actual $q$. The construction retains the omitted probability mass; renormalizing only the recorded actions defines a different distribution. A zero or numerically negligible $P_{\mathrm{tail}}$ requires explicit handling. In a log-probability surrogate, stop gradients through the bracketed coefficients, as in Section 4.3, to avoid additional derivative terms.

官方实现只记录采样策略下概率最高的 $k$ 个动作,其中 $k$ 是记录的动作数。这里仍固定一个前缀,并像第 4.3 节一样省略这一条件。令 $H$ 表示记录的动作集合,集合外的尾部(tail)概率质量为

\[P_{\mathrm{tail}}=1-\sum_{a\in H}p_\theta(a),\qquad Q_{\mathrm{tail}}=1-\sum_{a\in H}q(a).\]

下标“tail”指 $H$ 之外的动作,不表示回答长度。当 $P_{\mathrm{tail}}>0$ 时,按当前策略的概率比例近似未记录的采样概率:

\[\widehat q(a)= \begin{cases} q(a),&a\in H,\\ \dfrac{Q_{\mathrm{tail}}}{P_{\mathrm{tail}}}p_\theta(a),&a\notin H. \end{cases}\]

比例系数 $Q_{\mathrm{tail}}/P_{\mathrm{tail}}$ 使近似后的尾部概率之和仍等于记录的尾部质量 $Q_{\mathrm{tail}}$。计算 score 均值时,还可以利用当前策略下 score 均值为零这一性质,避免逐一遍历尾部动作:

\[\sum_{a\notin H}p_\theta(a)s_p(a,h) =-\sum_{a\in H}p_\theta(a)s_p(a,h).\]

因为集合内外的贡献之和为零,未知的尾部求和可以换成已记录部分求和的负值。将它与记录部分合并,得到近似 score 均值 $\widehat{\bar s}$:

\[\widehat{\bar s}=\sum_{a\in H} \left[q(a)-\frac{Q_{\mathrm{tail}}}{P_{\mathrm{tail}}}p_\theta(a)\right]s_p(a,h).\]

这里的帽子表示近似量,而非单个随机样本的更新。减去这个均值,使 score 在 $\widehat q$ 下中心化,但在实际 $q$ 下未必如此。这一构造保留了未记录部分的概率质量;只对已记录动作重新归一化,会得到不同的分布。$P_{\mathrm{tail}}$ 为零或在数值上过小时需要显式处理。若使用对数概率 surrogate,应像第 4.3 节一样对方括号中的系数停止梯度,避免引入额外求导项。

5. Two Interpretations of the Centered Update

5.1 Nan Jiang: the Linear-Softmax Case

Nan Jiang’s comment considers a linear-softmax policy. To spell out the observation, fix a feature vector $x$ and let $p=\operatorname{softmax}(Wx)$, where $W$ is the trainable weight matrix and $p$ is the action-probability vector. Let $e_a$ be the one-hot vector for action $a$. The score with respect to $W$ and its exact centered form are

\[\begin{aligned} s_W(a)&=(e_a-p)x^\top,\\ s_W(a)-\mathbb E_q[s_W]&=(e_a-q)x^\top. \end{aligned}\]

The explicit dependence on $p$ cancels. If the sampler is $q=\operatorname{softmax}(Ux)$ with the same fixed features and parameter coordinates, this equals its score at weights $U$. The observation shows why SC need not recover the trainer’s target gradient. It does not mean SC leaves the update unchanged or has no effect: centering still removes the constant-reward drift.

Nan Jiang 的评论考虑了 linear-softmax 策略。将其观察写成公式:固定特征向量 $x$,令 $p=\operatorname{softmax}(Wx)$,其中 $W$ 是可训练的权重矩阵,$p$ 是动作概率向量。令 $e_a$ 为动作 $a$ 的 one-hot 向量。对 $W$ 求导得到的 score 及其精确中心化形式为

\[\begin{aligned} s_W(a)&=(e_a-p)x^\top,\\ s_W(a)-\mathbb E_q[s_W]&=(e_a-q)x^\top. \end{aligned}\]

显式的 $p$ 被抵消。如果 sampler 为 $q=\operatorname{softmax}(Ux)$,且使用相同的固定特征与参数坐标,这就等于它在权重 $U$ 处的 score。这一观察说明,SC 未必恢复 trainer 的目标梯度。 它不意味着 SC 没有改变更新或没有作用:中心化仍然消除了恒定奖励对应的 drift。

5.2 Yingru Li: The Trainer-Tangent Policy Family

The construction in Yingru Li’s comment gives another way to understand SC: start from the sampler’s probabilities, apply the trainer’s relative probability changes, and normalize. Centering then follows from differentiating this normalized distribution. We will first define the distribution exactly, before making any approximation.

What changes, and what stays fixed? Fix a history or prefix $h$. The current trainer is $p_\theta(a\mid h)$, and the sampler is $q(a\mid h)$. Hold both $q$ and the reference parameter vector $\theta$ fixed. Let $\delta$ be a candidate parameter increment, so $p_{\theta+\delta}$ describes how the trainer would change. $\delta$ has the same coordinates as $\theta$; $\delta=0$ means no increment, not that the model’s parameters are zero. For simplicity, assume finitely many actions, positive probabilities, and a trainer policy differentiable near $\theta$.

For each candidate $\delta$, we will construct a new probability distribution $\nu_\delta$. The collection of distributions indexed by $\delta$ is a policy family. It is an auxiliary mathematical construction; we have not yet updated either actual model.

Apply relative changes, then normalize. The ratio

\[\frac{p_{\theta+\delta}(a\mid h)}{p_\theta(a\mid h)}\]

measures how much the trainer multiplies the probability of action $a$. A ratio of $1.5$ means increasing that probability by 50%; a ratio of $0.5$ means halving it. Apply that same multiplier to the sampler’s probability, giving $q(a\mid h)p_{\theta+\delta}(a\mid h)/p_\theta(a\mid h)$. These new numbers are nonnegative, but their sum need not be one.

Define their sum as

\[\boxed{ Z_\delta(h)=\sum_u q(u\mid h) \frac{p_{\theta+\delta}(u\mid h)}{p_\theta(u\mid h)}. }\]

Here $u$ is just an index running over all possible actions. $Z_\delta(h)$ is one number for this prefix and increment, shared by every action. It is called a normalization constant because dividing each weight by this total makes the probabilities sum to one:

\[\boxed{ \nu_\delta(a\mid h)= \frac{q(a\mid h)\,\dfrac{p_{\theta+\delta}(a\mid h)}{p_\theta(a\mid h)}} {Z_\delta(h)}. }\]

“Constant” means common across actions, not constant as $\delta$ changes. The sum of the numerators is exactly $Z_\delta(h)$, so $\sum_a\nu_\delta(a\mid h)=Z_\delta(h)/Z_\delta(h)=1$.

Why is $\nu_0=q$? Substitute $\delta=0$ into the exact definition. For every action,

\[\frac{p_{\theta+0}(a\mid h)}{p_\theta(a\mid h)} =\frac{p_\theta(a\mid h)}{p_\theta(a\mid h)}=1.\]

Therefore

\[Z_0(h)=\sum_u q(u\mid h)\times1=1, \qquad \boxed{\nu_0(a\mid h)=\frac{q(a\mid h)\times1}{1}=q(a\mid h).}\]

No relative change has been applied, so we recover the sampler. There is no requirement that $q=p_\theta$. The two roles are different: the base distribution is $q$; the relative-change rule comes from the current trainer.

Differentiate the normalized family. Keep the history fixed and omit $h$ below. Write $\psi_\theta(a)=\nabla_\theta\log p_\theta(a)$, the same trainer score previously denoted $s_p(a)$. Taking the logarithm of the exact definition gives

\[\log\nu_\delta(a) =\log q(a)+\log p_{\theta+\delta}(a) -\log p_\theta(a)-\log Z_\delta.\]

We differentiate with respect to the increment $\delta$. The terms $\log q(a)$ and $\log p_\theta(a)$ are fixed, so their derivatives are zero. The numerator term varies through $\theta+\delta$, giving $\psi_\theta(a)$ when evaluated at $\delta=0$.

The remaining term is the normalization. Differentiate its finite sum:

\[\begin{aligned} \left.\nabla_\delta Z_\delta\right|_{\delta=0} &=\sum_u\frac{q(u)}{p_\theta(u)} \left.\nabla_\delta p_{\theta+\delta}(u)\right|_{\delta=0}\\ &=\sum_u\frac{q(u)}{p_\theta(u)}\nabla_\theta p_\theta(u)\\ &=\sum_u q(u)\psi_\theta(u) =\mathbb E_{u\sim q}[\psi_\theta(u)]. \end{aligned}\]

The third line uses $\nabla_\theta p_\theta=p_\theta\nabla_\theta\log p_\theta$.

Why does a derivative with respect to the increment become a parameter gradient? (click to expand)

The differentiation variable has not been changed midway. The reference parameter $\theta$ stays fixed while the increment $\delta$ varies. To distinguish a variable from the point where a derivative is evaluated, introduce a separate parameter variable $w$ and, for one fixed action $u$, define

\[f(w)=\log p_w(u),\qquad w=\theta+\delta.\]

Our expression is now $f(\theta+\delta)$. Changing one coordinate $\delta_i$ changes $w_i=\theta_i+\delta_i$ by exactly the same amount, leaving every other coordinate unchanged. The chain rule therefore gives

\[\nabla_\delta f(\theta+\delta) =\left.\nabla_w f(w)\right|_{w=\theta+\delta}.\]

The left side measures change with respect to the increment; the right side measures change with respect to the actual parameter. They agree because adding a fixed $\theta$ only shifts the origin of the parameter coordinates.

Differentiate first, then evaluate at $\delta=0$. The vertical bar denotes this second operation. At zero increment, the actual parameter is $w=\theta$, so

\[\boxed{ \left.\nabla_\delta\log p_{\theta+\delta}(u)\right|_{\delta=0} =\left.\nabla_w\log p_w(u)\right|_{w=\theta} =\psi_\theta(u). }\]

The middle expression is commonly abbreviated as $\nabla_\theta\log p_\theta(u)$: differentiate the policy’s log-probability with respect to its parameter, then evaluate at the current parameter value. This does not mean differentiating the entire $Z_\delta$ expression with respect to its reference $\theta$. In our calculation, the denominator $p_\theta(u)$ and $q(u)$ remain fixed.

A scalar example of the chain rule, unrelated to probabilities, makes the order explicit. With reference value $\theta=3$ and $f(w)=w^2$,

\[\left.\frac{d}{d\delta}(3+\delta)^2\right|_{\delta=0} =\left.2(3+\delta)\right|_{\delta=0} =6 =\left.\frac{d}{dw}w^2\right|_{w=3}.\]

Substituting zero before differentiation would leave only the constant $9$; differentiating that constant gives zero, not the required slope $6$. It discards how the original function changes near that point. A function’s value at a point is not its derivative there.

Return to the normalization factor. Keeping $\delta$ unevaluated during differentiation and using $\nabla p=p\nabla\log p$ gives

\[\nabla_\delta Z_\delta =\sum_u q(u)\frac{p_{\theta+\delta}(u)}{p_\theta(u)} \nabla_\delta\log p_{\theta+\delta}(u).\]

The probability ratio must still be present here. Only when we now evaluate at $\delta=0$ does it become one. The remaining derivative is the parameter gradient evaluated at $w=\theta$:

\[\left.\nabla_\delta Z_\delta\right|_{\delta=0} =\mathbb E_{u\sim q}\!\left[ \left.\nabla_w\log p_w(u)\right|_{w=\theta} \right] =\mathbb E_{u\sim q}[\psi_\theta(u)].\]

This expectation is the desired result: the trainer score averaged under the fixed sampler $q$. It need not be zero, because the averaging distribution need not be $p_\theta$.

Since the derivative of a logarithm is the derivative of its argument divided by that argument, and $Z_0=1$,

\[\left.\nabla_\delta\log Z_\delta\right|_{\delta=0} =\frac{1}{Z_0} \left.\nabla_\delta Z_\delta\right|_{\delta=0} =\mathbb E_q[\psi_\theta].\]

Knowing that $Z_0=1$ tells us its value at one point, not that its derivative is zero. Substituting the derivative into the log-probability expression yields

\[\boxed{ \left.\nabla_\delta\log\nu_\delta(a)\right|_{\delta=0} =\psi_\theta(a)-\mathbb E_q[\psi_\theta] =\widetilde\psi_{\theta,q}(a). }\]

The vertical bar means “evaluate the derivative at $\delta=0$.” This is exactly the centered score used by SC. The subtracted mean comes from differentiating the normalization term, not from a separate correction added by hand. This score identity is exact.

What the first-order expansion reveals. We can also see why normalization requires centering by expanding small changes. Let $|\delta|$ measure the size of the parameter increment. The notation $o(|\delta|)$ denotes a remainder whose magnitude divided by $|\delta|$ tends to zero as the increment tends to zero. Differentiability gives

\[\frac{p_{\theta+\delta}(a)}{p_\theta(a)} =1+\delta^\top\psi_\theta(a)+o(\|\delta\|).\]

The dot product $\delta^\top\psi_\theta(a)$ is the first-order relative change associated with this parameter move. Summing the reweighted sampler probabilities gives

\[Z_\delta =1+\delta^\top\mathbb E_q[\psi_\theta]+o(\|\delta\|).\]

Using the first-order identity $1/(1+x)=1-x+o(\lvert x\rvert)$ for the normalization factor, we obtain

\[\boxed{ \nu_\delta(a) =q(a)\left[ 1+\delta^\top\bigl(\psi_\theta(a)-\mathbb E_q[\psi_\theta]\bigr) \right]+o(\|\delta\|). }\]

Before normalization, the first-order changes sum to $\delta^\top\mathbb E_q[\psi_\theta]$, which need not be zero. Normalization removes this shared relative component. The centered changes sum to zero because

\[\sum_a q(a)\bigl(\psi_\theta(a)-\mathbb E_q[\psi_\theta]\bigr)=0.\]

Thus probability can move between actions without creating or destroying total probability mass. The first-order formula approximates a nearby distribution; the exact ratio-and-normalization definition guarantees valid probabilities.

The name trainer-tangent policy family refers to this local construction: the family starts at the sampler, while the trainer supplies its relative-change rule. For a fixed reward $R(a)$ at this fixed history,

\[\left.\nabla_\delta\mathbb E_{a\sim\nu_\delta}[R(a)]\right|_{\delta=0} =\mathbb E_{a\sim q} \left[R(a)\widetilde\psi_{\theta,q}(a)\right].\]

The right-hand side is the expected exact-SC update. This identifies the local objective of the auxiliary family; it does not turn it into the current trainer’s expected-reward gradient. The essential explanation is: sampler as the starting point, trainer-relative changes as the rule, and normalization as the source of score centering.

Yingru Li 的评论中的构造给出了理解 SC 的另一条路径:从 sampler 的概率出发,应用 trainer 的概率相对变化,再进行归一化。 对这个归一化后的分布求导,中心化项就会自然出现。我们先精确定义这个分布,再讨论近似。

哪些量在变,哪些量固定? 固定一个 history 或前缀 $h$。当前 trainer 是 $p_\theta(a\mid h)$,sampler 是 $q(a\mid h)$。把 $q$ 与参考参数向量 $\theta$ 保持固定,令 $\delta$ 表示一个候选参数增量;$p_{\theta+\delta}$ 表示 trainer 在这个增量下会变成什么。$\delta$ 与 $\theta$ 有相同的参数坐标;$\delta=0$ 表示没有增量,不是模型参数为零。为简化讨论,假设动作数有限、概率为正,且 trainer 策略在 $\theta$ 附近可微。

每选一个 $\delta$,我们就构造一个新概率分布 $\nu_\delta$。由不同 $\delta$ 对应的分布组成的集合,就是一个 policy family(策略族)。这是辅助性的数学构造;此时还没有实际更新任何一个模型。

先应用相对变化,再归一化。 比率

\[\frac{p_{\theta+\delta}(a\mid h)}{p_\theta(a\mid h)}\]

表示 trainer 把动作 $a$ 的概率乘了多少倍。比率为 $1.5$,表示概率提高 50%;比率为 $0.5$,表示概率减半。把同样的倍数应用到 sampler 的概率上,就得到 $q(a\mid h)p_{\theta+\delta}(a\mid h)/p_\theta(a\mid h)$。这些新数值非负,但加起来未必等于 1。

把所有这些数的总和定义为:

\[\boxed{ Z_\delta(h)=\sum_u q(u\mid h) \frac{p_{\theta+\delta}(u\mid h)}{p_\theta(u\mid h)}. }\]

这里 $u$ 只是遍历所有可能动作的求和下标。给定前缀和增量后,$Z_\delta(h)$ 是所有动作共享的一个数。它叫归一化常数,因为让每个数除以这个总和,就得到和为 1 的概率:

\[\boxed{ \nu_\delta(a\mid h)= \frac{q(a\mid h)\,\dfrac{p_{\theta+\delta}(a\mid h)}{p_\theta(a\mid h)}} {Z_\delta(h)}. }\]

“常数”指它对各个动作都一样,不是说它不随 $\delta$ 改变。所有分子的和恰好是 $Z_\delta(h)$,因此 $\sum_a\nu_\delta(a\mid h)=Z_\delta(h)/Z_\delta(h)=1$。

为什么 $\nu_0=q$? 在精确定义中代入 $\delta=0$。对每个动作:

\[\frac{p_{\theta+0}(a\mid h)}{p_\theta(a\mid h)} =\frac{p_\theta(a\mid h)}{p_\theta(a\mid h)}=1.\]

因此:

\[Z_0(h)=\sum_u q(u\mid h)\times1=1, \qquad \boxed{\nu_0(a\mid h)=\frac{q(a\mid h)\times1}{1}=q(a\mid h).}\]

没有施加任何相对变化,所以得到的就是 sampler 本身。这里不要求 $q=p_\theta$。两个角色必须分开:起点的概率分布是 $q$;如何相对改变概率的规则来自当前 trainer。

对归一化后的策略族求导。 继续固定 history,下面省略 $h$。记 $\psi_\theta(a)=\nabla_\theta\log p_\theta(a)$,即前文写作 $s_p(a)$ 的 trainer score。对精确定义取对数:

\[\log\nu_\delta(a) =\log q(a)+\log p_{\theta+\delta}(a) -\log p_\theta(a)-\log Z_\delta.\]

现在对增量 $\delta$ 求导。$\log q(a)$ 和 $\log p_\theta(a)$ 固定,导数为零;分子中的 $\log p_{\theta+\delta}(a)$ 则随 $\theta+\delta$ 改变,在 $\delta=0$ 处求值,得到 $\psi_\theta(a)$。

剩下需要计算归一化项。先对有限求和 $Z_\delta$ 求导:

\[\begin{aligned} \left.\nabla_\delta Z_\delta\right|_{\delta=0} &=\sum_u\frac{q(u)}{p_\theta(u)} \left.\nabla_\delta p_{\theta+\delta}(u)\right|_{\delta=0}\\ &=\sum_u\frac{q(u)}{p_\theta(u)}\nabla_\theta p_\theta(u)\\ &=\sum_u q(u)\psi_\theta(u) =\mathbb E_{u\sim q}[\psi_\theta(u)]. \end{aligned}\]

第三行使用 $\nabla_\theta p_\theta=p_\theta\nabla_\theta\log p_\theta$。

明明对增量求导,为什么变成了参数梯度?何时代入零?(点击展开)

这里没有中途更换求导变量。 参考参数 $\theta$ 保持固定,增量 $\delta$ 可以变化。为了区分“求导变量”和“导数求值的位置”,另用一个参数变量 $w$,并对固定动作 $u$ 定义:

\[f(w)=\log p_w(u),\qquad w=\theta+\delta.\]

原来的表达式就变成了 $f(\theta+\delta)$。当某个坐标 $\delta_i$ 增加一点,$w_i=\theta_i+\delta_i$ 也增加同样的一点,其他坐标不变。因此,由链式法则:

\[\nabla_\delta f(\theta+\delta) =\left.\nabla_w f(w)\right|_{w=\theta+\delta}.\]

左边从“增量”的角度描述变化率,右边从“实际参数”的角度描述变化率。两者相同,是因为加上固定的 $\theta$ 只是平移了参数坐标的原点。

先求导,再代入 $\delta=0$。 长竖线表示后面这一步求值操作。增量为零时,实际参数位置就是 $w=\theta$,所以:

\[\boxed{ \left.\nabla_\delta\log p_{\theta+\delta}(u)\right|_{\delta=0} =\left.\nabla_w\log p_w(u)\right|_{w=\theta} =\psi_\theta(u). }\]

中间这一项通常简写成 $\nabla_\theta\log p_\theta(u)$:先求 log-probability 对模型参数的梯度,再在当前参数值处求值。它不是让我们改为对整个 $Z_\delta$ 中的参考参数 $\theta$ 求导。在原来的计算中,分母 $p_\theta(u)$ 和 $q(u)$ 始终固定。

用一个与概率无关的标量函数说明顺序。固定参考值 $\theta=3$,取 $f(w)=w^2$,则:

\[\left.\frac{d}{d\delta}(3+\delta)^2\right|_{\delta=0} =\left.2(3+\delta)\right|_{\delta=0} =6 =\left.\frac{d}{dw}w^2\right|_{w=3}.\]

如果一开始就代入零,只会剩下常数 $9$;再对这个常数求导得到零,不是我们需要的斜率 $6$。这样丢掉了原函数在这个位置附近如何变化的信息。函数在一点的取值,不是它在那一点的导数。

再回到归一化因子。 求导时先保留 $\delta$,利用 $\nabla p=p\nabla\log p$,得到:

\[\nabla_\delta Z_\delta =\sum_u q(u)\frac{p_{\theta+\delta}(u)}{p_\theta(u)} \nabla_\delta\log p_{\theta+\delta}(u).\]

这里必须保留概率比。现在再代入 $\delta=0$,这个比率才变成 1;剩下的导数,就是在 $w=\theta$ 处求值的参数梯度:

\[\left.\nabla_\delta Z_\delta\right|_{\delta=0} =\mathbb E_{u\sim q}\!\left[ \left.\nabla_w\log p_w(u)\right|_{w=\theta} \right] =\mathbb E_{u\sim q}[\psi_\theta(u)].\]

这个期望已经是我们要的结果:按照固定 sampler $q$,对 trainer 的 score 取平均。它不一定为零,因为取平均所用的分布不一定是 $p_\theta$。

对数的导数等于“里面的量的导数,除以这个量”;再利用 $Z_0=1$,得到:

\[\left.\nabla_\delta\log Z_\delta\right|_{\delta=0} =\frac{1}{Z_0} \left.\nabla_\delta Z_\delta\right|_{\delta=0} =\mathbb E_q[\psi_\theta].\]

$Z_0=1$ 只说明它在一个点的取值,不意味着它的导数为零。把这个结果代回 log-probability 的导数:

\[\boxed{ \left.\nabla_\delta\log\nu_\delta(a)\right|_{\delta=0} =\psi_\theta(a)-\mathbb E_q[\psi_\theta] =\widetilde\psi_{\theta,q}(a). }\]

长竖线表示“求导之后,在 $\delta=0$ 处求值”。这恰好是 SC 使用的 centered score。减掉的均值来自归一化项的导数,不是另外人为塞进去的一项修正。 这个 score 恒等式是精确的。

再用一阶展开看中心化为什么必要。 对较小的增量,还可以从概率变化本身看到这个过程。$|\delta|$ 表示参数增量的大小;$o(|\delta|)$ 表示一个余项,它除以 $|\delta|$ 后,会随增量趋近零而趋近零。由可微性:

\[\frac{p_{\theta+\delta}(a)}{p_\theta(a)} =1+\delta^\top\psi_\theta(a)+o(\|\delta\|).\]

点积 $\delta^\top\psi_\theta(a)$ 表示沿这次参数移动产生的一阶相对变化。将重新加权的 sampler 概率求和:

\[Z_\delta =1+\delta^\top\mathbb E_q[\psi_\theta]+o(\|\delta\|).\]

对归一化因子使用一阶恒等式 $1/(1+x)=1-x+o(\lvert x\rvert)$,得到:

\[\boxed{ \nu_\delta(a) =q(a)\left[ 1+\delta^\top\bigl(\psi_\theta(a)-\mathbb E_q[\psi_\theta]\bigr) \right]+o(\|\delta\|). }\]

归一化之前,各动作概率的一阶变化加起来是 $\delta^\top\mathbb E_q[\psi_\theta]$,未必为零。归一化扣掉这部分共同的相对变化;centered changes 加起来为零,因为:

\[\sum_a q(a)\bigl(\psi_\theta(a)-\mathbb E_q[\psi_\theta]\bigr)=0.\]

于是概率可以在动作之间转移,却不会凭空增加或减少总概率质量。一阶公式近似描述附近的分布;保证概率合法的是前面精确的“概率比率加归一化”定义。

Trainer-tangent policy family 指的就是这个局部构造:策略族从 sampler 出发,trainer 提供相对变化规则。在这个固定 history 下,对不随参数改变的动作奖励 $R(a)$,有:

\[\left.\nabla_\delta\mathbb E_{a\sim\nu_\delta}[R(a)]\right|_{\delta=0} =\mathbb E_{a\sim q} \left[R(a)\widetilde\psi_{\theta,q}(a)\right].\]

右侧就是精确 SC 的期望更新。这明确了辅助策略族的局部目标,并不把它变成当前 trainer 的期望奖励梯度。核心解释是:以 sampler 为起点,使用 trainer 的相对概率变化,而归一化自然产生 score centering。