从TinyZero的数据与源码来理解DeepSeek-R1-Zero的强化学习训练过程

news/2025/2/4 9:51:41 标签: 算法, 语言模型, deepseek

1. 引入

TinyZero(参考1)是伯克利的博士生复现DeepSeek-R1-Zero的代码参仓库,他使用veRL来运行RL强化学习方法,对qwen2.5的0.5B、1.5B、3B等模型进行训练,在一个数字游戏数据集上,达到了较好的推理效果。

下面解读源码中的关键训练逻辑细节。

2. 训练过程

  1. 原始数据

原始数据来自参考2,一共490k条数据,数据中只有两个字段,格式如下:

{
	"nums": [ 95, 11, 56 ],
	"target":28
}

这是一个数字游戏,要求对nums中的数据,进行基础数学运算(+, -, *, /),每个数字只能用一次,最终结果等于target的值。比如上例子,95-11-56=28。

  1. 数据处理

具体源码见参考3,下文仅仅解析关键步骤:

(1)训练集和测试集大小

默认值如下:

parser.add_argument('--train_size', type=int, default=327680)
parser.add_argument('--test_size', type=int, default=1024)

(2)对原始数据添加提示词

下面的dp就是一条原始数据(参考2.1例子):

def make_prefix(dp, template_type):
    target = dp['target']# 取出目标
    numbers = dp['nums']# 取出数字
    # 对于默认模型加的提示词如下
    if template_type == 'base':
        """This works for any base model"""
        prefix = f"""A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant first thinks about the reasoning process in the mind and then provides the user with the answer.
User: Using the numbers {numbers}, create an equation that equals {target}. You can use basic arithmetic operations (+, -, *, /) and each number can only be used once. Show your work in <think> </think> tags. And return the final answer in <answer> </answer> tags, for example <answer> (1 + 2) / 3 </answer>.
Assistant: Let me solve this step by step.
<think>"""
	# 对于qwen-instruct模型加的提示词如下
    elif template_type == 'qwen-instruct':
        """This works for Qwen Instruct Models"""
        prefix = f"""<|im_start|>system\nYou are a helpful assistant. You first thinks about the reasoning process in the mind and then provides the user with the answer.<|im_end|>\n<|im_start|>user\n Using the numbers {numbers}, create an equation that equals {target}. You can use basic arithmetic operations (+, -, *, /) and each number can only be used once. Show your work in <think> </think> tags. And return the final answer in <answer> </answer> tags, for example <answer> (1 + 2) / 3 </answer>.<|im_end|>\n<|im_start|>assistant\nLet me solve this step by step.\n<think>"""
    return prefix

(3)对数据进行完整的处理,增加提示词与reward等数据

如下函数中的example就是一条原始数据(参考2.1例子)。

        def process_fn(example, idx):
            question = make_prefix(example, template_type=args.template_type) # 增加提示词,见2.2.2
            solution = {
                "target": example['target'],
                "numbers": example['nums']
            }
            data = {
                "data_source": data_source, # 任务名称,默认为'countdown'
                "prompt": [{
                    "role": "user",
                    "content": question, # 带有提示词的问题
                }],
                "ability": "math",
                "reward_model": {
                    "style": "rule",
                    "ground_truth": solution # 含有nums和target
                },
                "extra_info": {
                    'split': split,
                    'index': idx,
                }
            }
            return data

最终数据为含有prompt和reward_model等字段的json结构。

  1. 训练

从参考4的训练代码中,摘取部分配置如下:

