基于人类反馈的强化学习

面向语言模型的后训练RLHF简明指南

作者:Nathan Lambert 译者:Junwei He

本章目录

奖励建模

奖励模型(Reward Model)是现代RLHF方法的核心组成部分。 奖励模型在强化学习领域被广泛用作环境奖励的代理 [1]。 这与逆向强化学习(Inverse Reinforcement Learning)密切相关,即通过智能体的行为轨迹来近似其奖励函数 [2],以及其他深度强化学习方向。 奖励模型的现代形式,最早被提出用于研究价值对齐(value alignment)问题 [3]。

常见的偏好奖励模型为 prompt 与回答输出一个标量分数。Bradley-Terry 模型将两个回答的分数差转换为偏好概率;单个分数既不是文本相似度,也不是概率。 本节后面还会介绍Outcome Reward Model(ORM,结果奖励模型,预测补全是否正确)和Process Reward Model(PRM,过程奖励模型,为推理过程中的每一步打分)。 如无特殊说明,本文提到的奖励模型均指预测文本偏好的模型。

奖励模型的训练

训练RLHF标准奖励模型有两种主流表达方式——它们在数值上是等价的。 规范做法源自Bradley-Terry偏好模型 [4]。 Bradley-Terry模型用于衡量同一分布下两个事件(如\(i\)和\(j\))的成对比较满足\(i > j\)的概率:

\[P(i > j) = \frac{p_i}{p_i + p_j}\qquad{(1)}\]

要训练奖励模型,需要设计一个损失函数,使模型输出满足上述关系。 首先,将语言模型转为输出标量值的模型,该标量可以是未归一化的 logit,而不是概率。 对于同一prompt下的两个补全\(y_1\)和\(y_2\),用奖励模型\(r_\theta\)分别打分。

奖励模型在成对比较下的“成功概率”可写为:

\[P(y_1 > y_2) = \frac{\exp(r(y_1))}{\exp(r(y_1)) + \exp(r(y_2))}\qquad{(2)}\]

最大化上述函数的对数似然(或等价地,最小化负对数似然),即可得到奖励模型的训练损失:

\[ \begin{aligned} \theta^* = \arg\max_\theta P(y_w > y_l) &= \arg\max_\theta \frac{\exp(r_\theta(y_w))}{\exp(r_\theta(y_w)) + \exp(r_\theta(y_l))} \\ &= \arg\max_\theta \frac{1}{1 + \exp(-(r_\theta(y_w) - r_\theta(y_l)))} \\ &= \arg\max_\theta \sigma \left( r_\theta(y_w) - r_\theta(y_l) \right) \\ &= \arg\min_\theta - \log \left( \sigma \left(r_\theta(y_w) - r_\theta(y_l)\right) \right) \end{aligned} \qquad{(3)}\]

常见的两种写法如下:

模型结构

一种常见实现是使用 AutoModelForSequenceClassification 并设置 num_labels=1:在主干模型的序列表示上添加线性评分头,每个回答独立输出一个标量。具体使用哪个 token 的表示取决于模型的池化实现。

推理时先获得分数 \(r(x,y)\);对同一 prompt 的两个回答,再用 \(\sigma(r(x,y_1)-r(x,y_2))\) 估计第一个回答更受偏好的概率。不要把单个 logit 当作“被选中概率”。

实现示例

奖励模型损失的实现非常简单。 更多的工程挑战在于设置独立的数据加载器和推理流程。 只要数据加载器正确,损失函数可这样写:

import torch.nn as nn
rewards_chosen = model(**inputs_chosen).logits.squeeze(-1)
rewards_rejected = model(**inputs_rejected).logits.squeeze(-1)

loss = -nn.functional.logsigmoid(rewards_chosen - rewards_rejected).mean()

一些早期工作只训练 1 个 epoch 以减轻过拟合;训练轮数仍应根据数据规模和验证集表现决定。

变体

奖励建模仍是RLHF领域相对探索较少的部分。 许多流行工作对传统奖励模型损失做了修改,但尚未形成统一最佳实践。

偏好间隔损失(Preference Margin Loss)

如果标注员给出的是Likert量表上的分数或排序,可以利用这些关系的幅度信息进行训练。 常见做法是将数据二值化(隐含1/0),但利用更多信息有助于提升模型训练效果。 Llama 2提出用两个数据点间的间隔\(m(r)\)来区分偏好强度:

