RLHF
Reinforcement Learning from Human Feedback
Reinforcement learning from human feedback. A core training technique of ChatGPT and Claude. Aligns models with human preferences.
Reinforcement Learning from Human Feedback
Reinforcement learning from human feedback. A core training technique of ChatGPT and Claude. Aligns models with human preferences.
RLHF (Reinforcement Learning from Human Feedback) is a technique that trains AI models using human preference feedback. It is a core training method for modern conversational AI such as ChatGPT and Claude, used to align models so that their responses match human values and preferences (Alignment).
RLHF was first applied to game AI training at OpenAI and DeepMind in 2017, and was applied to LLMs in earnest in the 2022 InstructGPT paper. Supervised learning alone struggled to produce "helpful yet safe" responses, and RLHF drew attention as an innovative way to solve this problem.
The core principle of RLHF is a three-stage training pipeline. 1) Learn basic capabilities with SFT (Supervised Fine-Tuning), 2) humans compare pairs of responses to train a Reward Model, 3) optimize the Policy Model with the PPO (Proximal Policy Optimization) algorithm. Through this process, the model gradually improves in the direction humans prefer.
In practice, RLHF is a key technology for balancing AI safety and usefulness. However, it requires thousands of human labelers, making it costly, and keeping labelers consistent with one another is difficult. Recently, more efficient alternatives such as DPO and RLAIF are being researched.
An overview of an RLHF pipeline using the TRL library.
# pip install trl transformers torch
from trl import PPOTrainer, PPOConfig, AutoModelForCausalLMWithValueHead
from transformers import AutoTokenizer
# 1. λͺ¨λΈ λ° ν ν¬λμ΄μ λ‘λ
model_name = "gpt2" # μ€μ λ‘λ SFTλ λͺ¨λΈ μ¬μ©
model = AutoModelForCausalLMWithValueHead.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
# 2. PPO μ€μ
ppo_config = PPOConfig(
model_name=model_name,
learning_rate=1.41e-5,
batch_size=16,
mini_batch_size=4,
gradient_accumulation_steps=1,
)
# 3. PPO Trainer μ΄κΈ°ν
ppo_trainer = PPOTrainer(
config=ppo_config,
model=model,
tokenizer=tokenizer,
)
# 4. RLHF νμ΅ λ£¨ν (κ°λ
μ μμ)
def reward_fn(response: str) -> float:
"""Reward Modelμ΄ μλ΅ νμ§ μ μ λ°ν"""
# μ€μ λ‘λ νμ΅λ Reward Model μ¬μ©
return len(response) * 0.01 # λ¨μ μμ
# νμ΅μ PPO μκ³ λ¦¬μ¦μΌλ‘ μ§ν
# ppo_trainer.step(query_tensors, response_tensors, rewards)
print("RLHF νμ΄νλΌμΈ μ€λΉ μλ£!")
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free