首页 > 科技周边 > 人工智能 > 微调DeepSeek R1(推理模型)

微调DeepSeek R1(推理模型)

Lisa Kudrow
发布: 2025-03-01 09:08:13
原创
500 人浏览过

DeepSeek的开创性AI模型挑战Openai的主导地位。 这些先进的推理模型是免费的,可以使获得强大AI的访问民主化。 了解如何通过我们的视频教程微调DeepSeek:

该教程微调使用拥抱脸部医疗链数据集使用DeepSeek-r1-Distill-lalama-8b型号。 这种蒸馏型型号衍生自Llama 3.1 8b,提供了与原始DeepSeek-R1相当的推理能力。 LLM和微调的新手? 考虑我们在Python课程中对LLM的介绍。

Fine-Tuning DeepSeek R1 (Reasoning Model)

>由作者 图像

介绍DeepSeek R1模型

> DeepSeek AI具有开源的DeepSeek-R1和DeepSeek-R1-Zero,在推理任务(数学,编码,逻辑)中与Openai的O1媲美。 探索我们综合的DeepSeek R1指南以获取详细信息。

> deepSeek-r1-Zero

这个开创性的模型使用大规模增强学习(RL),绕过初始监督微调(SFT)。 在实现独立的经营链(COT)推理的同时,它提出了重复推理和可读性问题等挑战。

> deepSeek-r1

DeepSeek-R1解决DeepSeek-R1-Zero的局限性,在RL之前包含了冷启动数据。这种多阶段的训练可实现最先进的性能,匹配OpenAI-O1,同时提高输出清晰度。

DeepSeek Distillation

DeepSeek还提供蒸馏型,平衡功率和效率。 这些较小的模型(1.5b至70b参数)保留了强有力的推理,DeepSeek-R1-Distill-Qwen-32b在基准中超过OpenAI-O1-Mini。 这突出了蒸馏过程的有效性。

来源:DeepSeek-ai/deepSeek-r1

>在我们的博客文章中了解更多有关DeepSeek-R1的功能,开发,蒸馏模型,访问,定价和OpenAi O1比较的信息:“ DeepSeek-R1:功能,O1比较,蒸发模型及更多”。 Fine-Tuning DeepSeek R1 (Reasoning Model) >微调DeepSeek R1:实用指南

>

按照以下步骤微调您的DeepSeek R1型号:>

1。设置

>我们利用Kaggle的免费GPU访问权限。创建一个Kaggle笔记本电脑,将您的拥抱脸和偏见令牌添加为秘密。安装

python软件包,以进行更快,更具内存效率的微调。 有关详细信息
<code>%%capture
!pip install unsloth
!pip install --force-reinstall --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git</code>
登录后复制

>用拥抱的面部CLI和重量和偏见(WANDB)进行身份验证:

<code>from huggingface_hub import login
from kaggle_secrets import UserSecretsClient
user_secrets = UserSecretsClient()

hf_token = user_secrets.get_secret("HUGGINGFACE_TOKEN")
login(hf_token)

import wandb

wb_token = user_secrets.get_secret("wandb")

wandb.login(key=wb_token)
run = wandb.init(
    project='Fine-tune-DeepSeek-R1-Distill-Llama-8B on Medical COT Dataset', 
    job_type="training", 
    anonymous="allow"
)</code>
登录后复制
2。加载模型和令牌

>使用4位量化的DeepSeek-R1-Distill-Lalama-8b加载不塞版本,以进行优化的性能:

3。预先调节推理
<code>from unsloth import FastLanguageModel

max_seq_length = 2048 
dtype = None 
load_in_4bit = True


model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/DeepSeek-R1-Distill-Llama-8B",
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
    token = hf_token, 
)</code>
登录后复制

>定义一个提示样式,并带有占位符的问题和回答。 这指导了模型的分步推理:

用样本医学问题测试模型:
<code>prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context. 
Write a response that appropriately completes the request. 
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.

### Instruction:
You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. 
Please answer the following medical question. 

### Question:
{}

