DeepSeek - R1 - Zero:强化学习提升LLM推理能力的奥秘

第一节:强化学习在DeepSeek - R1 - Zero中的基本概念与公式解释【通俗解释】

强化学习在DeepSeek - R1 - Zero里就像是一位“聪明的探险家”,在各种可能的推理路径中探索,通过不断尝试,找到最正确的推理链。它不像传统方法依赖大量人工标注数据,而是自己在“推理世界”里闯荡,靠奖励机制来引导自己走向正确方向。

核心内容

【DeepSeek - R1 - Zero利用强化学习,就如同探险家在陌生区域寻找宝藏,奖励机制就是引导探险家的“指南针”,让模型朝着生成正确推理链的方向前进。】

强化学习的奖励机制公式【通俗解释,用打比方的方式解释】

假设模型在状态 s t s_t st​ 下采取行动 a t a_t at​ 后,获得的奖励为 r t + 1 r_{t + 1} rt+1​,则奖励机制可表示为:

R = ∑ t = 0 T − 1 γ t r t + 1 R = \sum_{t = 0}^{T - 1} \gamma^t r_{t + 1} R=t=0∑T−1​γtrt+1​

变量解释

  • R R R:总奖励,代表模型在整个探索过程中获得的累计奖励,就像探险家最终找到的宝藏总价值。
  • t t t:时间步,记录模型探索的阶段,类似探险家在探索过程中的不同时间点。
  • T T T:总的时间步数,即探索过程的总时长,好比探险家计划的探险总时长。
  • γ \gamma γ:折扣因子, 0 ≤ γ ≤ 1 0 \leq \gamma \leq 1 0≤γ≤1,它决定了未来奖励相对于当前奖励的重要性。 γ \gamma γ 越接近1,模型越看重未来奖励,就像探险家更看重未来可能发现的更珍贵宝藏; γ \gamma γ 越接近0,模型越关注当前奖励,类似探险家只在乎眼前能拿到的东西。
  • r t + 1 r_{t + 1} rt+1​:在时间步 t t t 采取行动 a t a_t at​ 后获得的即时奖励,如同探险家在某个时间点采取某个行动后立即得到的收获。

具体实例与推演

假设我们有一个简单的数学推理任务,判断一个数是否为偶数。

  • 步骤

    1. 初始状态:模型拿到一个数字,这就是初始状态 s 0 s_0 s0​。
    2. 采取行动:模型可以选择两种行动,“判断为偶数”( a 1 a_1 a1​)和“判断为奇数”( a 2 a_2 a2​)。
    3. 获得奖励:如果数字确实是偶数,模型选择 a 1 a_1 a1​,则获得即时奖励 r 1 = 1 r_1 = 1 r1​=1;若选择 a 2 a_2 a2​,则 r 1 = − 1 r_1 = -1 r1​=−1。如果数字是奇数,情况相反。
    4. 累计奖励:假设这个推理任务总共有3个时间步( T = 3 T = 3 T=3),折扣因子 γ = 0.8 \gamma = 0.8 γ=0.8。如果模型在第一步就做出正确判断,那么总奖励 R = 1 + 0.8 × 0 + 0. 8 2 × 0 = 1 R = 1 + 0.8\times0 + 0.8^2\times0 = 1 R=1+0.8×0+0.82×0=1。
  • 应用公式

    这里我们应用上述奖励机制公式来计算模型在这个推理任务中的总奖励,以评估模型的表现。

第二节:DeepSeek - R1 - Zero相关公式

准确性奖励公式

为了激励模型生成正确的推理结果,定义准确性奖励 r a c c r_{acc} racc​:

