DeepSeek-R1的发布为国产大模型争光了(太强了),不过 GRPO 算法源自 DeepSeekMath 7B 模型,该模型在 MATH 基准测试中取得了优异成绩。GRPO 是一种在线学习算法,核心思想是通过组内相对奖励来估计基线,从而避免使用额外的价值函数模型。
DeepSeek-R1的发布为国产大模型争光了(太强了),不过 GRPO 算法源自 DeepSeekMath 7B 模型,该模型在 MATH 基准测试中取得了优异成绩,论文发表于2024年2月份:https://huggingface.co/papers/2402.03300,以下是该论文的摘要原文:
Mathematical reasoning poses a significant challenge for language models due to its complex and structured nature. In this paper, we introduce DeepSeekMath 7B, which continues pre-training DeepSeek-Coder-Base-v1.5 7B with 120B math-related tokens sourced from Common Crawl, together with natural language and code data. DeepSeekMath 7B has achieved an impressive score of 51.7% on the competition-level MATH benchmark without relying on external toolkits and voting techniques, approaching the performance level of Gemini-Ultra and GPT-4. Self-consistency over 64 samples from DeepSeekMath 7B achieves 60.9% on MATH. The mathematical reasoning capability of DeepSeekMath is attributed to two key factors: First, we harness the significant potential of publicly available web data through a meticulously engineered data selection pipeline. Second, we introduce Group Relative Policy Optimization (GRPO), a variant of Proximal Policy Optimization (PPO), that enhances mathematical reasoning abilities while concurrently optimizing the memory usage of PPO.
def completion_reward(completions, **kwargs):
'''奖励函数,对较长的补全给予更高的分数'''
return [float(len(completion))/100 for completion in completions]
prompts = ["The sky is", "The sun is"]
completions = [" blue.", " in the sky."]
print("completion_reward: ", completion_reward(prompts=prompts, completinotallow=completions))
1.
2.
3.
4.
5.
6.
7.
(2)格式正确奖励函数
def format_reward(completions, **kwargs):
'''格式奖励'''
pattern = r"<think>.*?</think>\s*<answer>.*?</answer>"
responses = [completion[0]["content"] for completion in completions]
matches = [re.match(pattern, response) for response in responses]
return [0.5if match else0.0for match in matches]
prompts = [
[{"role": "assistant", "content": "What is the result of (1 + 2) * 4?"}],
[{"role": "assistant", "content": "What is the result of (3 + 1) * 2?"}],
]
completions = [
[{"role": "assistant", "content": "<think>The sum of 1 and 2 is 3, which we multiply by 4 to get 12.</think><answer>(1 + 2) * 4 = 12</answer>"}],
[{"role": "assistant", "content": "The sum of 3 and 1 is 4, which we multiply by 2 to get 8. So (3 + 1) * 2 = 8."}],
]
print("format_reward: ", format_reward(prompts=prompts, completinotallow=completions))