### Response:
<think>{}"""</think></code>
登录后复制

>观察模型的预先调整推理,并通过微调来确定改进的领域。
<code>question = "A 61-year-old woman with a long history of involuntary urine loss during activities like coughing or sneezing but no leakage at night undergoes a gynecological exam and Q-tip test. Based on these findings, what would cystometry most likely reveal about her residual volume and detrusor contractions?"


FastLanguageModel.for_inference(model) 
inputs = tokenizer([prompt_style.format(question, "")], return_tensors="pt").to("cuda")

outputs = model.generate(
    input_ids=inputs.input_ids,
    attention_mask=inputs.attention_mask,
    max_new_tokens=1200,
    use_cache=True,
)
response = tokenizer.batch_decode(outputs)
print(response[0].split("### Response:")[1])</code>
登录后复制

4。加载和处理数据集

修改提示样式,以包括一个复杂思想链的占位符:>

创建一个函数以格式化数据集:

<code>train_prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context. 
Write a response that appropriately completes the request. 
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.

### Instruction:
You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. 
Please answer the following medical question. 

### Question:
{}

### Response:
<think>
{}
</think>
{}"""</code>
登录后复制

加载并处理数据集:

<code>EOS_TOKEN = tokenizer.eos_token  # Must add EOS_TOKEN


def formatting_prompts_func(examples):
    inputs = examples["Question"]
    cots = examples["Complex_CoT"]
    outputs = examples["Response"]
    texts = []
    for input, cot, output in zip(inputs, cots, outputs):
        text = train_prompt_style.format(input, cot, output) + EOS_TOKEN
        texts.append(text)
    return {
        "text": texts,
    }</code>
登录后复制

5。设置模型

<code>from datasets import load_dataset
dataset = load_dataset("FreedomIntelligence/medical-o1-reasoning-SFT","en", split = "train[0:500]",trust_remote_code=True)
dataset = dataset.map(formatting_prompts_func, batched = True,)
dataset["text"][0]</code>
登录后复制
使用lora配置模型:

设置教练:

<code>model = FastLanguageModel.get_peft_model(
    model,
    r=16,  
    target_modules=[
        "q_proj",
        "k_proj",
        "v_proj",
        "o_proj",
        "gate_proj",
        "up_proj",
        "down_proj",
    ],
    lora_alpha=16,
    lora_dropout=0,  
    bias="none",  
    use_gradient_checkpointing="unsloth",  # True or "unsloth" for very long context
    random_state=3407,
    use_rslora=False,  
    loftq_config=None,
)</code>
登录后复制

6。模型培训

<code>from trl import SFTTrainer
from transformers import TrainingArguments
from unsloth import is_bfloat16_supported

trainer = SFTTrainer(
    model=model,
    tokenizer=tokenizer,
    train_dataset=dataset,
    dataset_text_field="text",
    max_seq_length=max_seq_length,
    dataset_num_proc=2,
    args=TrainingArguments(
        per_device_train_batch_size=2,
        gradient_accumulation_steps=4,
        # Use num_train_epochs = 1, warmup_ratio for full training runs!
        warmup_steps=5,
        max_steps=60,
        learning_rate=2e-4,
        fp16=not is_bfloat16_supported(),
        bf16=is_bfloat16_supported(),
        logging_steps=10,
        optim="adamw_8bit",
        weight_decay=0.01,
        lr_scheduler_type="linear",
        seed=3407,
        output_,
    ),
)</code>
登录后复制
训练模型:

(注意:原始响应包括训练进度的图像;此处省略了这些图像,因为不可能进行图像复制。

7。邮政调节推理
<code>trainer_stats = trainer.train()</code>
登录后复制

通过与以前相同的问题查询微调模型来比较结果。 观察推理和响应简洁性的改善。

(注意:原始响应包括改进的模型输出;此处省略了这一点。

8。保存和推动模型

>在本地保存模型,然后将其推到拥抱的脸部集线器:>

(注意:原始响应包括显示成功的模型保存和推动的图像;此处省略了这些。)

>

9。部署和结论

>教程结束时,建议使用Bentoml或本地转换为GGEF格式提出部署选项。 它强调了开源LLM的重要性,并强调了O3和操作员AI的OpenAI柜台。 保留了这些资源的链接。
<code>new_model_local = "DeepSeek-R1-Medical-COT"
model.save_pretrained(new_model_local) 
tokenizer.save_pretrained(new_model_local)

model.save_pretrained_merged(new_model_local, tokenizer, save_method = "merged_16bit",)

new_model_online = "kingabzpro/DeepSeek-R1-Medical-COT"
model.push_to_hub(new_model_online)
tokenizer.push_to_hub(new_model_online)

model.push_to_hub_merged(new_model_online, tokenizer, save_method = "merged_16bit")</code>
登录后复制

>重写的响应在简化结构并删除不必要的重复时维护核心信息。 保留代码块以进行完整。 图像被引用但不复制。

>

以上是微调DeepSeek R1(推理模型)的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板