\[\mathcal{L}(\theta) = - \log \left( \sigma \left( r_{\theta}(x, y_w) - r_{\theta}(x, y_l) - m(r) \right) \right)\qquad{(6)}\]

需要注意的是,Llama 3中去除了margin项,因为团队发现随着规模扩大,收益递减。

单prompt多比较的平衡

InstructGPT研究了每个prompt用不同数量补全进行训练的影响,并在奖励模型训练时做了平衡 [5]。 做法是按prompt对每个比较加权更新损失。 同一 prompt 的比较应共同参与损失计算;当各 prompt 的回答数不同,还需显式做 prompt 内归一化,不能仅靠放进同一 batch 来保证权重平衡。以下假设每组 \(K\) 个回答已按偏好从高到低排列: 损失函数如下:

\[\mathcal{L}(\theta) = -\mathbb{E}_{(x,y_{1:K})\sim D}\left[\frac{1}{\binom{K}{2}}\sum_{i<j}\log\sigma\left(r_\theta(x,y_i)-r_\theta(x,y_j)\right)\right]\qquad{(7)}\]

K-wise损失函数

还有很多其他形式可以用于RLHF中的人类偏好建模。 比如Starling 7B和34B等早期RLHF模型 [7],采用了基于Plackett-Luce模型的K-wise损失函数 [8]。

Zhu等(2023)[9]形式化如下: 对于某个prompt或状态\(s^i\),采样\(K\)个动作\((a_0^i, a_1^i, \cdots, a_{K-1}^i)\),然后由标注员给出排序\(\sigma^i: [K] \mapsto [K]\),其中\(\sigma^i(0)\)为最优动作。 概率建模如下:

\[P(\sigma^i|s^i,a_0^i,a_1^i,\ldots,a_{K-1}^i) = \prod_{k=0}^{K-1} \frac{\exp(r_{\theta\star}(s^i,a_{\sigma^i(k)}^i))}{\sum_{j=k}^{K-1}\exp(r_{\theta\star}(s^i,a_{\sigma^i(j)}^i))}\qquad{(8)}\]

当\(K=2\)时,退化为成对Bradley-Terry模型。 训练完成后,这类模型在RLHF训练中用法与其他奖励模型类似。

结果奖励模型(Outcome Reward Models, ORM)

大多数语言模型及AI系统的偏好微调都采用上述Bradley-Terry模型。 对于推理密集型任务,则可以采用Outcome Reward Model(ORM,结果奖励模型)。 ORM 使用回答是否正确的结果标签,样本可表示为 \((x,y,z)\),其中 \(z\in\{0,1\}\)。这不要求每个正确回答必须与一个错误回答配对。

模型结构与标准奖励模型类似,都是在主模型顶部加一个线性层输出logit(RM),但ORM的训练目标略有不同 [10]:

[我们]用联合目标训练验证器,让模型不仅学习原有的语言建模目标,还要学会判断补全是否正确。
架构上,验证器是语言模型,在最后的unembedding层加一个小的标量head,对每个token输出预测。
这个head仅用一个偏置参数和一个增益参数作用于语言模型输出的logit。

该工作将结果标签用于 token 位置上的辅助预测;这是一种实现方式。ORM 的定义取决于结果级监督,而不要求逐 token 输出,也不要求输出一个分类 token。 形式化地,参考[11]:

\[\mathcal{L}_{\text{CE}} = -\mathbb{E}_{(s,r)\sim \mathcal{D}}[r\log p_\theta(s) + (1-r)\log(1-p_\theta(s))]\qquad{(9)}\]

其中\(r \in \{0,1\}\)为二元标签,1代表正确答案,0代表错误,\(p_\theta(s)\)为模型预测正确概率。

这类模型仍在使用,但在开源RLHF工具中支持较少。 比如Let’s Verify Step by Step [12]用的也是类似ORM,但没有用语言建模损失。 最终损失是每个token的交叉熵,判断最终答案是否正确。

由于支持有限,Outcome Reward Model(ORM)一词在不同文献中定义略有差异。 有些文献(如[11])沿用Cobbe等2021年定义,其他文献则不同。

过程奖励模型(Process Reward Models, PRM)

过程奖励模型(Process Reward Models, PRMs),最初称为过程监督奖励模型(Process-supervised Reward Models),用于在推理链条的每一步输出分数。 PRM 的监督目标是各个推理步骤,ORM 的监督目标是整个回答。步骤标签常放在步骤末尾的分隔 token 上,其余位置忽略损失;输出位置与评分头的具体结构并不是这些模型类别的定义。

