基于LLaMA-Factory大语言模型微调实战-训练一个属于自己的大模型

第一章 LLaMA-Factory安装及SFT微调

LLaMA-Factory 安装

运行以下指令以安装 LLaMA-Factory 及其依赖:

powershell 复制代码
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[torch,metrics]"

如果出现环境冲突,请尝试使用 pip install --no-deps -e . 解决

下载模型

在魔塔社区下载Qwen2.5-0.5B-Instruct模型

复制代码
git clone https://www.modelscope.cn/Qwen/Qwen2.5-0.5B-Instruct.git

SFT微调

微调命令

复制代码
llamafactory-cli train examples/train_lora/qwen2.5_lora_sft.yaml

说明:examples/train_lora/qwen2.5_lora_sft.yaml是LLaMA-Factory文件夹下面的相对文件路径;

qwen2.5_lora_sft.yaml文件内容

txt 复制代码
### model
# model_name_or_path:本地下载模型存放的绝对路径
### model
model_name_or_path: /mnt/workspace/models/Qwen2.5-0.5B-Instruct
trust_remote_code: true

### method 
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 8
lora_target: all

### dataset数据集
dataset: identity
template: qwen
cutoff_len: 2048
max_samples: 1000
overwrite_cache: true
preprocessing_num_workers: 16
dataloader_num_workers: 4

### output 微调后模型存放的位置
output_dir: saves/Qwen2.5-0.5B-Instruct/lora/sft-2026-01-14-01
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true
save_only_model: false
report_to: none  # choices: [none, wandb, tensorboard, swanlab, mlflow]

### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000
resume_from_checkpoint: null

### eval
# eval_dataset: alpaca_en_demo
# val_size: 0.1
# per_device_eval_batch_size: 1
# eval_strategy: steps
# eval_steps: 500

重要训练参数说明

数据集参数说明:

dataset: identity,identity是数据集json文件的名称,需要配置在/LLaMA-Factory/data/dataset_info.json文件中

identify.json文件路径为/LLaMA-Factory/data/identify.json,如下截图

微调过程截图

训练损失变化图

观察与分析

从 step 10 到 step 30,smoothed 曲线​ 从 loss ≈ 2.6 持续下降至 ≈ 1.4,表明模型在学习过程中逐步优化,微调有效。

相关推荐
AI_小站25 分钟前
6个GitHub爆火的免费大模型教程,助你快速进阶AI编程
人工智能·langchain·github·知识图谱·agent·llama·rag
xindoo26 分钟前
GitHub Trending霸榜!深度解析AI Coding辅助神器 Superpowers
人工智能·github
时间之里31 分钟前
【深度学习】:RF-DETR与yolo对比
人工智能·深度学习·yolo
北京阿法龙科技有限公司36 分钟前
数智化升级:AR 智能眼镜驱动工业运维效能革新
人工智能
风落无尘40 分钟前
《智能重生:从垃圾堆到AI工程师》——第二章 概率与生存
大数据·人工智能
j_xxx404_43 分钟前
Linux:静态链接与动态链接深度解析
linux·运维·服务器·c++·人工智能
收获不止数据库1 小时前
达梦9发布会归来:AI 时代,我们需要一款什么样的数据库?
数据库·人工智能·ai·语言模型·数据分析
hhb_6181 小时前
AI全栈编程生存指南
人工智能
AI-Frontiers1 小时前
transformer进阶之路:#2 工作原理详解
人工智能·深度学习·transformer
科研前沿1 小时前
2026 数字孪生前沿科技:全景迭代报告 —— 镜像视界生成式孪生(Generative DT)技术白皮书
大数据·人工智能·科技·算法·音视频·空间计算