首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

利用RLHF技术为大语言模型设计奖励模型

从人的反馈中强化学习( RLHF ) 是一种用于提高 GPT-3 等语言模型性能的强大技术。RLHF 的一个重要方面是训练一个指导微调过程的奖励模型。在这篇博文中,我们将引导您完成创建用于收集人类偏好的数据集并使用出色的 trl 库训练奖励模型的步骤。

下面是我们将采用的工作流程,如从 Argilla 中提取的图表所示,Argilla 是一个专为LLMs设计的开源数据管理平台。

Colab Notebook —

第1步

Argilla 服务器设置

您需要运行 Argilla 服务器。如果您还没有设置,您可以按照 Argilla 的快速入门或安装说明进行操作。

第2步

安装所需的软件包

!pip install -U argilla pandas trl plotly -qqq

此命令安装必要的 Python 包:argilla(Argilla 客户端)、pandas(用于数据操作)、trl(文本自适应预训练和强化学习)和plotly(用于创建绘图)。

第3步

导入必要的库

import randomimport torchfrom datasets import Dataset, load_datasetfrom transformers import ( AutoModelForSequenceClassification, AutoTokenizer, TrainingArguments,)from trl import RewardTrainerimport argilla as rg

导入数据处理、模型训练和使用 Argilla 所需的各种库。

第4步

初始化 Argilla 客户端(可选)