以下为HuggingFace TRL [13]中每步标签的打包示例:

# 获取分隔符token的ID并加入补全序列
separator_ids = tokenizer.encode(step_separator, add_special_tokens=False)
completions_ids = [completion + separator_ids for completion in completions_ids]

# 创建标签
labels = [[-100] * (len(completion) - 1) + [label] for completion, label in zip(completions_ids, labels)]

传统上,PRM用语言建模head在每个推理步骤结束时输出token(如遇到双换行或特殊token)。 标签可以是错误、中性、正确三类,也可以是二元标签;分类器输出的是相应类别的分数或概率。 这些标签不一定表示模型是否“走在正确道路上”,而是该步是否正确。

奖励模型 vs. 结果RM vs. 过程RM vs. 价值函数

上述各种奖励模型展示了RLHF及后训练中衡量“质量”的多种方式。 下表总结了各类模型的预测内容、训练方式及结构。

表 1: 奖励模型类型对比。
模型类别 预测内容 训练方式 语言模型结构
偏好奖励模型 回答的相对偏好分数 成对或多元比较 序列表示与标量评分头
结果奖励模型 回答正确性 结果级标签 分类头或生成式评分等
过程奖励模型 各步骤的正确性或质量 步骤级标签 步骤位置上的评分或分类头等
价值函数 当前策略下的期望回报 回报目标或自举目标 状态表示与标量回归头

补充说明:

生成式奖励建模

由于偏好数据昂贵,研究者开始探索用现有大语言模型(LLM)充当“裁判”以评判人类偏好或用于评测 [14]。 核心思想是:用prompt让LLM作为公正裁判,给出评判指令、问题和两个补全(类似人工标注流程)。 MT-Bench [14] 的评测prompt如下:

[System]
Please act as an impartial judge and evaluate the quality of the responses provided by two
AI assistants to the user question displayed below. You should choose the assistant that
follows the user’s instructions and answers the user’s question better. Your evaluation
should consider factors such as the helpfulness, relevance, accuracy, depth, creativity,
and level of detail of their responses. Begin your evaluation by comparing the two
responses and provide a short explanation. Avoid any position biases and ensure that the
order in which the responses were presented does not influence your decision. Do not allow
the length of the responses to influence your evaluation. Do not favor certain names of
the assistants. Be as objective as possible. After providing your explanation, output your
final verdict by strictly following this format: "[[A]]" if assistant A is better, "[[B]]"
if assistant B is better, and "[[C]]" for a tie.
[User Question]
{question}
[The Start of Assistant A’s Answer]
{answer_a}
[The End of Assistant A’s Answer]
[The Start of Assistant B’s Answer]
{answer_b}
[The End of Assistant B’s Answer]

由于LLM裁判在评测中的高效,催生了大量基于LLM的评测方法,如AlpacaEval [15]、Arena-Hard [16]、WildBench [17]等,许多团队甚至直接用LLM裁判替代奖励模型生成和利用偏好数据。

“生成式奖励模型”(Generative Reward Models)也成为活跃研究领域 [18] [19] [20](包括专门训练为“裁判”的模型 [21]),但在奖励模型评测上,LLM裁判通常不如专业奖励模型,说明奖励建模仍是当前RLHF的重要技术。

一个常用技巧是将LLM裁判的采样温度设为0,以减少评分的随机性。

延伸阅读

奖励建模的学术文献在2024年逐步成熟。 早期进展主要集中在建立基准和识别行为模式。 首个奖励模型基准RewardBench为奖励模型测试提供了通用基础设施 [22]。 此后,奖励模型评测扩展到类似通用后训练模型的多种评测,包括已知答案的准确性评测 [22],以及LLM裁判或与其他基准相关的“体验型”评测 [23]。

新基准示例包括多语种RewardBench(M-RewardBench)[24]、RAG-RewardBench [25]、RMB [26]、RM-Bench [27](通用对话)、ReWordBench(拼写错误)[28]、MJ-Bench [29]、多模态RewardBench [30]、VL RewardBench [31]、VLRMBench [32](视觉语言模型)、Preference Proxy Evaluations [33]、RewardMATH [34]等。 过程奖励模型(PRM)也有自己的新基准,如PRM Bench [35]、视觉类VisualProcessBench [36]和ViLBench [37]。

