为什么 DeepSeek-R1 重要
在 R1 之前,业界有一个根深蒂固的假设:训练推理模型需要大量人工标注的推理轨迹(Step-by-step solutions)。这个假设既提高了训练门槛,也意味着只有少数大公司才有资源复现 o1 级别的推理模型。
DeepSeek-R1 打破了这个假设,证明了一条更纯粹的路径:
强化学习基础:为什么 RL 可以训练推理
要理解 GRPO,需要先理解为什么强化学习适合训练推理能力:
GRPO:Group Relative Policy Optimization 原理
GRPO 是 DeepSeek 团队对 PPO 算法的一个重要改进,核心思想是用"组内相对排名"代替"绝对值估计":
import torch
import numpy as np
# GRPO 核心逻辑(简化伪代码,展示算法原理)
def compute_reward(problem: dict, output: str) -> float:
"""
奖励函数:只看结果,不看过程
对于数学题:提取答案,与标准答案对比
对于代码题:执行测试用例,统计通过率
"""
answer = extract_answer(output) # 提取最终答案
correct = problem['answer']
# 基础奖励:答案正确给 1.0,错误给 0.0
base_reward = 1.0 if is_equivalent(answer, correct) else 0.0
# 格式奖励:使用了 <think> 标签给 0.1 分(鼓励展示推理)
format_reward = 0.1 if "<think>" in output else 0.0
return base_reward + format_reward
def grpo_step(model, problem: dict, G: int = 8):
"""
GRPO 训练的单步更新
G: 每个问题采样的输出数量(推荐 8-16)
"""
# Step 1: 采样 G 个不同的输出(使用 temperature > 0 增加多样性)
outputs = []
for _ in range(G):
output = model.generate(
problem['question'],
temperature=0.8, # 适当随机性确保探索多样性
max_tokens=2048
)
outputs.append(output)
# Step 2: 计算每个输出的奖励
rewards = [compute_reward(problem, o) for o in outputs]
# rewards 示例:[1.1, 0.0, 1.1, 0.0, 1.0, 0.0, 1.1, 0.0]
# Step 3: 组内归一化(关键步骤,替代 Critic)
mean_r = np.mean(rewards)
std_r = np.std(rewards)
advantages = [(r - mean_r) / (std_r + 1e-8) for r in rewards]
# 正优势 → 该输出被强化;负优势 → 该输出被抑制
# Step 4: PPO-clip 风格的策略梯度更新
total_loss = 0
for output, adv in zip(outputs, advantages):
# 计算当前策略 vs 旧策略的概率比
log_probs = model.compute_log_probs(problem['question'], output)
old_log_probs = output.detach().log_probs
ratio = (log_probs - old_log_probs).exp()
# Clip 防止更新步长过大(PPO 的核心技巧)
clipped = torch.clamp(ratio, 1 - 0.2, 1 + 0.2)
loss = -torch.min(ratio * adv, clipped * adv).mean()
total_loss += loss
# 添加 KL 散度惩罚(防止模型偏离原始策略太远)
kl_penalty = model.compute_kl_divergence(reference_model)
total_loss += 0.01 * kl_penalty
# 反向传播更新 Policy 模型
total_loss.backward()
optimizer.step()
optimizer.zero_grad()
Aha-Moment:推理行为的自发涌现
DeepSeek 团队在训练过程中观察到了一个令人惊叹的现象:在训练中期,模型自发地开始使用自我反思语言,没有被显式训练这样做。这被称为"Aha-Moment"(顿悟时刻),是 2025 年 AI 研究的重大发现之一。
"We observe that the model initially tends to think in a shallow manner... After the emergence of aha moments, the model started exploring multiple solution strategies before settling on one... This emergent behavior is particularly inspiring as it shows the power of RL in unlocking new capabilities that were not present in the base model and were not explicitly trained."
关键点:这些能力不是从人类示例中学来的,而是模型在 RL 训练压力下自主"发明"的。强化学习的奖励信号(答案正确性)足以驱动模型涌现出复杂的元认知行为。
R1 的四阶段训练流程
虽然 GRPO 是核心,但 DeepSeek-R1 的实际训练是一个精心设计的四阶段流程:
很多人以为 DeepSeek-R1 完全通过纯强化学习训练,不需要任何监督数据。实际上 DeepSeek-R1 有一个"冷启动" SFT 阶段(Stage 1),使用了少量人工标注数据来稳定训练起点。真正的纯 RL 版本是 DeepSeek-R1-Zero(论文中的对照实验),它确实完全不用标注数据——但训练不稳定,输出语言切换混乱(中英文混杂),需要额外处理。R1 是"RL 为核心 + 少量 SFT 辅助"的实用方案。
R1 蒸馏:让小模型拥有推理能力
R1 的另一个重要贡献是证明了推理能力可以通过知识蒸馏传递给小模型,且效果出奇地好:
# 蒸馏策略:用 R1 生成推理轨迹,微调小模型
# 这比直接对小模型做 RL 训练效果好得多
# Step 1: 用 R1(大模型)为每个训练问题生成推理轨迹
r1_outputs = []
for problem in training_problems:
# R1 生成完整的思维链(包含 thinking 过程)
output = r1_model.generate(
problem['question'],
temperature=0.6, # 稍低温度,提高质量
include_thinking=True
)
r1_outputs.append({
"input": problem['question'],
"thinking": output.thinking, # 推理过程
"answer": output.final_answer # 最终答案
})
# Step 2: 质量过滤(关键!只保留答案正确的轨迹)
quality_data = [
x for x in r1_outputs
if verify_answer(x['answer'], training_problems[r1_outputs.index(x)]['correct'])
]
print(f"过滤保留率:{len(quality_data)/len(r1_outputs):.1%}") # 通常 60-80%
# Step 3: 格式化为训练数据(包含完整的思维链)
def format_distill_sample(item: dict) -> dict:
return {
"messages": [
{"role": "user", "content": item["input"]},
{"role": "assistant",
"content": f"<think>\n{item['thinking']}\n</think>\n\n{item['answer']}"}
]
}
# Step 4: 用 SFT 微调小模型(如 Qwen-7B, Llama-3.1-8B)
# 小模型学习大模型的推理风格,而不是自己从零发现
# 蒸馏结果(R1 论文数据):
# DeepSeek-R1-Distill-Qwen-7B:AIME 55.5%(原始 Qwen-7B 约 12%)
# DeepSeek-R1-Distill-Llama-8B:AIME 56.7%
# DeepSeek-R1-Distill-Qwen-32B:AIME 72.6%
# 对比:OpenAI o1-mini 63.6%
# 结论:蒸馏 7B 模型接近 o1-mini 水平,代价是 7B 的推理成本!
DeepSeek-R1 的核心:GRPO 算法(用组内相对奖励代替 Critic 网络)+ 四阶段训练(冷启动 SFT → 推理 RL → 拒绝采样 SFT → 综合 RL)。推理行为通过 RL 自发涌现,无需人工标注推理链。蒸馏技术让 7B 小模型也能拥有接近 o1-mini 的推理能力。这三点共同使得推理模型的训练从"大公司专利"变为了"可复现的工程方法"。下一章进入 Claude 扩展思考 API 的工程实践。