为 RLHF 准备数据

来自人类反馈的强化学习(RLHF)

Mina Parham

AI Engineer

偏好数据集 vs. 提示数据集

完整的 RLHF 流程。

来自人类反馈的强化学习(RLHF)

偏好数据集 vs. 提示数据集

包含初始提示数据集的完整 RLHF 流程。

来自人类反馈的强化学习(RLHF)

偏好数据集 vs. 提示数据集

包含初始提示数据集和用于奖励模型的偏好数据集的完整 RLHF 流程。

来自人类反馈的强化学习(RLHF)

提示数据集

  • 面向模型的问题
  • 可在 Hugging Face datasets 获取
prompt_data = load_dataset("center-for-humans-and-machines/rlhf-hackathon-prompts", 
                           split="train")
prompt_data['prompt'][0]
'How important is climate change?'
  • 可能需要提取 prompt
  • 查找标记:Input=, {{Text}}:, ###Human:
来自人类反馈的强化学习(RLHF)

探索偏好数据集

from datasets import load_dataset
preference_data = load_dataset("trl-internal-testing/hh-rlhf-helpful-base-trl-style", 
                               split="train")

Hugging Face 平台上 hh-rlhf 数据集的截图。

来自人类反馈的强化学习(RLHF)

处理偏好数据集

def extract_prompt(text):
    # Extract the prompt as the first element in the list
    prompt = text[0]["content"]
    return prompt
# Apply the extraction function to the dataset
preference_data_with_prompt = preference_data.map(
    lambda example: {**example, 'prompt': extract_prompt(example['chosen'])}
)
  • 提示的提取方式在不同数据集中可能不同
来自人类反馈的强化学习(RLHF)

最终的偏好数据集

sample = preference_data_with_prompt.select(range(1))
sample['prompt']
'What vitamins are essential for the body to function?'
sample['chosen']
[ { "content": "What vitamins are essential for the body to function?", "role": 
   "user" }, { "content": "There are some very important vitamins that ensure the 
   proper functioning of the body, including Vitamins A, C, D, E, and K along ...}]
来自人类反馈的强化学习(RLHF)

Passons à la pratique !

来自人类反馈的强化学习(RLHF)

Preparing Video For Download...