微调入门实战指南,本文教你从零基础开始,通过实践操作掌握大模型Qwen2的微调技术。首先,确保你的Python环境兼容,安装必要的库,如swanlab
, modelscope
, transformers
, datasets
, peft
, accelerate
, pandas
。其次,获取并准备复旦新闻分类数据集,加载Qwen2模型并进行配置,同时集成SwanLab进行训练可视化与监控。通过提供的完整代码示例,实现数据集处理、模型训练、推理以及结果演示,助你全面掌握Qwen2模型的微调方法。无论是理论学习还是实际应用,本文都将是你必备的资源。
Qwen2大模型微调入门实战(完整代码) - 零基础入门到精通,收藏这一篇就够了
环境安装与调试
-
确保Python环境:Python版本应为3.8或更高。推荐使用PyCharm或其他IDE进行编程。
-
显卡要求:确保您的计算机内有一块兼容的英伟达显卡(至少10GB显存)。
- 安装所需库:
pip install swanlab modelscope transformers datasets peft accelerate pandas
测试版本:确保库版本兼容,例如:
modelscope==1.14.0, transformers==4.41.2, datasets==2.18.0, peft==0.11.1, accelerate==0.30.1, swanlab==0.3.9
准备数据集
- 数据获取:前往“魔搭社区”下载复旦新闻分类数据集,将
train.jsonl
和test.jsonl
文件保存在本地根目录下。
加载与配置模型
-
Qwen2模型下载:
from modelscope import snapshot_download model_dir = snapshot_download("qwen/Qwen2-1.5B-Instruct", cache_dir="./", revision="master")
- 模型加载与配置:
from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", torch_dtype=torch.bfloat16)
配置训练可视化与监控工具
-
安装SwanLab:
pip install swanlab
-
SwanLab集成与监控:
from swanlab.integration.huggingface import SwanLabCallback swanlab_callback = SwanLabCallback() trainer = Trainer( model=model, callbacks=[swanlab_callback], )
完整代码示例
目录结构:
|--- train.py
|--- train.jsonl
|--- test.jsonl
train.py脚本:
import json
from datasets import Dataset
def dataset_jsonl_transfer(origin_path, new_path):
with open(origin_path, "r") as file:
messages = [json.loads(line) for line in file]
with open(new_path, "w", encoding="utf-8") as file:
for message in messages:
file.write(json.dumps(message, ensure_ascii=False) + "\n")
def process_func(example):
MAX_LENGTH = 384
input_ids, attention_mask, labels = [], [], []
instruction = tokenizer("system\n你是一个文本分类领域的专家,你会接收到一段文本和几个潜在的分类选项,请输出文本内容的正确类型\nuser\n", add_special_tokens=False)
response = tokenizer(message["output"], add_special_tokens=False)
input_ids += instruction["input_ids"] + response["input_ids"] + [tokenizer.pad_token_id]
attention_mask += instruction["attention_mask"] + response["attention_mask"] + [1]
labels += [-100] * len(instruction["input_ids"]) + response["input_ids"] + [tokenizer.pad_token_id]
if len(input_ids) > MAX_LENGTH:
input_ids = input_ids[:MAX_LENGTH]
attention_mask = attention_mask[:MAX_LENGTH]
labels = labels[:MAX_LENGTH]
return {"input_ids": input_ids, "attention_mask": attention_mask, "labels": labels}
if __name__ == "__main__":
train_dataset_path = "train.jsonl"
test_dataset_path = "test.jsonl"
new_train_path = "new_train.jsonl"
new_test_path = "new_test.jsonl"
if not os.path.exists(new_train_path):
dataset_jsonl_transfer(train_dataset_path, new_train_path)
if not os.path.exists(new_test_path):
dataset_jsonl_transfer(test_dataset_path, new_test_path)
train_df = Dataset.from_json(new_train_path)
train_dataset = train_df.map(process_func, remove_columns=train_df.column_names)
args = TrainingArguments(
output_dir="./output/Qwen2",
per_device_train_batch_size=4,
num_train_epochs=2,
save_steps=100,
logging_steps=10,
learning_rate=1e-4,
gradient_accumulation_steps=4,
save_on_each_node=True,
)
swanlab_callback = SwanLabCallback()
trainer = Trainer(
model=model,
args=args,
train_dataset=train_dataset,
callbacks=[swanlab_callback],
)
trainer.train()
推理训练好的模型:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
def predict(messages, model, tokenizer):
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text], return_tensors="pt")
generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
return response
tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=False, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", torch_dtype=torch.bfloat16)
model = PeftModel.from_pretrained(model, "./output/Qwen2/checkpoint-XXX")
test_text_list = []
# 使用测试数据集验证模型性能
结果演示与模型应用
在SwanLab上查看训练曲线和模型效果,通过测试数据集评估模型性能。使用训练生成的模型进行文本分类预测,验证模型在实际场景中的应用能力。
小结
本文详细介绍了从零基础到精通大模型Qwen2微调的全过程,包括环境配置、数据准备、模型加载与配置、集成SwanLab进行监控与可视化,以及完整代码示例和模型应用。通过本文,你将能够掌握Qwen2模型微调的实战技能,实现理论与实践的无缝对接。无论是初学者还是高级用户,本指南都将是你不可或缺的参考资源。
共同学习,写下你的评论
评论加载中...
作者其他优质文章