r a c c = { 1 , if 推理结果正确 − 1 , if 推理结果错误 r_{acc} = \begin{cases} 1, & \text{if 推理结果正确} \\ -1, & \text{if 推理结果错误} \end{cases} racc​={1,−1,​if 推理结果正确if 推理结果错误​

变量解释

  • r a c c r_{acc} racc​:准确性奖励,根据推理结果是否正确给予相应奖励,就像考试答对得分答错扣分。
格式化奖励公式

为了让推理结果具有良好的可读性,定义格式化奖励 r f m t r_{fmt} rfmt​:

假设推理结果的格式符合要求的程度用 f f f 表示, 0 ≤ f ≤ 1 0 \leq f \leq 1 0≤f≤1,则

r f m t = α f + β ( 1 − f ) r_{fmt} = \alpha f + \beta (1 - f) rfmt​=αf+β(1−f)

其中 α \alpha α 和 β \beta β 是权重参数,且 α > β \alpha > \beta α>β。

变量解释

  • r f m t r_{fmt} rfmt​:格式化奖励,用于鼓励模型生成格式良好的推理结果。
  • f f f:推理结果格式符合要求的程度, f f f 越接近1,格式越好。
  • α \alpha α:格式符合要求时给予的奖励权重, α \alpha α 越大,对格式良好的奖励越高。
  • β \beta β:格式不符合要求时给予的奖励权重, β \beta β 越小,对格式不好的惩罚越大。
综合奖励公式

准确性奖励和格式化奖励综合起来,得到综合奖励 r t o t a l r_{total} rtotal​:

r t o t a l = λ r a c c + ( 1 − λ ) r f m t r_{total} = \lambda r_{acc} + (1 - \lambda) r_{fmt} rtotal​=λracc​+(1−λ)rfmt​

其中 λ \lambda λ 是权重参数, 0 ≤ λ ≤ 1 0 \leq \lambda \leq 1 0≤λ≤1。

变量解释

  • r t o t a l r_{total} rtotal​:综合奖励,综合考虑了准确性和格式化两方面的奖励,以全面引导模型生成优质推理链。
  • λ \lambda λ:权重参数,用于调整准确性奖励和格式化奖励在综合奖励中的相对重要性。 λ \lambda λ 越接近1,越看重准确性奖励; λ \lambda λ 越接近0,越看重格式化奖励。

第三节:公式探索与推演运算

相似公式比对
公式/概念共同点不同点
R = ∑ t = 0 T − 1 γ t r t + 1 R = \sum_{t = 0}^{T - 1} \gamma^t r_{t + 1} R=∑t=0T−1​γtrt+1​都用于强化学习奖励计算是通用的累计奖励公式,综合考虑所有时间步奖励及折扣因子
r a c c = { 1 , if 推理结果正确 − 1 , if 推理结果错误 r_{acc} = \begin{cases} 1, & \text{if 推理结果正确} \\ -1, & \text{if 推理结果错误} \end{cases} racc​={1,−1,​if 推理结果正确if 推理结果错误​与奖励相关专注于推理结果准确性的奖励,简单直接判断对错给予奖励
r f m t = α f + β ( 1 − f ) r_{fmt} = \alpha f + \beta (1 - f) rfmt​=αf+β(1−f)与奖励相关针对推理结果格式的奖励,考虑格式符合程度及权重参数
r t o t a l = λ r a c c + ( 1 − λ ) r f m t r_{total} = \lambda r_{acc} + (1 - \lambda) r_{fmt} rtotal​=λracc​+(1−λ)rfmt​与奖励相关综合准确性和格式化奖励,通过权重参数平衡两者
公式推导

综合奖励公式推导

我们希望模型既准确又有良好格式的推理结果,所以要综合准确性奖励和格式化奖励。设准确性奖励为 r a c c r_{acc} racc​,格式化奖励为 r f m t r_{fmt} rfmt​,为了平衡两者对模型的引导作用,引入权重参数 λ \lambda λ。则综合奖励 r t o t a l r_{total} rtotal​ 就是准确性奖励乘以其权重 λ \lambda λ 加上格式化奖励乘以其权重 ( 1 − λ ) (1 - \lambda) (1−λ),即得到 r t o t a l = λ r a c c + ( 1 − λ ) r f m t r_{total} = \lambda r_{acc} + (1 - \lambda) r_{fmt} rtotal​=λracc​+(1−λ)rfmt​。

探索相关公式

强化学习中,还有价值函数相关公式。状态价值函数 V ( s ) V(s) V(s) 表示从状态 s s s 开始,遵循最优策略所能获得的期望累计奖励:

V ( s ) = E π [ ∑ t = 0 ∞ γ t r t + 1 ∣ s 0 = s ] V(s) = \mathbb{E}_{\pi} \left[ \sum_{t = 0}^{\infty} \gamma^t r_{t + 1} \mid s_0 = s \right] V(s)=Eπ​[t=0∑∞​γtrt+1​∣s0​=s]

其中 π \pi π 是策略, E π \mathbb{E}_{\pi} Eπ​ 表示在策略 π \pi π 下的期望。

变量解释

  • V ( s ) V(s) V(s):状态 s s s 的价值函数,评估从该状态出发的长期收益,类似从某个地点出发按照最佳路线能获得的总收益。
  • π \pi π:策略,决定模型在每个状态下采取什么行动,好比探险家的行动路线规划。
  • E π \mathbb{E}_{\pi} Eπ​:在策略 π \pi π 下的期望,考虑了所有可能的行动序列及其概率。

动作价值函数 Q ( s , a ) Q(s, a) Q(s,a) 表示在状态 s s s 下采取行动 a a a,然后遵循最优策略所能获得的期望累计奖励:

Q ( s , a ) = E π [ ∑ t = 0 ∞ γ t r t + 1 ∣ s 0 = s , a 0 = a ] Q(s, a) = \mathbb{E}_{\pi} \left[ \sum_{t = 0}^{\infty} \gamma^t r_{t + 1} \mid s_0 = s, a_0 = a \right] Q(s,a)=Eπ​[t=0∑∞​γtrt+1​∣s0​=s,a0​=a]

变量解释

  • Q ( s , a ) Q(s, a) Q(s,a):在状态 s s s 采取行动 a a a 的动作价值函数,评估在该状态下采取这个行动的长期收益,类似在某个地点采取某个行动后按最佳路线能获得的总收益。

第四节:核心代码与可视化

以下Python代码演示了如何模拟DeepSeek - R1 - Zero中强化学习的奖励机制,并进行简单可视化。

# This code performs the following functions:
# 1. Simulates a simple reasoning task with different states and actions.
# 2. Calculates rewards based on accuracy and format rewards.
# 3. Visualizes the rewards over different time steps using line plots.
# 4. Analyzes the impact of different parameters on rewards.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 1. Simulate a simple reasoning task
num_steps = 10
states = np.arange(num_steps)
actions = np.random.choice([0, 1], size = num_steps)  # 0 for incorrect, 1 for correct
true_results = np.random.choice([0, 1], size = num_steps)

# 2. Calculate rewards
gamma = 0.9
alpha = 0.8
beta = 0.2
lambda_val = 0.6

accuracy_rewards = np.where(actions == true_results, 1, -1)
format_scores = np.random.rand(num_steps)
format_rewards = alpha * format_scores + beta * (1 - format_scores)
total_rewards = lambda_val * accuracy_rewards + (1 - lambda_val) * format_rewards

cumulative_rewards = np.zeros(num_steps)
for t in range(num_steps):
    cumulative_rewards[t] = np.sum([gamma**i * total_rewards[t - i] for i in range(t + 1)])

# 3. Visualize rewards
sns.set_theme(style = "whitegrid")
plt.figure(figsize=(12, 6))

plt.plot(states, accuracy_rewards, label='Accuracy Reward', linestyle='-', marker='o', color='blue')
plt.plot(states, format_rewards, label='Format Reward', linestyle='--', marker='s', color='green')
plt.plot(states, total_rewards, label='Total Reward', linestyle='-.', marker='^', color='red')
plt.plot(states, cumulative_rewards, label='Cumulative Reward', linestyle=':', marker='*', color='purple')

plt.title('Rewards in DeepSeek - R1 - Zero Reinforcement Learning Simulation', fontsize = 14)
plt.xlabel('Time Step', fontsize = 12)
plt.ylabel('Reward', fontsize = 12)
plt.legend()
plt.grid(axis='y', linestyle='--', alpha = 0.7)
plt.axhline(y = 0, color='black', linewidth = 1)

plt.annotate(f'Gamma: {gamma}', xy=(2, 0.8), xytext=(2, 1.2),
             arrowprops=dict(facecolor='black', shrink = 0.05), fontsize = 10, color='navy')
plt.annotate(f'Alpha: {alpha}', xy=(4, 0.6), xytext=(4, 1),
             arrowprops=dict(facecolor='black', shrink = 0.05), fontsize = 10, color='navy')
plt.annotate(f'Lambda: {lambda_val}', xy=(6, 0.4), xytext=(6, 0.8),
             arrowprops=dict(facecolor='black', shrink = 0.05), fontsize = 10, color='navy')

plt.tight_layout()
plt.show()

# 4. Output intermediate data
print("States:", states)
print("Actions:", actions)
print("True Results:", true_results)
print("Accuracy Rewards:", accuracy_rewards)
print("Format Rewards:", format_rewards)
print("Total Rewards:", total_rewards)
print("Cumulative Rewards:", cumulative_rewards)
输出内容描述
States模拟推理任务的时间步状态
Actions模型在每个时间步采取的行动
True Results每个时间步推理任务的真实结果
Accuracy Rewards每个时间步的准确性奖励
Format Rewards每个时间步的格式化奖励
Total Rewards每个时间步的综合奖励
Cumulative Rewards每个时间步的累计奖励
奖励随时间步变化图展示准确性奖励、格式化奖励、综合奖励和累计奖励随时间步的变化情况

代码功能实现

  1. 模拟推理任务:生成不同时间步的状态、行动和真实结果。
  2. 奖励计算:根据准确性和格式化计算各种奖励,并计算累计奖励。
  3. 可视化:绘制不同奖励随时间步的变化图,展示奖励动态。
  4. 输出中间数据:输出模拟过程中的各种数据,方便理解和调试。

第五节:参考信息源

  1. 强化学习理论基础
    • Sutton, R. S., & Barto, A. G. (2018). Reinforcement Learning: An Introduction. The MIT Press. [【强化学习经典著作,全面介绍强化学习的理论和算法】]
  2. 语言模型推理相关
    • Brown, T. B., et al. (2020). Language Models are Few - Shot Learners. arXiv preprint arXiv:2005.14165. [【探讨语言模型推理能力及少样本学习的论文】]

关键词:

#强化学习
#ReinforcementLearning
#DeepSeek - R1 - Zero
#大型语言模型
#LLM
#推理能力
#奖励机制
#准确性奖励
#格式化奖励