使用 PPO 訓練

Reinforcement Learning from Human Feedback(RLHF)

Mina Parham

AI Engineer

以強化學習進行微調

RLHF 流程中的初始 LLM 與回饋模型。

Reinforcement Learning from Human Feedback(RLHF)

以強化學習進行微調

完整的 RLHF 流程。

Reinforcement Learning from Human Feedback(RLHF)

用 PPO 微調語言模型

 

將查詢提供給 LLM 以產生延續內容的示意圖。

Reinforcement Learning from Human Feedback(RLHF)

用 PPO 微調語言模型

 

將查詢提供給 LLM,模型補全:『we're half way there, oh livin' on a prayer』的示意圖。

Reinforcement Learning from Human Feedback(RLHF)

用 PPO 微調語言模型

 

將查詢提供給 LLM,補全為『we're half way there, oh livin' on a prayer』,並由另一個 LLM 評估補全的示意圖。

Reinforcement Learning from Human Feedback(RLHF)

用 PPO 微調語言模型

  • PPO:讓模型逐步調整
  • 避免對回饋過度擬合

機器人與蝸牛,象徵演算法緩步精進。

Reinforcement Learning from Human Feedback(RLHF)

用 TRL 實作 PPOTrainer

from trl import PPOConfig
config = PPOConfig(model_name="gpt2",learning_rate=1.4e-5)
from trl import AutoModelForCausalLMWithValueHead
model = AutoModelForCausalLMWithValueHead.from_pretrained(config.model_name)
tokenizer = AutoTokenizer.from_pretrained(config.model_name)
from trl import PPOTrainer
ppo_trainer = PPOTrainer(model=model,config=config,dataset=dataset,
                         tokenizer=tokenizer)
Reinforcement Learning from Human Feedback(RLHF)

啟動訓練迴圈

for epoch in tqdm(range(10), "epoch: "):


for batch in tqdm(ppo_trainer.dataloader):
# Get responses response_tensors = ppo_trainer.generate(batch["input_ids"])
batch["response"] = [tokenizer.decode(r.squeeze()) for r in response_tensors]
# Compute reward score texts = [q + r for q, r in zip(batch["query"], batch["response"])]
rewards = reward_model(texts)
stats = ppo_trainer.step(query_tensors, response_tensors, rewards) ppo_trainer.log_stats(stats, batch, rewards)
Reinforcement Learning from Human Feedback(RLHF)

一起來練習吧!

Reinforcement Learning from Human Feedback(RLHF)

Preparing Video For Download...