python3 -m verl.trainer.main_ppo \
data.train_files=$DATA_DIR/train.parquet \
data.val_files=$DATA_DIR/test.parquet \
data.train_batch_size=256 \
data.val_batch_size=1312 \
data.max_prompt_length=256 \
data.max_response_length=1024 \
actor_rollout_ref.model.path=$BASE_MODEL \
actor_rollout_ref.actor.optim.lr=1e-6 \
actor_rollout_ref.actor.ppo_mini_batch_size=128 \
actor_rollout_ref.actor.ppo_micro_batch_size=8 \
actor_rollout_ref.rollout.log_prob_micro_batch_size=8 \
actor_rollout_ref.rollout.tensor_model_parallel_size=$ROLLOUT_TP_SIZE \
actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \
actor_rollout_ref.ref.log_prob_micro_batch_size=4 \
critic.optim.lr=1e-5 \
critic.model.path=$BASE_MODEL \
critic.ppo_micro_batch_size=8 \
algorithm.kl_ctrl.kl_coef=0.001 \
trainer.logger=['wandb'] \
+trainer.val_before_train=False \
trainer.default_hdfs_dir=null \
trainer.n_gpus_per_node=$N_GPUS \
trainer.nnodes=1 \
trainer.save_freq=100 \
trainer.test_freq=100 \
trainer.project_name=TinyZero \
trainer.experiment_name=$EXPERIMENT_NAME \
trainer.total_epochs=15 2>&1 | tee verl_demo.log

这条命令是一个典型的 Python 脚本调用,用于训练一个基于 PPO(Proximal Policy Optimization) 算法的模型。

用veRL进行训练(参考5),需要指定 数据、模型、超参数:

(1)数据相关配置

data.train_files=$DATA_DIR/train.parquet:指定训练数据文件路径(Parquet 格式)。

data.val_files=$DATA_DIR/test.parquet:指定验证数据文件路径。

data.train_batch_size=256:训练时的批量大小(batch size)。

data.val_batch_size=1312:验证时的批量大小。

data.max_prompt_length=256:输入提示(prompt)的最大长度。

data.max_response_length=1024:生成响应(response)的最大长度。

(2)Actor 模型配置

actor_rollout_ref.model.path=$BASE_MODEL:指定 Actor 模型的预训练权重路径。

actor_rollout_ref.actor.optim.lr=1e-6:Actor 模型的学习率。

actor_rollout_ref.actor.ppo_mini_batch_size=128:PPO 算法中 Actor 的 mini-batch 大小。

actor_rollout_ref.actor.ppo_micro_batch_size=8:PPO 算法中 Actor 的 micro-batch 大小。

actor_rollout_ref.rollout.log_prob_micro_batch_size=8:Rollout 阶段计算 log probability 的 micro-batch 大小。

actor_rollout_ref.rollout.tensor_model_parallel_size=$ROLLOUT_TP_SIZE:Rollout 阶段的张量并行大小(用于分布式训练)。

actor_rollout_ref.rollout.gpu_memory_utilization=0.4:Rollout 阶段的 GPU 内存利用率。

actor_rollout_ref.ref.log_prob_micro_batch_size=4:参考模型(ref model)计算 log probability 的 micro-batch 大小。

(3)Critic 模型配置

critic.optim.lr=1e-5:Critic 模型的学习率。

critic.model.path=$BASE_MODEL:指定 Critic 模型的预训练权重路径。

critic.ppo_micro_batch_size=8:PPO 算法中 Critic 的 micro-batch 大小。

(4)算法配置

algorithm.kl_ctrl.kl_coef=0.001:KL 散度(Kullback-Leibler divergence)的系数,用于控制策略更新的稳定性。

(5)训练器配置

trainer.logger=['wandb']:使用 Weights & Biases(WandB)作为日志记录工具。

+trainer.val_before_train=False:在训练开始前不进行验证。

trainer.default_hdfs_dir=null:HDFS 目录未设置(HDFS 是分布式文件系统)。

trainer.n_gpus_per_node=$N_GPUS:每个节点使用的 GPU 数量。

trainer.nnodes=1:使用的节点数量(单节点训练)。

trainer.save_freq=100:每 100 步保存一次模型。

trainer.test_freq=100:每 100 步进行一次测试。

trainer.project_name=TinyZero:WandB 项目名称。

trainer.experiment_name=$EXPERIMENT_NAME:实验名称。

trainer.total_epochs=15:总训练轮数(epochs)。
  1. 训练效果

