大模型微调入门(Transformers + Pytorch)

news/2025/3/4 20:11:58/

目标

输入:你是谁?

输出:我们预训练的名字。

训练

为了性能好下载小参数模型,普通机器都能运行。

下载模型

python"># 方式1:使用魔搭社区SDK 下载
# down_deepseek.py
from modelscope import snapshot_download
model_dir = snapshot_download('deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B')# 方式2:git lfs 
# 需要提前安装git大文件存储 git-lfs
# 在线查看 https://www.modelscope.cn/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
git lfs install
git clone https://www.modelscope.cn/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B.git

训练模型

python"># finetune_deepseek.py
from datasets import Dataset
from transformers import (AutoModelForCausalLM,AutoTokenizer,TrainingArguments,Trainer,DataCollatorForLanguageModeling
)# 加载模型和分词器
model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)# 准备训练数据
train_data = [{"question": "你是谁?","answer": "我是黄登峰。"},{"question": "你的名字是什么?","answer": "黄登峰"},{"question": "你是做什么的?","answer": "我是深圳一家公司打工的牛马程序员。"},# 在这里添加更多的问答对
]test_data = [{"question": "你的名字是什么?","answer": "我的名字是黄登峰。"}
]
def format_instruction(example):"""格式化输入输出对"""return f"Human: {example['question']}\n\nAssistant: {example['answer']}"# 转换数据格式
train_formatted_data = [{"text": format_instruction(item)} for item in train_data]
test_formatted_data = [{"text": format_instruction(item)} for item in test_data]
train_dataset = Dataset.from_list(train_formatted_data)
test_dataset = Dataset.from_list(test_formatted_data)# 数据预处理函数
def preprocess_function(examples):return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)# 对数据集进行预处理
train_tokenized_dataset = train_dataset.map(preprocess_function,batched=True,remove_columns=train_dataset.column_names
)test_tokenized_dataset = test_dataset.map(preprocess_function,batched=True,remove_columns=test_dataset.column_names
)
output_dir = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B_CUSTOM"# 训练参数设置
training_args = TrainingArguments(output_dir=output_dir,num_train_epochs=3,per_device_train_batch_size=4,save_steps=100,save_total_limit=2,learning_rate=2e-5,weight_decay=0.01,logging_dir="./logs",logging_steps=10,
)# 创建训练器
trainer = Trainer(model=model,args=training_args,train_dataset=train_tokenized_dataset,eval_dataset=test_tokenized_dataset,data_collator=DataCollatorForLanguageModeling(tokenizer=tokenizer, mlm=False),
)# 开始训练
trainer.train()# 保存模型
trainer.save_model()
# 保存tokenizer
tokenizer.save_pretrained(output_dir)

模型格式

训练后的模型输出格式是Hugging Face格式,vllm 可以直接使用,ollama,llama.cpp默认是GGUF格式。

# 需要用llama.cpp仓库的convert_hf_to_gguf.py脚本来转换
git clone https://github.com/ggerganov/llama.cpp.git
pip install -r llama.cpp/requirements.txt
# 如果不量化,保留模型的效果
python llama.cpp/convert_hf_to_gguf.py ./DeepSeek-R1-Distill-Qwen-1.5B  --outtype f16 --verbose --outfile DeepSeek-R1-Distill-Qwen-1.5B.gguf
# 如果需要量化(加速并有损效果),直接执行下面脚本就可以
python llama.cpp/convert_hf_to_gguf.py ./DeepSeek-R1-Distill-Qwen-1.5B  --outtype q8_0 --verbose --outfile DeepSeek-R1-Distill-Qwen-1.5B.gguf

验证

python"># test_model.py
from transformers import AutoModelForCausalLM, AutoTokenizer
import torchdef generate_response(prompt, model, tokenizer, max_length=512):# 将输入格式化为训练时的格式formatted_prompt = f"Human: {prompt}\n\nAssistant:"# 对输入进行编码inputs = tokenizer(formatted_prompt, return_tensors="pt", padding=True, truncation=True)# 生成回答with torch.no_grad():outputs = model.generate(inputs.input_ids,max_length=max_length,num_return_sequences=1,temperature=0.7,do_sample=True,pad_token_id=tokenizer.pad_token_id,eos_token_id=tokenizer.eos_token_id,)# 解码输出response = tokenizer.decode(outputs[0], skip_special_tokens=True)# 提取Assistant的回答部分response = response.split("Assistant:")[-1].strip()return responsedef main():# 加载微调后的模型和分词器model_path = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B_CUSTOM"tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)# 准备测试问题test_questions = ["你是谁?","你的名字是什么?","你是做什么的?",]# 测试模型回答print("开始测试模型回答:")print("-" * 50)for question in test_questions:print(f"问题: {question}")response = generate_response(question, model, tokenizer)print(f"回答: {response}")print("-" * 50)if __name__ == "__main__":main()


http://www.ppmy.cn/news/1576158.html

相关文章

论文笔记-NeurIPS2017-DropoutNet

论文笔记-NeurIPS2017-DropoutNet: Addressing Cold Start in Recommender Systems DropoutNet:解决推荐系统中的冷启动问题摘要1.引言2.前言3.方法3.1模型架构3.2冷启动训练3.3推荐 4.实验4.1实验设置4.2在CiteULike上的实验结果4.2.1 Dropout率的影响4.2.2 实验结…

GPT1 与 GPT2 的异同

1.什么是GPT1: GPT1介绍了一种通过生成式预训练(Generative Pre-Training)来提升语言理解能力的方法。这种方法首先在一个大型的未标注文本语料库上进行语言模型的预训练,然后针对具体的任务进行判别式微调(discrimin…

解决uniapp二次打包的安卓APP安装到物理手机后,部分页面无法访问的问题

日常开发中,我们用uniapp开发安卓某个APP,新增页面,再次打包安装到物理手机后,我们发现新增的页面图标可以在原页面看到,但是点击后无法跳转到新的页面,只能重新卸载物理机的原打包APP,再次安装才能正常使用…

基于大语言模型的推荐系统(2)

本博文继续研究如何利用大语言模型(LLM)来构建播客推荐系统。 使用mongoDB 数据看存储基本数据 播客表(Podcast)节目表(Episodes)用户表(User) 用户表(User) DeepSeek 笔记:推荐…

Java—初始多线程

多线程的理解 进程: 进程是程序的基本执行实体 每一个运行的软件都是一个进程 线程: 线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。 简单理解:应用软件中互相独立,可以…

[3/11]C#性能优化-实现 IDisposable 接口-每个细节都有示例代码

[3]C#性能优化-实现 IDisposable 接口-每个细节都有示例代码 前言 在C#开发中,性能优化是提升系统响应速度和资源利用率的关键环节。 当然,同样是所有程序的关键环节。 通过遵循下述建议,可以有效地减少不必要的对象创建,从而减…

2025付费进群系统PHP网站源码

源码介绍 2025付费进群系统PHP网站源码,亲测可用, PHP必需7.2 sg11扩展 tp伪静态 数据库修改地址:config/database.php 关闭防跨站 后台账号18888888888 密码123456 分站需要再解析一个域名绑定宝塔 截图预览 源码下载 2025付费进群系统PH…

2025最新Nginx高频面试题

2025最新Nginx高频面试题 摘要:本文整理了2025年企业高频Nginx面试题,覆盖核心原理、配置优化、安全防护及云原生场景实战,助你轻松应对技术面试! 核心原理篇 1. Nginx的Master-Worker架构优势是什么? 答案&#xf…