Direct Preference Optimization: Your Language Model is Secretly a Reward Model
文章标题:直接偏好优化:你的语言模型秘密地是一个奖励模型 作者/机构:Rafael Rafailov, Archit Sharma, Eric Mitchell, Stefano Ermon, Christopher D. Manning, Chelsea Finn (Stanford University, CZ Biohub)
我们回顾了Ziegler等人【索引51,Fine-tuning language models from human preferences,2020】(以及后来的【索引40,Learning to summarize from human feedback,2022】,【索引1,Training a helpful and harmless assistant with reinforcement learning from human feedback,2022】,【索引28,Training language models to follow instructions with human feedback,2022】)中的RLHF流程。它通常包括三个阶段:1)监督微调(SFT);2)偏好采样和奖励学习;3)强化学习优化。
奖励建模阶段:在第二阶段,SFT模型被提示(prompt)$x$ 以产生一对答案 $(y_1, y_2) \sim \pi^{SFT}(y|x)$。然后将这些答案呈现给人类标注者,他们表达对其中一个答案的偏好,记为 $y_w \succ y_l | x$,其中 $y_w$ 和 $y_l$ 分别表示 $(y_1, y_2)$ 中受偏好的和不受偏好的完成。这些偏好被假定是由某个我们无法访问的潜在奖励模型 $r^*(y, x)$ 生成的。有多种方法用于建模偏好,Bradley-Terry (BT) 模型【索引5,Rank analysis of incomplete block designs: I. the method of paired comparisons. Biometrika, 1952】是一个流行的选择(尽管更通用的Plackett-Luce排名模型【索引32,The analysis of permutations. Journal of the Royal Statistical Society. Series C (Applied Statistics), 1975】,【索引23,Individual choice behavior: A theoretical analysis. Courier Corporation, 2012】如果我们可以访问多个排名答案,也与该框架兼容)。BT模型规定人类偏好分布 $p^*$可以写成: 公式1
奖励模型训练:假设可以访问从 $p^*$ 中采样的静态比较数据集 $\mathcal{D} = \{x^{(i)}, y_w^{(i)}, y_l^{(i)}\}_{i=1}^N$,我们将问题构建为二元分类,其负对数似然损失为: 公式2
其中 $\sigma$ 是 logistic 函数。在LM的背景下,网络 $r_\phi(x, y)$ 通常从SFT模型 $\pi^{SFT}(y|x)$ 初始化,并在最后的transformer层之上增加一个线性层,该层产生奖励值的单个标量预测【索引51,Fine-tuning language models from human preferences,2020】。为了确保奖励函数的方差较低,先前的工作对奖励进行归一化,使得对于所有 $x$,$\mathbb{E}_{x,y \sim \mathcal{D}}[r_\phi(x, y)] = 0$。
强化学习微调阶段:在RL阶段,学习到的奖励函数用于向语言模型提供反馈。遵循先前的工作【索引17,Sequence tutor: Conservative fine-tuning of sequence generation models with kl-control. International Conference on Machine Learning, 2017】,【索引18,Human-centric dialog training via offline reinforcement learning. arXiv preprint arXiv:2010.05848, 2020】,优化问题被表述为: 公式3
其中 $\beta$ 是一个控制与基础参考策略 $\pi_{ref}$(即初始SFT模型 $\pi^{SFT}$)偏离程度的参数。在实践中,语言模型策略 $\pi_\theta$ 也被初始化为 $\pi^{SFT}$。增加的约束很重要,因为它防止模型偏离奖励模型准确的分布太远,同时保持生成多样性并防止模式崩溃到单个高奖励答案。由于语言生成的离散性,该目标不可微,通常使用强化学习进行优化。标准方法【索引51,Fine-tuning language models from human preferences,2020】,【索引40,Learning to summarize from human feedback,2022】,【索引1,Training a helpful and harmless assistant with reinforcement learning from human feedback,2022】,【索引28,Training language models to follow instructions with human feedback,2022】是构建奖励函数 $r(x, y) = r_\phi(x, y) - \beta(\log \pi_\theta(y | x) - \log \pi_{ref}(y | x))$,并使用PPO【索引39,Proximal policy optimization algorithms, 2017】进行最大化。
推导DPO目标:我们从与先前工作相同的RL目标(公式3)开始,考虑一个通用的奖励函数 $r$。遵循先前的工作【索引31,Reinforcement learning by reward-weighted regression for operational space control. In Proceedings of the 24th international conference on Machine learning, 2007】,【索引30,Advantage-weighted regression: Simple and scalable off-policy reinforcement learning. arXiv preprint arXiv:1910.00177, 2019】,【索引19,On reinforcement learning and distribution matching for fine-tuning language models with no catastrophic forgetting. In Advances in Neural Information Processing Systems, 2022】,【索引15,Aligning language models with preferences through f-divergence minimization. In Proceedings of the 40th International Conference on Machine Learning, 2023】,可以很简单地证明,KL约束的奖励最大化目标(公式3)的最优解形式如下: 公式4
其中 $Z(x) = \sum_y \pi_{ref}(y|x) \exp(\frac{1}{\beta} r(x, y))$ 是配分函数。完整的推导见附录A.1。即使我们使用真实奖励函数 $r^*$ 的最大似然估计 $r_\phi$,估计配分函数 $Z(x)$ 仍然是昂贵的【索引19,On reinforcement learning and distribution matching for fine-tuning language models with no catastrophic forgetting. In Advances in Neural Information Processing Systems, 2022】,【索引15,Aligning language models with preferences through f-divergence minimization. In Proceedings of the 40th International Conference on Machine Learning, 2023】,这使得该表示在实践中难以利用。然而,我们可以重新排列公式4,用其对应的最优策略 $\pi_r$、参考策略 $\pi_{ref}$ 和未知的配分函数 $Z(\cdot)$ 来表示奖励函数。具体来说,我们首先对公式4的两边取对数,然后通过一些代数运算得到: 公式5
将重参数化应用于偏好模型:我们可以将此重参数化应用于真实奖励 $r^*$ 及其对应的最优模型 $\pi^*$。幸运的是,Bradley-Terry模型仅依赖于两个完成之间奖励的差异,即 $p^*(y_1 \succ y_2|x) = \sigma(r^*(x, y_1) - r^*(x, y_2))$。将公式5中 $r^*(x, y)$ 的重参数化代入偏好模型公式1,配分函数被消去,我们可以仅用最优策略 $\pi^*$ 和参考策略 $\pi_{ref}$ 来表示人类偏好概率。因此,在Bradley-Terry模型下,最优的RLHF策略 $\pi^*$ 满足偏好模型: 公式6
推导过程在附录A.2中。虽然公式6使用了Bradley-Terry模型,我们也可以在更通用的Plackett-Luce模型【索引32,The analysis of permutations. Journal of the Royal Statistical Society. Series C (Applied Statistics), 1975】,【索引23,Individual choice behavior: A theoretical analysis. Courier Corporation, 2012】下推导出类似的表达式,如附录A.3所示。
DPO的损失函数:现在我们已经用最优策略而不是奖励模型来表示人类偏好数据的概率,我们可以为参数化策略 $\pi_\theta$ 构建一个最大似然目标。类似于奖励建模方法(即公式2),我们的策略目标变为: 公式7
通过这种方式,我们使用一种替代的参数化方法来拟合一个隐式奖励,其最优策略就是 $\pi_\theta$。此外,由于我们的过程等同于拟合一个重参数化的Bradley-Terry模型,它享有某些理论特性,例如在偏好数据分布的适当假设下的收敛性【索引4,Generalized results for the existence and consistency of the MLE in the Bradley-Terry-Luce model. International Conference on Machine Learning, 2022】。在第5节中,我们进一步讨论DPO与其他工作相关的理论特性。
引理的证明和意义:证明是直接的,我们将其放在附录A.5中。第一个引理是Plackett-Luce模型族中一个众所周知的欠定问题【索引32,The analysis of permutations. Journal of the Royal Statistical Society. Series C (Applied Statistics), 1975】。由于这种欠定性,我们通常必须施加额外的可识别性约束,才能对公式2的最大似然估计获得任何保证【索引4,Generalized results for the existence and consistency of the MLE in the Bradley-Terry-Luce model. International Conference on Machine Learning, 2022】。第二个引理指出,同一等价类中的所有奖励函数都会产生相同的最优策略,因此对于我们的最终目标,我们只关心从最优等价类中恢复任意一个奖励函数。
从控制即推断视角分析RLHF:我们也可以使用我们的框架来诊断用于RLHF的标准行动者-评论家算法(如PPO)的不稳定性。我们遵循RLHF流程,并关注第3节中概述的RL微调步骤。我们可以将其与约束RL问题的控制即推断框架【索引22,Reinforcement learning and control as probabilistic inference: Tutorial and review, 2018】联系起来。我们假设一个参数化模型 $\pi_\theta(y|x)$ 并最小化 $D_{KL}[\pi_\theta(y|x) || \pi^*(y|x)]$,其中 $\pi^*$ 是由奖励函数 $r_\phi(y, x)$ 导出的公式7中的最优策略。经过一些代数运算,这导致了优化目标: 公式11
DPO与传统RLHF方法的对比:这是先前工作【索引51,Fine-tuning language models from human preferences,2020】,【索引40,Learning to summarize from human feedback,2022】,【索引1,Training a helpful and harmless assistant with reinforcement learning from human feedback,2022】,【索引28,Training language models to follow instructions with human feedback,2022】中使用与DPO等价的 $r_\phi$ 奖励类别的奖励所优化的相同目标。在这种情况下,我们可以将 $f(r_\phi, \pi_{ref}, \beta)$ 中的归一化项解释为参考策略 $\pi_{ref}$ 的软价值函数。虽然该项不影响最优解,但没有它,该目标的策略梯度可能会有高方差,使学习不稳定。我们可以使用学习到的价值函数来处理归一化项,但这也很难优化。另外,先前的工作使用人类完成基线来归一化奖励,这本质上是归一化项的单样本蒙特卡洛估计。相比之下,DPO重参数化产生了一个不需要任何基线的奖励函数。
A4 实验环境
任务与数据集:
受控情感生成: 使用IMDb数据集【索引24,Learning word vectors for sentiment analysis. In Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies, 2011】的电影评论前缀作为提示 $x$,策略需要生成具有积极情感的文本 $y$。使用预训练的情感分类器生成偏好对。
摘要: 使用Reddit TL;DR摘要数据集【索引43,TL;DR: Mining Reddit to learn automatic summarization. In Proceedings of the Workshop on New Frontiers in Summarization, 2017】,其中 $x$ 是Reddit论坛帖子,策略需生成摘要 $y$。使用Stiennon等人【索引40,Learning to summarize from human feedback,2022】收集的人类偏好数据。
单轮对话: 使用Anthropic的“有用与无害”(Helpful and Harmless)对话数据集【索引1,Training a helpful and harmless assistant with reinforcement learning from human feedback,2022】,包含17万次人机对话。策略需对用户查询 $x$ 生成有吸引力且有帮助的响应 $y$。
模型架构:
情感生成: 使用gpt2-large作为基础模型。SFT模型在IMDb训练集上微调。
摘要: 使用在人类编写的摘要上微调过的GPT-J SFT模型。
对话: 使用预训练的Pythia-2.8B【索引3,Pythia: A suite for analyzing large language models across training and scaling, 2023】模型。由于没有现成的SFT模型,通过在偏好数据中的首选完成上进行微调来创建SFT模型。
Plackett-Luce模型:Plackett-Luce模型【索引32,The analysis of permutations. Journal of the Royal Statistical Society. Series C (Applied Statistics), 1975】,【索引23,Individual choice behavior: A theoretical analysis. Courier Corporation, 2012】是Bradley-Terry模型在排名(而不仅仅是成对比较)上的推广。它规定,当面对一组K个答案 $y_1, ..., y_K$ 时,用户会输出一个排列 $\tau$,给出他们对答案的排名。该模型规定: 公式22
当 $K=2$ 时,该公式简化为Bradley-Terry模型。对于通用的Plackett-Luce模型,我们仍然可以利用公式5的结果,并用其最优策略参数化的奖励函数进行替换。与附录A.2类似,归一化常数 $Z(x)$ 被消去,我们剩下: 公式23