用强化学习的方法训练后,能如下所示,输出字段(推理过程),并给出最终结果字段。
在这里插入图片描述

3. 总结

通过具体的数据与处理训练过程,来更好的理解DeepSeek-R1-Zero的强化学习训练方法。

4. 参考

  1. 项目:https://github.com/Jiayi-Pan/TinyZero
  2. 数据:https://huggingface.co/datasets/Jiayi-Pan/Countdown-Tasks-3to4
  3. 数据处理源码:https://github.com/Jiayi-Pan/TinyZero/blob/main/examples/data_preprocess/countdown.py
  4. 训练源码:https://github.com/Jiayi-Pan/TinyZero/blob/main/scripts/train_tiny_zero.sh
  5. veRL:https://verl.readthedocs.io/en/latest/start/quickstart.html

http://www.niftyadmin.cn/n/5841464.html

相关文章

国土安全保障利器,高速巡飞无人机技术详解

高速巡飞无人机作为国土安全保障的利器&#xff0c;其技术特性和应用价值不可小觑。以下是对高速巡飞无人机技术的详细解析&#xff1a; 一、技术原理与关键组件 高速巡飞无人机的工作原理基于先进的飞行控制系统和制导技术。其核心组件主要包括&#xff1a; 1. 动力系统&…

【大数据技术】教程05:本机DataGrip远程连接虚拟机MySQL/Hive

本机DataGrip远程连接虚拟机MySQL/Hive datagrip-2024.3.4VMware Workstation Pro 16CentOS-Stream-10-latest-x86_64-dvd1.iso写在前面 本文主要介绍如何使用本机的DataGrip连接虚拟机的MySQL数据库和Hive数据库,提高编程效率。 安装DataGrip 请按照以下步骤安装DataGrip软…

说说Redis的内存淘汰策略?

大家好&#xff0c;我是锋哥。今天分享关于【说说Redis的内存淘汰策略?】面试题。希望对大家有帮助&#xff1b; 说说Redis的内存淘汰策略? 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 Redis 提供了多种内存淘汰策略&#xff0c;用于在内存达到限制时决定如何…

2.3学习总结

今天做了下上次测试没做出来的题目&#xff0c;作业中做了一题&#xff0c;看了下二叉树&#xff08;一脸懵B&#xff09; P2240&#xff1a;部分背包问题 先求每堆金币的性价比&#xff08;价值除以重量&#xff09;&#xff0c;将这些金币由性价比从高到低排序。 对于排好…

w191教师工作量管理系统的设计与实现

&#x1f64a;作者简介&#xff1a;多年一线开发工作经验&#xff0c;原创团队&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的网站项目。 代码可以查看文章末尾⬇️联系方式获取&#xff0c;记得注明来意哦~&#x1f339;赠送计算机毕业设计600个选题excel文…

DeepSeek 原理解析:与主流大模型的差异及低算力优势

在人工智能大模型蓬勃发展的浪潮中&#xff0c;DeepSeek 以其独特的技术路线和出色的性能表现脱颖而出。与主流大模型相比&#xff0c;DeepSeek 不仅在技术原理上有着显著的差异&#xff0c;还展现出了在较低算力下达到 OpenAI API 水平的卓越能力。本文将深入剖析这些独特之处…

7-1 什么是机器学习

什么是机器学习&#xff1f;上图展示了一段面试官与“机器学习程序”的对话&#xff1a; 面试官&#xff1a;9 10 等于多少&#xff1f; 答&#xff1a;3 面试官&#xff1a;差远了&#xff0c;是19。 答&#xff1a;16 面试官&#xff1a;错了&#xff0c;是19。 答&#xf…

二叉搜素树中的插入操作(力扣701)

为了满足题目的要求&#xff0c;我们可以选择在叶子节点之后添加节点&#xff0c;这样既可以满足题目要求&#xff0c;也不会破坏二叉搜索树的结构。在二叉搜索树中递归的时候&#xff0c;要根据大小&#xff0c;选择进行左递归还是右递归。当递归到空节点的时候&#xff0c;我…