Skip to content

Repository files navigation

Qwen 意图识别模型训练

基于 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/                # 训练输出目录

环境配置

1. 创建虚拟环境 (推荐)

conda create -n qwen-intent python=3.10
conda activate qwen-intent

2. 安装依赖

pip install -r requirements.txt

3. 准备基础模型

将 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 FaceModelScope 下载模型文件,解压到 model/ 目录。

训练模型

基础训练

python train.py

自定义参数训练

python 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-500

查看训练日志

tensorboard --logdir ./output/logs

评估模型

基础评估

python evaluate.py --model_path ./output/final_model

显示错误案例

python evaluate.py \
    --model_path ./output/final_model \
    --show_errors \
    --max_errors 50

保存详细结果

python 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%

常见问题

1. CUDA 内存不足

减小 batch size 或启用梯度检查点:

python train.py --per_device_train_batch_size 4

2. 训练速度慢

  • 确保使用 GPU 训练
  • 启用混合精度: --fp16--bf16
  • 增加 dataloader_num_workers

3. 模型找不到

确保 model/ 目录下包含以下文件:

  • config.json
  • model.safetensorspytorch_model.bin
  • tokenizer.json
  • tokenizer_config.json

4. 评估指标异常

  • 检查测试数据格式是否正确
  • 确认模型输出是否为 01
  • 使用 --show_errors 查看错误案例

数据格式

数据集使用 JSONL 格式,每行一条记录:

{"text": "做一个猜数字游戏", "label": 1}
{"text": "今天天气怎么样", "label": 0}

字段说明:

  • text: 用户输入文本
  • label: 标签,1 表示生成游戏,0 表示不生成游戏

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages