HyperAI
Back to Headlines

Reinforcement Learning from Human Feedback: Aligning Large Language Models with Human Values

vor einem Tag

Reinforcement Learning from Human Feedback (RLHF) is a critical technology driving the alignment of Large Language Models (LLMs) like GPT-4, Gemini, Claude, and Llama with human preferences and values. This process involves fine-tuning these models to ensure they produce helpful, harmless, and honest responses, making them more reliable and user-friendly. ### Stage 1: Supervised Fine-Tuning (SFT) Before delving into RLHF, LLMs undergo supervised fine-tuning (SFT). In this stage, a model is fine-tuned on a dataset of human-labeled examples to align its initial responses with human intent. This step provides a solid foundation, making the subsequent RLHF more effective. ### Stage 2: Training a Reward Model (RM) — Learning Human Preferences The heart of RLHF lies in training a reward model (RM) that captures human judgment. This process involves several key steps: 1. **Select Prompts and Generate Responses**: A diverse set of prompts is used to generate multiple candidate responses from the SFT model. 2. **Present Pairs to Human Labelers**: These candidate responses are shown to human labelers in pairs for the same prompt. 3. **Collect Preferences**: Labelers choose the preferred response between each pair, creating a dataset of preference tuples (prompt, winner response, loser response). 4. **Train RM**: The reward model is trained using these preferences to predict which responses humans would prefer. The reward model is trained to output a scalar score for each response, indicating its desirability. The loss function for training the RM is calculated to minimize the difference between the model's predicted scores and the human preferences. Here’s a conceptual implementation of the RM loss calculation using PyTorch: ```python import torch import torch.nn as nn import torch.optim as optim from transformers import AutoModelForSequenceClassification, AutoTokenizer # Initialize models and tokenizer reward_model = AutoModelForSequenceClassification.from_pretrained('sft_model_path', num_labels=1) tokenizer = AutoTokenizer.from_pretrained('sft_model_path') # Function to compute RM loss def compute_reward_model_loss(reward_model, tokenizer, prompt, response_winner, response_loser): # Tokenize inputs winner_inputs = tokenizer(prompt + response_winner, return_tensors='pt', truncation=True, padding=True) loser_inputs = tokenizer(prompt + response_loser, return_tensors='pt', truncation=True, padding=True) # Get reward scores from the model score_winner = reward_model(**winner_inputs).logits.squeeze() score_loser = reward_model(**loser_inputs).logits.squeeze() # Calculate loss based on pairwise comparison loss = -torch.log(torch.sigmoid(score_winner - score_loser)).mean() return loss # Training loop optimizer = optim.Adam(reward_model.parameters(), lr=1e-5) for batch in preference_dataloader: optimizer.zero_grad() loss = compute_reward_model_loss(reward_model, tokenizer, batch['prompt'], batch['response_winner'], batch['response_loser']) loss.backward() optimizer.step() ``` ### Stage 3: Fine-Tuning with Reinforcement Learning (RL) — Optimizing the Policy The final stage of RLHF involves using the trained reward model to optimize the behavior of the SFT language model (now referred to as the policy model) through reinforcement learning. This process ensures that the model generates responses that not only align with human preferences but also avoid harmful or unethical content. The key steps in this stage are: 1. **Generate Responses**: The policy model generates responses to a batch of prompts. 2. **Calculate Log Probabilities**: The log probabilities of generating these responses are calculated under both the policy and reference models (the frozen SFT model). 3. **Calculate KL Divergence**: The Kullback-Leibler (KL) divergence between the policy and reference models is approximated to ensure the policy does not deviate too much from the initial aligned behavior. 4. **Combine into Objective**: The objective is to maximize the expected reward minus a penalty term for the KL divergence. This is achieved by minimizing the negative of this objective. Here’s a conceptual implementation of the RL loss calculation: ```python import torch from transformers import AutoModelForCausalLM # Initialize models and tokenizer policy_llm = AutoModelForCausalLM.from_pretrained('policy_model_path') reference_llm = AutoModelForCausalLM.from_pretrained('sft_model_path') reward_model = AutoModelForSequenceClassification.from_pretrained('reward_model_path') tokenizer = AutoTokenizer.from_pretrained('tokenizer_path') # Function to compute RL loss def compute_rl_loss(policy_llm, reference_llm, reward_model, tokenizer, prompts, responses, beta): # Get rewards reward_inputs = tokenizer(prompts, responses, return_tensors='pt', padding=True, truncation=True) with torch.no_grad(): rewards = reward_model(**reward_inputs).logits.squeeze() # Get log probabilities policy_inputs = tokenizer(prompts, return_tensors='pt', padding=True, truncation=True) policy_labels = tokenizer(responses, return_tensors='pt', padding=True, truncation=True).input_ids outputs_policy = policy_llm(**policy_inputs, labels=policy_labels) log_probs_policy = outputs_policy.loss * -policy_labels.size(1) with torch.no_grad(): outputs_ref = reference_llm(**policy_inputs, labels=policy_labels) log_probs_ref = outputs_ref.loss * -policy_labels.size(1) # Calculate KL divergence kl_div = log_probs_policy - log_probs_ref # Combine into objective loss = (-rewards + beta * kl_div).mean() return loss, rewards.mean(), kl_div.mean() # RL training loop rl_optimizer = optim.Adam(policy_llm.parameters(), lr=1e-6) for prompts_batch in rl_prompt_dataloader: # Generate responses responses_batch = policy_llm.generate(tokenizer(prompts_batch, return_tensors='pt').input_ids) responses_text = tokenizer.batch_decode(responses_batch) # Calculate loss and update rl_optimizer.zero_grad() loss, avg_reward, avg_kl = compute_rl_loss(policy_llm, reference_llm, reward_model, tokenizer, prompts_batch, responses_text, beta=0.1) loss.backward() rl_optimizer.step() ``` ### Industry Evaluation and Company Insights The introduction of RLHF has been a game-changer in the field of AI, particularly in the development of LLMs. Industry insiders highlight its significance in bridging the gap between AI capabilities and human expectations, making AI more trustworthy and useful. Companies like Anthropic, creators of Claude, and Meta, developers of Llama, have been at the forefront of applying RLHF to their models, demonstrating significant improvements in alignment and ethical behavior. RLHF is not without its challenges, however. The need for large-scale human feedback and the computational demands of training reward models and fine-tuning policies with RL add complexity to the process. Despite these hurdles, the technology continues to evolve, with ongoing research aimed at making it more efficient and scalable. In summary, RLHF represents a crucial step in the development of AI, ensuring that powerful models like GPT-4 and Llama can be deployed safely and effectively in real-world applications. The synergy between human judgment and machine learning fosters an AI landscape that is both advanced and aligned with human values.

Related Links