大模型微调入门
当提示词工程无法满足需求时,微调(Fine-tuning)是下一步。LoRA 让微调变得轻量可行。
什么是 LoRA
LoRA(Low-Rank Adaptation)通过冻结原模型、只训练少量低秩矩阵,让微调成本大幅降低。一个 7B 模型用 LoRA 微调,单张消费级显卡就能跑。
用 LLaMA-Factory 微调
pip install llamafactory
# train.yaml
model_name_or_path: Qwen/Qwen2.5-7B
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 8
lora_target: all
dataset: my_data
template: qwen
output_dir: ./output
per_device_train_batch_size: 2
gradient_accumulation_steps: 8
learning_rate: 1e-4
num_train_epochs: 3
数据格式
[
{
"instruction": "把下面的中文翻译成英文",
"input": "今天天气真好",
"output": "The weather is nice today."
}
]
何时该微调
- 提示词搞不定的固定风格/格式
- 垂直领域术语理解
- 需要稳定一致的输出
微调不是万能的,先用好提示词和 RAG,再考虑微调。