奖励模型与 TRL 的结果监督奖励建模训练器

奖励模型(RM)通常用监督学习训练,数据集包含偏好回答与非偏好回答的配对。

核验日期

本页目录

定义

奖励模型(RM)通常用监督学习训练,所用数据集包含成对的偏好回答与非偏好回答。训练目标是学习一个函数,给偏好回答打更高的分,使模型能够依据偏好对输出排序。TRL 支持用结果监督奖励建模(ORM)训练器来训练奖励模型。结果监督奖励建模训练器这一后训练方法由 Younes Belkada 贡献。

一图看懂

RewardTrainer 支持偏好数据集类型,包括隐式提示和显式提示两种。RewardTrainer 兼容标准格式和对话格式的数据集。训练和评估期间会记录指标。

流程图: 训练期间,每个样本应包含 chosen 和 rejected 字段。 > RewardTrainer 使用模型的分词器对每条输入分词。 > 损失定义为所观察到偏好的负对数似然。1训练期间,每个样本应包含 chosen 和 rejected 字段。2RewardTrainer 使用模型的分词器对每条输入分词。3损失定义为所观察到偏好的负对数似然。

工作原理

收到对话格式的数据集时,训练器会自动对数据集应用聊天模板。如果提示与补全(chosen 和 rejected)是分开提供的(显式提示情形),它们会在分词前先拼接。Bradley-Terry 模型是欠定的,即给所有奖励加上同一个常数,不会改变偏好概率。

论文 Helping or Herding? Reward Model Ensembles Mitigate but do not Eliminate Reward Hacking 提出增加一个辅助损失项,促使奖励以零为中心。该辅助损失项由 RewardConfig 中的 center_rewards_coefficient 参数控制。RewardConfig 是 RewardTrainer 的配置类。

RewardConfig 只包含奖励训练专用的参数。在 RewardConfig 中,learning_rate 默认为 1e-4,而 TrainingArguments 中为 5e-5。同样,在 RewardConfig 中 logging_steps 默认为 10,而 TrainingArguments 中为 500。

在 RewardConfig 的模型初始化配置中,可以转交 from_pretrained() 的关键字参数;除 num_labels 外的参数都受支持,num_labels 会自动设为 1。accuracy 指标是正确预测的比例,即模型给 chosen 回答的分数高于 rejected 回答,并在最近一个记录间隔内取平均。margin 指标是最近一个记录间隔内的平均间隔,即 chosen 与 rejected 奖励之间的差值。

loss 指标是最近一个记录间隔内的平均损失。mean_reward 指标是模型在最近一个记录间隔内给出的平均奖励分数。min_reward 指标是模型给出的最小奖励分数,这个值在记录间隔内取平均。

global_step 指标是到目前为止已执行的优化器步数总数。grad_norm 指标是梯度的 L2 范数,在梯度裁剪之前计算。

例子

快速开始示例演示了如何使用 TRL 的 RewardTrainer 训练奖励模型。在该示例中,Qwen 3 0.6B 模型在 UltraFeedback 数据集上训练。如果数据集不是预期格式之一,可以对其预处理,转换成预期格式。文档给出了一个使用 lmarena-ai 的 arena-human-preference-55k 数据集的预处理示例。

摘自官方来源
python
from trl import RewardTrainer
from datasets import load_dataset

trainer = RewardTrainer(
    model="Qwen/Qwen3-0.6B",
    train_dataset=load_dataset("trl-lib/ultrafeedback_binarized", split="train"),
)
trainer.train()
摘自官方来源
python
# Standard preference (implicit prompt)
{"chosen": "The sky is blue.",
 "rejected": "The sky is green."}

# Conversational preference (implicit prompt)
{"chosen": [{"role": "user", "content": "What color is the sky?"},
            {"role": "assistant", "content": "It is blue."}],
 "rejected": [{"role": "user", "content": "What color is the sky?"},
              {"role": "assistant", "content": "It is green."}]}

# Standard preference (explicit prompt)
{"prompt": "The sky is",
 "chosen": " blue.",
 "rejected": " green."}

# Conversational preference (explicit prompt)
{"prompt": [{"role": "user", "content": "What color is the sky?"}],
 "chosen": [{"role": "assistant", "content": "It is blue."}],
 "rejected": [{"role": "assistant", "content": "It is green."}]}
摘自官方来源
python
from datasets import load_dataset
import json

dataset = load_dataset("lmarena-ai/arena-human-preference-55k")

# Filter out ties
dataset = dataset.filter(lambda example: example["winner_tie"] == 0)

# Create 'chosen' and 'rejected' fields based on the winner column
def response_a_b_to_chosen_rejected(example):
    if example["winner_model_a"] == 1:
        example["chosen"] = example["response_a"]
        example["rejected"] = example["response_b"]
    else:
        example["chosen"] = example["response_b"]
        example["rejected"] = example["response_a"]
    return example

dataset = dataset.map(response_a_b_to_chosen_rejected)

# Convert to conversational format
def make_conversation(example):
    prompt = json.loads(example["prompt"])[0]  # '["What color is the sky?"]' -> "What color is the sky?"
    chosen = json.loads(example["chosen"])[0]
    rejected = json.loads(example["rejected"])[0]
    return {
        "chosen": [{"role": "user", "content": prompt}, {"role": "assistant", "content": chosen}],
        "rejected": [{"role": "user", "content": prompt}, {"role": "assistant", "content": rejected}],
    }


dataset = dataset.map(make_conversation)

# Keep only necessary columns
dataset = dataset.select_columns(["chosen", "rejected"])

print(next(iter(dataset["train"])))

来源

本页内容整理自以下官方来源,点击可核对原文。

核验日期 ·

本页由 AI 依据上述官方来源整理,并经自动核对,未经本站实测。 发现错误?告诉我们。