簡介
DPO(直接偏好優化) 簡化了 RLHF — 直接根據偏好資料進行訓練,無需單獨的獎勵模型和 PPO。
1. DPO 與 RLHF
| 方面 | RLHF | 資料保護官 |
|---|---|---|
| 步驟 | SFT→RM→PPO(3步驟) | SFT → DPO(2 步驟) |
| 獎勵模式 | 明確的、單獨的 | 政策隱含 |
| 訓練 | 複合物 (PPO) | 簡單(類似監督) |
| 穩定性 | 調整起來很棘手 | 穩定 |
| 性能 | 最先進的 | 類似 |
2. DPO 目標
$$\mathcal{L}{DPO}(\pi\theta; \pi_{ref}) = -\mathbb{E}\left[\log \sigma\left(\beta \log \frac{\pi_\theta(y_w|x)}{\pi_{ref}(y_beta|x) - \y_w| \frac{\pi_\theta(y_l|x)}{\pi_{ref}(y_l|x)}\right)\right]$$
3. 使用 TRL 實作 DPO
from trl import DPOTrainer, DPOConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("./sft_model")
ref_model = AutoModelForCausalLM.from_pretrained("./sft_model")
tokenizer = AutoTokenizer.from_pretrained("./sft_model")
config = DPOConfig(
beta=0.1,
learning_rate=5e-7,
per_device_train_batch_size=4,
num_train_epochs=3,
output_dir="./dpo_model",
)
trainer = DPOTrainer(
model=model,
ref_model=ref_model,
train_dataset=preference_dataset,
# Dataset format: {"prompt": str, "chosen": str, "rejected": str}
tokenizer=tokenizer,
args=config,
)
trainer.train()
4.GRPO-群組相關策略優化
用於 DeepSeek-R1:
- 不需要批評/價值網絡
- 基於群體的優勢估計
- 高效率的獎勵計算
from trl import GRPOTrainer, GRPOConfig
config = GRPOConfig(
num_generations=4, # Generate multiple responses per prompt
learning_rate=1e-6,
)
trainer = GRPOTrainer(
model=model,
reward_funcs=[reward_function],
train_dataset=prompt_dataset,
args=config,
)
trainer.train()
5.其他變體
| 方法 | 所需資料 | 關鍵想法 |
|---|---|---|
| 資料保護官 | 成對偏好 | 政策中的隱性獎勵 |
| GRPO | 提示+獎勵fn | 集團優勢 |
| 韓國貿易組織 | 二進制(好/壞) | 無需配對即可工作 |
| 首次公開發行 | 成對偏好 | 正規化 DPO |
| 奧爾波 | 成對偏好 | 無需參考模型 |
總結
| 方法 | 複雜性 | 資料 | 效能 |
|---|---|---|---|
| RLHF | 高 | 比較 + RM | 最佳 |
| 資料保護官 | 低 | 僅供比較 | 太棒了 |
| GRPO | 中等 | 提示+獎勵fn | 太棒了 |
| 韓國貿易組織 | 低 | 二進制回饋 | 好 |