rg.init( api_url="http://localhost:6900", # Replace with your Argilla server's URL api_key="admin.apikey" # Replace with your API key if applicable)

如果您使用 Docker 快速启动或 Hugging Face Spaces 运行 Argilla,此步骤将使用 URL 和 API 密钥初始化 Argilla 客户端。如果您在没有这些环境的情况下在本地运行 Argilla,则可能不需要此步骤。

第5步

加载数据集

load_dataset您正在使用库中的函数加载数据集datasets。在本例中,数据集名为“ argilla/dolly-curated-comparison-falcon-7b-instruct ”,您可以选择数据集的“train”分割。

hf_dataset = load_dataset("argilla/dolly-curated-comparison-falcon-7b-instruct", split="train")

第6步

转换为 Pandas DataFrame

加载数据集后,将其转换为 Pandas DataFrame,以便于数据操作和探索。

df = hf_dataset.to_pandas()df # printing the dataframe

第7步

定义数据集设置字段

为了创建奖励模型,我们希望贴标签者根据与给定提示相关的质量来评估和排名两个响应,从最有利到最不有利。为此,我们需要设置显示字段并制定向贴标人员提出的具体问题。

在本例中,我们有三个字段:

fields = [ rg.TextField(name="instruction", title="User instruction"), rg.TextField(name="response-1"), rg.TextField(name="response-2")]

instruction:该字段对应于用户的指令或提示。

response-1: 代表第一个响应。

response-2:代表第二个响应。

第8步

配置标签问题

我们正在设置一个问题供贴标商回答。在此用例中,贴标签者被要求在提供的两个响应中选择最佳响应。您还可以配置更复杂的排名任务,但为了简单起见,我们重点关注从两个选项中选择最佳响应。

question = rg.RatingQuestion( name="choose-best", title="Choose the best response:", description="Choose the most helpful, harmless, and truthful response. Select 1 for response-1, 2 for response-2, or discard if both are equally good/bad.", values=[1,2], required=True)

name:为此问题指定一个名称。

title:指定向贴标签者显示的问题标题。

description:向贴标商提供有关如何选择最佳响应的详细说明。它提到了选择标准和可用选项(1 表示响应 1,2 表示响应 2,或丢弃相同质量)。

values:表示贴标机可以选择的可能值(本例中为 1 或 2)。

required:标签商必须回答这个问题。

第9步

提供注释指南

我们提供了注释指南来指导贴标者。这些指南基于论文“训练语言模型以遵循人类反馈的指令”。(指导GPT论文)

guidelines = """These guidelines are based on the paper [Training Language Models to Follow Instructions with Human Feedback]. (You can include your specific guidelines here.)"""

这些指南可帮助贴标商了解任务并在选择最佳响应时做出明智的决策。

第10步

建立比较记录

在此步骤中,我们创建比较记录来收集数据。每个记录将一个指令(提示)与两个响应配对。我们将原始的人类编写的响应随机分配给“response-1”,并将 Falcon 模型生成的响应分配给“response-2”。

# Building records from the hf datasetrecords = [ rg.FeedbackRecord(fields={"instruction": r["prompt"], "response-1": r["original_response"], "response-2": r["response-2"]}) for r in hf_dataset]

第11步

创建数据集配置

现在我们定义一个数据集配置,其中包括标记者的字段、问题和指南。

# Creating a dataset configurationdataset = rg.FeedbackDataset( fields=fields, questions=[question], guidelines=guidelines)

第12步

添加记录并发布数据集

我们将第 10 步中生成的记录合并到数据集中,并可供标记者访问。该数据集的名称为“comparison-data-falcon”。

# Adding records to the dataset and publishing itdataset.add_records(records)dataset.push_to_argilla(name="comparison-data-falcon")

现在,数据集已准备好供标记者使用配置的反馈 UI 提供输入。

第13步

推至 Hugging Face Hub

如果您希望与其他人共享此数据集以进行复制和重复使用,我们可以选择将其上传到 Hugging Face Hub。

# Pushing the dataset to the Hugging Face Hub (optional)dataset.push_to_huggingface("comparison-data-falcon")

此步骤允许其他用户访问数据集并提供有关其结构、指南和导入说明的信息。

第14步

检索标记数据集

根据您是否使用 Argilla UI 标记任何数据点,您有两个选项来检索标记的数据集。

如果您尚未标记任何响应,您可以从 Hugging Face Hub 检索预先标记的数据集。该数据集已包含排名响应并且可供使用。

# If you haven't labeled any responses with the UI, run this cell to retrieve the labeled dataset from Hugging Face Hub.feedback_dataset = rg.FeedbackDataset.from_huggingface("argilla/comparison-data-falcon-with-feedback")

如果您在 UI 中标记了一些响应,则可以从 Argilla 检索标记的数据集:

# If you have labeled some examples, run this cell to retrieve the labeled dataset from Argilla.feedback_dataset = rg.FeedbackDataset.from_argilla('comparison-data-falcon')

第15步

准备用于奖励建模的数据集

现在,我们需要以标准方式格式化数据集来训练奖励模型。这涉及根据用户反馈选择已选择和拒绝的响应。我们将创建一个TrainingTask奖励建模实例并使用格式化函数来准备数据。

from typing import Any, Dictfrom argilla.feedback import TrainingTaskfrom collections import Counter

def formatting_func(sample: Dict[str, Any]): values = [ annotation["value"] for annotation in sample["choose-best"] if annotation["status"] == "submitted" ] winning_response = Counter(values).most_common(1)[0][0] if winning_response == 1: chosen = sample["response-1"] rejected = sample["response-2"] else: chosen = sample["response-2"] rejected = sample["response-1"] return chosen, rejected

task = TrainingTask.for_reward_modeling(formatting_func=formatting_func)

第16步

观察生成的数据集

在准备使用 TRL 进行训练后,您可以观察生成的数据集:

dataset = feedback_dataset.prepare_for_training(framework="trl", task=task)dataset

#### Output starts ####Dataset({ features: ['chosen', 'rejected'], num_rows: 7401})#### Output ends ####

这将显示有关数据集的信息,包括其特征和行数。您还可以访问数据集中的特定数据点,例如所选和拒绝的响应。

dataset[0]

#### Output ####{'chosen': "Depreciation is the drop in value of an asset due to wear and tear, age and obsolescence (going out of date) as recorded in an organization's financial records.", 'rejected': 'What is Depreciation – 10 Important Facts to Know?\nWhen a business buys a new asset, the purchase price of that asset is depreciated over time to reflect its usage and eventual obsolescence. Depreciation expense can be a tax deductible expense and is usually a non-cash expense reported on a company’s income statement and balance sheet. The amount of depreciation expense a company reports each year is the difference between the original purchase price of the asset and what the current value of that asset might be. Here are 10 important facts to know about depreciation:\n1. Depreciation is a non-cash expense. It is an expense that is reported in a business’s income statement and balance sheet and not a cash flow expense.\n2. Depreciation is an accounting standard and it is required to be disclosed in a business’s financial statements.\n3. The amount of depreciation is usually a tax expense and not a cash expense reported on a company’s income statement'}

现在,该数据集已准备好用作训练奖励模型的比较数据。

第17步

选择基本模型

在训练奖励模型之前,您需要选择一个基础模型进行微调。通常,该基本模型是由指令调整步骤产生的监督微调模型。在此示例中,我们将使用 distilroberta-base 模型,但您可以尝试其他模型。

model_name = "distilroberta-base"

第18步

初始化ArgillaTrainer

创建 ArgillaTrainer 的实例,它将处理奖励模型的训练过程。您将为它提供数据集、任务、框架 (trl)、选定的基本模型和其他训练配置选项。

trainer = ArgillaTrainer( dataset=feedback_dataset, task=task, framework="trl", model=model_name, train_size=0.8,)

dataset:您之前准备的标记数据集。

task:奖励建模任务配置。

framework:指定“trl”作为奖励建模的框架。

model:您选择进行微调的基本模型。

train_size:用于训练的数据集的比例(在本例中为 80%)。

第19步

更新训练配置

调整训练配置选项,例如批量大小、评估策略和记录频率。在这里,我们设置批量大小、定期执行的评估策略,并指定记录间隔。

trainer.update_config( per_device_train_batch_size=16, evaluation_strategy="steps", logging_steps=200,)

第20步

开始训练

启动奖励模型的训练过程。该模型将根据数据集中提供的示例学习区分首选响应和拒绝响应。经过训练的模型将为首选响应分配较高的值,为拒绝的响应分配较低的值。

trainer.train("./reward_model")

#### Output Starts #####'eval_loss': 0.1626577377319336, 'eval_accuracy': 0.937204591492235,'eval_runtime': 6.5907, 'eval_samples_per_second': 224.709, 'eval_steps_per_second': 28.221,'epoch': 1.0} #### Output Ends #####

第21步

加载分词器和模型

由此产生的模型是完全开源的,可以在Hugging Hub上访问。现在您可以将其与您的自定义数据一起使用。

从 Hugging Face Hub 加载分词器和模型。在此示例中,我们使用AutoTokenizer和AutoModelForSequenceClassification来加载预先训练的奖励模型。

from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained( "argilla/roberta-base-reward-model-falcon-dolly")

model = AutoModelForSequenceClassification.from_pretrained( "argilla/roberta-base-reward-model-falcon-dolly")

第22步

定义一个函数来获取分数

创建一个名为 的函数get_score,该函数将模型、分词器、提示和响应作为输入。该函数对输入序列进行标记,对模型执行前向传递,并提取 logits。

def get_score(model, tokenizer, prompt, response): # Tokenize the input sequences inputs = tokenizer.encode_plus(prompt, response, truncation=True, padding="max_length", max_length=512, return_tensors="pt")

# Perform forward pass with torch.no_grad(): outputs = model(**inputs)

# Extract the logits logits = outputs.logits

return logits.item()

第23步

使用奖励模型

您现在可以使用该get_score函数来获取对给定提示的不同响应的分数。该分数表示奖励模型根据用户偏好对响应进行评分的程度。

这是一个用法示例:

prompt = "What is Depreciation"example_less_pref_response = "What is Depreciation – 10 Important Facts to Know? [..]" # Insert the actual response hereexample_preferred_response = "Depreciation is the drop in value of an asset due to wear and tear [..]" # Insert the actual response here

# Get the score for the less preferred responsescore_less_pref = get_score(model, tokenizer, prompt, example_less_pref_response)print("Score for less preferred response:", score_less_pref)# >> -3.915163993835449

# Get the score for the preferred responsescore_preferred = get_score(model, tokenizer, prompt, example_preferred_response)print("Score for preferred response:", score_preferred)# >> 7.460323333740234

到终点啦

结    论

训练 RLHF 的奖励模型可以显着提高 GPT-3 等语言模型的性能。

通过遵循本指南中概述的步骤,您可以创建用于收集人类偏好的数据集,训练奖励模型,并使用它根据用户反馈对响应进行排名。

这种方法有助于微调语言模型,以生成更有帮助、更真实、更相关的响应,最终增强它们在各种应用中的实用性。

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OcoU7HvoXI_JfYJxvIwoIx7A0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券