πŸ€– AI/ML

RLHF

Reinforcement Learning from Human Feedback

Reinforcement learning from human feedback. A core training technique of ChatGPT and Claude. Aligns models with human preferences.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

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 νŒŒμ΄ν”„λΌμΈ μ€€λΉ„ μ™„λ£Œ!")

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"Our chatbot sometimes gives inappropriate answers; strengthening safety alignment with RLHF might fix that."
πŸ’¬ In an Interview
"The three stages of RLHF are SFT, Reward Modeling, and PPO. The key is converting human preferences into a Reward signal."
πŸ’¬ In a Technical Discussion
"RLHF's KL Divergence penalty keeps the model from straying too far from its original distribution. Without it, Reward Hacking occurs."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Ignoring Reward Hacking: The model may learn ways to game the Reward rather than improve actual quality. Prevent this with a KL penalty and diverse evaluation metrics.
❌
Not checking labeler consistency: If labelers use different evaluation criteria, the Reward Model learns noise. Always measure inter-rater agreement.
βœ…
Consider using DPO: DPO (Direct Preference Optimization), which can learn preferences directly without a Reward Model, may be more efficient.

πŸ”— Related Terms

πŸ“š Learn More