基于 Hugging Face Transformers 的 Qwen 模型全参数微调,用于识别用户是否想要生成互动小游戏。
- 任务类型: 二分类意图识别
- 输入: 用户自然语言文本
- 输出:
1(生成游戏)或0(不生成游戏)
| 输入 | 输出 |
|---|---|
| 做一个猜数字游戏 | 1 |
| 来个修仙题材的文字冒险 | 1 |
| 今天天气怎么样 | 0 |
| 帮我写一首诗 | 0 |
Qwen3/
├── config.py # 配置文件
├── data_utils.py # 数据处理工具
├── train.py # 训练脚本
├── evaluate.py # 评估脚本
├── inference.py # 推理脚本
├── requirements.txt # 依赖配置
├── dataset/ # 数据集目录
│ ├── train.jsonl # 训练集 (6,069 条)
│ ├── valid.jsonl # 验证集 (1,012 条)
│ └── test.jsonl # 测试集 (1,012 条)
├── model/ # 基础模型目录 (需要放置 Qwen 模型)
└── output/ # 训练输出目录
conda create -n qwen-intent python=3.10
conda activate qwen-intentpip install -r requirements.txt将 Qwen 模型文件放置到 model/ 目录下。
方式一: 从 Hugging Face 下载
# 安装 huggingface-cli
pip install huggingface_hub
# 下载模型到 model 目录
huggingface-cli download Qwen/Qwen2.5-0.5B --local-dir ./model方式二: 使用 ModelScope 下载 (国内推荐)
pip install modelscope
# Python 下载
from modelscope import snapshot_download
snapshot_download('Qwen/Qwen2.5-0.5B', cache_dir='./model')方式三: 手动下载
从 Hugging Face 或 ModelScope 下载模型文件,解压到 model/ 目录。
python train.pypython train.py \
--model_name_or_path ./model \
--output_dir ./output \
--num_train_epochs 5 \
--learning_rate 2e-5 \
--per_device_train_batch_size 8 \
--gradient_accumulation_steps 4| 参数 | 默认值 | 说明 |
|---|---|---|
--model_name_or_path |
./model | 基础模型路径 |
--output_dir |
./output | 输出目录 |
--num_train_epochs |
3 | 训练轮数 |
--learning_rate |
2e-5 | 学习率 |
--per_device_train_batch_size |
8 | 每个 GPU 的 batch size |
--gradient_accumulation_steps |
4 | 梯度累积步数 |
--fp16 |
True | 使用 FP16 混合精度 |
--bf16 |
False | 使用 BF16 混合精度 |
--resume_from_checkpoint |
None | 从 checkpoint 恢复训练 |
python train.py --resume_from_checkpoint ./output/checkpoint-500tensorboard --logdir ./output/logspython evaluate.py --model_path ./output/final_modelpython evaluate.py \
--model_path ./output/final_model \
--show_errors \
--max_errors 50python evaluate.py \
--model_path ./output/final_model \
--output_file ./evaluation_results.jsonl \
--show_errors| 参数 | 默认值 | 说明 |
|---|---|---|
--model_path |
(必填) | 模型路径 |
--test_file |
dataset/test.jsonl | 测试集文件 |
--batch_size |
32 | 评估 batch size |
--show_errors |
False | 显示错误案例 |
--max_errors |
20 | 最多显示的错误数 |
--output_file |
None | 保存详细结果的文件 |
python inference.py \
--model_path ./output/final_model \
--text "做一个猜数字游戏"输出示例:
==================================================
推理结果
==================================================
输入文本: 做一个猜数字游戏
预测标签: 1 (生成游戏)
原始输出: 1
==================================================
python inference.py \
--model_path ./output/final_model \
--interactive进入交互模式后,输入文本即可获得预测结果,输入 quit 退出。
准备输入文件 input.txt(每行一条文本):
做一个猜数字游戏
今天天气怎么样
来个修仙题材的冒险游戏
运行批量推理:
python inference.py \
--model_path ./output/final_model \
--input_file input.txt \
--output_file results.jsonl输出文件 results.jsonl 格式:
{"text": "做一个猜数字游戏", "prediction": 1, "label_name": "生成游戏", "raw_output": "1"}
{"text": "今天天气怎么样", "prediction": 0, "label_name": "不生成游戏", "raw_output": "0"}编辑 config.py 可以修改默认配置:
@dataclass
class ModelConfig:
model_name_or_path: str = "/your/model/path" # 模型路径
@dataclass
class TrainingConfig:
learning_rate: float = 2e-5 # 学习率
num_train_epochs: int = 3 # 训练轮数
per_device_train_batch_size: int = 8 # batch size| 指标 | 目标值 |
|---|---|
| 准确率 (Accuracy) | ≥ 95% |
| 精确率 (Precision) | ≥ 93% |
| 召回率 (Recall) | ≥ 93% |
| F1 Score | ≥ 93% |
减小 batch size 或启用梯度检查点:
python train.py --per_device_train_batch_size 4- 确保使用 GPU 训练
- 启用混合精度:
--fp16或--bf16 - 增加
dataloader_num_workers
确保 model/ 目录下包含以下文件:
config.jsonmodel.safetensors或pytorch_model.bintokenizer.jsontokenizer_config.json
- 检查测试数据格式是否正确
- 确认模型输出是否为
0或1 - 使用
--show_errors查看错误案例
数据集使用 JSONL 格式,每行一条记录:
{"text": "做一个猜数字游戏", "label": 1}
{"text": "今天天气怎么样", "label": 0}字段说明:
text: 用户输入文本label: 标签,1表示生成游戏,0表示不生成游戏