想了解奖励模型训练的最新进展,可查阅相关新方法,如面向特定方面的奖励模型 [38]、高质量人类数据集 [39] [40]、大规模训练 [41]、大规模实验 [42]、数据去偏 [43]等。

参考文献

[1]
R. S. Sutton, 《Reinforcement learning: An introduction》, A Bradford Book, 2018.
[2]
A. Y. Ng, S. Russell, 等, 《Algorithms for inverse reinforcement learning.》, 收入 Proceedings of the Seventeenth International Conference on Machine Learning, 收入 ICML ’00. 2000, 页 663--670.
[3]
J. Leike, D. Krueger, T. Everitt, M. Martic, V. Maini, 和 S. Legg, 《Scalable agent alignment via reward modeling: a research direction》, arXiv preprint arXiv:1811.07871, 2018.
[4]
R. A. Bradley 和 M. E. Terry, 《Rank Analysis of Incomplete Block Designs: I. The Method of Paired Comparisons》, Biometrika, 卷 39, 期 3/4, 页 324~345, 1952, 见于: 2023年2月13日. [在线]. 载于: http://www.jstor.org/stable/2334029
[5]
L. Ouyang 等, 《Training language models to follow instructions with human feedback》, Advances in neural information processing systems, 卷 35, 页 27730~27744, 2022.
[6]
A. Askell 等, 《A general language assistant as a laboratory for alignment》, arXiv preprint arXiv:2112.00861, 2021.
[7]
B. Zhu 等, 《Starling-7b: Improving helpfulness and harmlessness with rlaif》, 收入 First Conference on Language Modeling, 2024.
[8]
A. Liu, Z. Zhao, C. Liao, P. Lu, 和 L. Xia, 《Learning plackett-luce mixtures from partial preferences》, 收入 Proceedings of the AAAI Conference on Artificial Intelligence, 2019, 页 4328~4335.
[9]
B. Zhu, M. Jordan, 和 J. Jiao, 《Principled reinforcement learning with human feedback from pairwise or k-wise comparisons》, 收入 International Conference on Machine Learning, PMLR, 2023, 页 43037~43067.
[10]
K. Cobbe 等, 《Training verifiers to solve math word problems》, arXiv preprint arXiv:2110.14168, 2021.
[11]
C. Lyu 等, 《Exploring the Limit of Outcome Reward for Learning Mathematical Reasoning》, arXiv preprint arXiv:2502.06781, 2025.
[12]
H. Lightman 等, 《Let’s verify step by step》, arXiv preprint arXiv:2305.20050, 2023.
[13]
L. von Werra 等, 《TRL: Transformer Reinforcement Learning》, GitHub repository. https://github.com/huggingface/trl; GitHub, 2020年.
[14]
L. Zheng 等, 《Judging llm-as-a-judge with mt-bench and chatbot arena》, Advances in Neural Information Processing Systems, 卷 36, 页 46595~46623, 2023.
[15]
Y. Dubois, B. Galambosi, P. Liang, 和 T. B. Hashimoto, 《Length-controlled alpacaeval: A simple way to debias automatic evaluators》, arXiv preprint arXiv:2404.04475, 2024.
[16]
T. Li 等, 《From Crowdsourced Data to High-Quality Benchmarks: Arena-Hard and BenchBuilder Pipeline》, arXiv preprint arXiv:2406.11939, 2024.
[17]
B. Y. Lin 等, 《WILDBENCH: Benchmarking LLMs with Challenging Tasks from Real Users in the Wild》, arXiv preprint arXiv:2406.04770, 2024.
[18]
D. Mahan 等, 《Generative Reward Models》, 2024, 载于: https://www.synthlabs.ai/pdf/Generative_Reward_Models.pdf
[19]
L. Zhang, A. Hosseini, H. Bansal, M. Kazemi, A. Kumar, 和 R. Agarwal, 《Generative verifiers: Reward modeling as next-token prediction》, arXiv preprint arXiv:2408.15240, 2024.
[20]
Z. Ankner, M. Paul, B. Cui, J. D. Chang, 和 P. Ammanabrolu, 《Critique-out-loud reward models》, arXiv preprint arXiv:2408.11791, 2024.
[21]
S. Kim 等, 《Prometheus: Inducing fine-grained evaluation capability in language models》, 收入 The Twelfth International Conference on Learning Representations, 2023.
[22]
N. Lambert 等, 《Rewardbench: Evaluating reward models for language modeling》, arXiv preprint arXiv:2403.13787, 2024.
[23]
X. Wen 等, 《Rethinking Reward Model Evaluation: Are We Barking up the Wrong Tree?》, arXiv preprint arXiv:2410.05584, 2024.
[24]
S. Gureja 等, 《M-RewardBench: Evaluating Reward Models in Multilingual Settings》, arXiv preprint arXiv:2410.15522, 2024.
[25]
Z. Jin 等, 《RAG-RewardBench: Benchmarking Reward Models in Retrieval Augmented Generation for Preference Alignment》, arXiv preprint arXiv:2412.13746, 2024.
[26]
E. Zhou 等, 《RMB: Comprehensively Benchmarking Reward Models in LLM Alignment》, arXiv preprint arXiv:2410.09893, 2024.
[27]
Y. Liu, Z. Yao, R. Min, Y. Cao, L. Hou, 和 J. Li, 《RM-bench: Benchmarking reward models of language models with subtlety and style》, arXiv preprint arXiv:2410.16184, 2024.
[28]
Z. Wu, M. Yasunaga, A. Cohen, Y. Kim, A. Celikyilmaz, 和 M. Ghazvininejad, 《reWordBench: Benchmarking and Improving the Robustness of Reward Models with Transformed Inputs》, arXiv preprint arXiv:2503.11751, 2025.
[29]
Z. Chen 等, 《MJ-Bench: Is Your Multimodal Reward Model Really a Good Judge for Text-to-Image Generation?》, arXiv preprint arXiv:2407.04842, 2024.
[30]
M. Yasunaga, L. Zettlemoyer, 和 M. Ghazvininejad, 《Multimodal rewardbench: Holistic evaluation of reward models for vision language models》, arXiv preprint arXiv:2502.14191, 2025.
[31]
L. Li 等, 《VLRewardBench: A Challenging Benchmark for Vision-Language Generative Reward Models》, arXiv preprint arXiv:2411.17451, 2024.
[32]
J. Ruan 等, 《Vlrmbench: A comprehensive and challenging benchmark for vision-language reward models》, arXiv preprint arXiv:2503.07478, 2025.
[33]
E. Frick 等, 《How to Evaluate Reward Models for RLHF》, arXiv preprint arXiv:2410.14872, 2024.
[34]
S. Kim 等, 《Evaluating robustness of reward models for mathematical reasoning》, arXiv preprint arXiv:2410.01729, 2024.
[35]
M. Song, Z. Su, X. Qu, J. Zhou, 和 Y. Cheng, 《PRMBench: A Fine-grained and Challenging Benchmark for Process-Level Reward Models》, arXiv preprint arXiv:2501.03124, 2025.
[36]
W. Wang 等, 《VisualPRM: An Effective Process Reward Model for Multimodal Reasoning》, arXiv preprint arXiv:2503.10291, 2025.
[37]
H. Tu, W. Feng, H. Chen, H. Liu, X. Tang, 和 C. Xie, 《ViLBench: A Suite for Vision-Language Process Reward Modeling》. 2025年3月. 载于: https://arxiv.org/abs/2503.20271
[38]
H. Wang, W. Xiong, T. Xie, H. Zhao, 和 T. Zhang, 《Interpretable Preferences via Multi-Objective Reward Modeling and Mixture-of-Experts》, arXiv preprint arXiv:2406.12845, 2024.
[39]
Z. Wang 等, 《HelpSteer2: Open-source dataset for training top-performing reward models》, arXiv preprint arXiv:2406.08673, 2024.
[40]
Z. Wang 等, 《HelpSteer2-Preference: Complementing Ratings with Preferences》, arXiv preprint arXiv:2410.01257, 2024.
[41]
B. Adler 等, 《Nemotron-4 340B Technical Report》, arXiv preprint arXiv:2406.11704, 2024.
[42]
H. Touvron 等, 《Llama 2: Open foundation and fine-tuned chat models》, arXiv preprint arXiv:2307.09288, 2023.
[43]
J. Park, S. Jwa, M. Ren, D. Kim, 和 S. Choi, 《Offsetbias: Leveraging debiased data for tuning evaluators》, arXiv preprint arXiv:2407.06551, 2024.
← 上一章: 偏好数据 下一章: 正则化 →