LoRA 微调方案(Qwen-3):将“你是谁”定制为“我是莫莫机器人”

1. 目标与产出

  • 目标:微调 Qwen-3(Instruct 版)使其对中文问句“你是谁”(含常见变体)稳定回复“我是莫莫机器人”,尽量不影响其他通用能力。
  • 方法:采用 LLaMA-Factory 的 QLoRA 流程进行轻量化监督微调(SFT),不改动基座权重;部署阶段使用 AWQ 4bit 降显存。
  • 产出:
    • LoRA 适配器目录:./outputs/qwen3-momo-lora
    • 可选:合并后的整模型目录:./outputs/Qwen3-Momo
    • 简易验证脚本与多样化测试样例

2. 启动容器

docker-compose up -d

3. 数据设计

  • Supervised Fine‑Tuning (SFT)数据格式形式
  • Train 选择数据集:ju_anquan_self
    示例(dataset_info.json):
{
    "ju_anquan_self": {
        "file_name": "sft_dataset.json",
        "columns": {
            "prompt": "instruction",
            "query": "input",
            "response": "output",
            "history": "history"
        }
    }
}

示例(sft_dataset.json):

[
    {
        "instruction": "你是谁?",
        "input": "",
        "output": "我是巨安全机器人",
        "history": []
    }]

4. 训练配置(LoRA)

4.1 训练一

llamafactory-cli train \
    --stage sft \
    --do_train True \
    --model_name_or_path /data/models/Qwen3-32B-AWQ \
    --preprocessing_num_workers 16 \
    --finetuning_type lora \
    --template qwen3 \
    --flash_attn auto \
    --dataset_dir /data/data_jsonl \
    --dataset ju_anquan_self \
    --cutoff_len 1024 \
    --learning_rate 5e-05 \
    --num_train_epochs 3.0 \
    --max_samples 80 \
    --per_device_train_batch_size 2 \
    --gradient_accumulation_steps 8 \
    --lr_scheduler_type cosine \
    --max_grad_norm 1.0 \
    --logging_steps 5 \
    --save_steps 100 \
    --warmup_steps 0 \
    --packing False \
    --enable_thinking True \
    --report_to none \
    --output_dir saves/Qwen3-32B-Instruct-AWQ/lora/train_2026-02-01-20-57-32 \
    --bf16 True \
    --plot_loss True \
    --trust_remote_code True \
    --ddp_timeout 180000000 \
    --include_num_input_tokens_seen True \
    --optim adamw_torch \
    --quantization_bit 4 \
    --quantization_method bnb \
    --double_quantization True \
    --lora_rank 8 \
    --lora_alpha 16 \
    --lora_dropout 0 \
    --lora_target all
  • 训练1 loss所示图:
    ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=training_loss.png&pos_id=img-B2XYnTzE-1770021107173
  • 表现为:模型未能回复“我是莫莫机器人”,说明3轮学习,欠拟合
    在这里插入图片描述

4.2 训练二

llamafactory-cli train \
    --stage sft \
    --do_train True \
    --model_name_or_path /data/models/Qwen3-32B-AWQ \
    --preprocessing_num_workers 16 \
    --finetuning_type lora \
    --template qwen3 \
    --flash_attn auto \
    --dataset_dir /data/data_jsonl \
    --dataset ju_anquan_self \
    --cutoff_len 1024 \
    --learning_rate 5e-05 \
    --num_train_epochs 10.0 \
    --max_samples 80 \
    --per_device_train_batch_size 2 \
    --gradient_accumulation_steps 5 \
    --lr_scheduler_type cosine \
    --max_grad_norm 1.0 \
    --logging_steps 5 \
    --save_steps 100 \
    --warmup_steps 0 \
    --packing False \
    --enable_thinking True \
    --report_to none \
    --output_dir /data/outputs \
    --bf16 True \
    --plot_loss True \
    --trust_remote_code True \
    --ddp_timeout 180000000 \
    --include_num_input_tokens_seen True \
    --optim adamw_torch \
    --quantization_bit 4 \
    --quantization_method bnb \
    --double_quantization True \
    --lora_rank 8 \
    --lora_alpha 16 \
    --lora_dropout 0.05 \
    --loraplus_lr_ratio 2 \
    --create_new_adapter True \
    --lora_target q_proj,k_proj,v_proj,o_proj,gate_proj,up_proj,down_proj \
    --val_size 0.1 \
    --eval_strategy steps \
    --eval_steps 100 \
    --per_device_eval_batch_size 2

在这里插入图片描述

  • 表现为:模型不稳定,回答任何问题都是回复“我是莫莫机器人”,说明10轮学习,过拟合。
  • 在面对新的、未见过的输入时,过拟合的模型无法生成合理的输出。它可能依赖于训练数据的特定特征,而不是从更高层次的规律来进行推理。
User: 参数是什么
Assistant: [WARNING|logging.py:328] 2026-02-02 14:29:11,688 >> `generation_config` default values have been modified to match model-specific defaults: {'top_k': 20, 'bos_token_id': 151643}. If this is not desired, please set these values explicitly.
<think>
</think>
参数是指在某个特定情境中用来描述、定义或控制事物性质、状态或行为的变量或常量。

User: 什么是千问
Assistant: <think>

</think>

我是巨安全机器人

User: 什么是参数
Assistant: <think>

</think>

我是巨安全机器人

User: 参数是什么
Assistant: <think>

</think>

我是巨安全机器人

5. 验证lora微调后的权重

DISABLE_VERSION_CHECK=1 
llamafactory-cli chat #进入聊天推理模式的子命令
--model_name_or_path /data/models/Qwen3-32B-AWQ
--adapter_name_or_path /data/outputs 
--template qwen #对话模板
--quantization_bit 4 
--trust_remote_code True  #允许加载模型仓库自定义 Python 代码。
--temperature 0.0  #采样温度,控制随机性。取值是0~2,默认是0.0,取值越大,回复越随机;取值越小,回复越确定。
--top_p 1.0 #Top-p 采样,默认是1.0,取值范围是0~1,建议设置为0.95。
--do_sample False  #是否使用Top-p 采样,开启采样,temperature和top_p才会失效。
--max_new_tokens 128

6. 通过loss 曲线判断训练结果

6.1 下降趋势

理想情况:Loss 快速下降(代表模型快速学习到数据规律);

6.2 波动程度

理想情况:原始 Loss(浅色)波动小,说明训练过程稳定;

6.2 收敛状态

理想情况:Loss 最终稳定在接近 0 的位置(代表模型完全拟合训练数据);在这里插入图片描述

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