使用 LLaMA Factory 微调一个 Qwen3-0.6B 猫娘

目录

前言

古人云,铸万象言机,笼天地妙文,其本意所归,唯在猫娘而已。

深度学习环境配置

详见:https://blog.csdn.net/qq_43650934/article/details/148948282?spm=1001.2014.3001.5501

大模型下载

魔搭社区:https://www.modelscope.cn/models/Qwen/Qwen3-0.6B

bash 复制代码
pip install modelscope
modelscope download --model Qwen/Qwen3-0.6B --local_dir ./llm

LLaMA Factory 配置

bash 复制代码
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
cd LLaMA-Factory
pip install -e ".[metrics]" --no-build-isolation  # 在"深度学习环境配置"一节中已经安装了 pytorch,为避免出现意外此处不再重复安装
llamafactory-cli webui  # Web 可视化页面,需要进入 LLaMA-Factory 文件夹使用

数据集准备

本次实验使用 Alpaca 格式数据集,需要包含 instruction(指令)input(输入,可选)output(输出)

数据集下载:https://www.modelscope.cn/datasets/AI-ModelScope/NekoQA-10K

该数据集只包含 instructionoutput ,使用代码将数据集处理为 Alpaca 格式:

python 复制代码
import json
import os

def format_dataset_to_alpaca(input_file, output_file=None):
    """
    读取 JSON 数据集,为每条数据添加空的 "input" 字段,
    并重新排序字段以符合 Alpaca 格式。
    """
    # 如果没有指定输出文件,则覆盖原文件
    if output_file is None:
        output_file = input_file

    # 检查文件是否存在
    if not os.path.exists(input_file):
        print(f"❌ 错误:找不到文件 '{input_file}',请确认路径是否正确。")
        return

    try:
        print(f"📂 正在读取文件: {input_file} ...")
        
        # 1. 读取 JSON 数据
        with open(input_file, 'r', encoding='utf-8') as f:
            data = json.load(f)

        if not isinstance(data, list):
            print("❌ 错误:JSON 文件的内容应该是一个列表(数组)。")
            return

        formatted_data = []
        
        # 2. 遍历并转换每一条数据
        for index, item in enumerate(data):
            # 构建新的字典,确保顺序为 instruction, input, output
            # Python 3.7+ 字典会保持插入顺序
            new_item = {
                "instruction": item.get("instruction"),
                "input": "",  # 在这里添加空的 input 字段
                "output": item.get("output")
            }
            formatted_data.append(new_item)

        # 3. 写入文件
        with open(output_file, 'w', encoding='utf-8') as f:
            json.dump(formatted_data, f, ensure_ascii=False, indent=4)
            
        print(f"✅ 成功!文件已更新并保存为: {output_file}")
        print(f"📊 共处理了 {len(formatted_data)} 条数据。")

    except json.JSONDecodeError as e:
        print("❌ JSON 解析失败。请检查你的 JSON 格式是否正确。")
        print(f"   常见原因:文件末尾多余逗号、使用了中文标点符号等。")
        print(f"   错误详情: {e}")
    except Exception as e:
        print(f"❌ 发生未知错误: {e}")

# ==========================================
# 主程序入口
# ==========================================
if __name__ == "__main__":
    # 确保这里填写你文件的实际路径
    # 如果文件就在当前目录下,直接写 'NekoQA-10K.json' 即可
    file_name = 'NekoQA-10K.json'
    
    format_dataset_to_alpaca(file_name)

LLaMA Factory 配置

  • 将微调数据集放入 LLaMA-Factory/data 文件夹。
  • LLaMA-Factory/data/dataset_info.json 中写入微调数据集信息:

LLaMA Factory 中主要需要修改的信息:

  • 模型路径:写入本地绝对路径,也可以设置模型下载源在线下载;
  • 微调方法:主要使用 fulllora
  • 数据集:在 dataset_info.json 中写入了微调数据集后,可以直接选中;
  • 训练轮数:模型训练多少轮。

点击开始即可训练,训练完成如下图:

验证微调效果

  • 点击 chat
  • 选取检查点路径
  • 点击加载模型,通过问答验证微调效果:

导出模型

  • 点击 Export
  • 指定导出目录
  • 点击开始导出

猫娘问答

相关推荐
wasp5201 分钟前
从 Vibe Coding 到真·生产力:OpenHarness 的“Harness 方程式”及其实战分析
人工智能·架构·开源·agent
weixin_408099674 分钟前
【完整教程】天诺脚本如何调用 OCR 文字识别 API?自动识别屏幕文字实战(附代码)
前端·人工智能·后端·ocr·api·天诺脚本·自动识别文字脚本
lvyuanj20 分钟前
深度解析Agent技术演进路径与未来趋势
人工智能
卷卷说风控33 分钟前
Claude Code 技术架构深扒:Prompt / Context / Harness 三维设计实践
人工智能·架构·prompt
Frank学习路上38 分钟前
【AI技能】跟着费曼学自动驾驶
人工智能·机器学习·自动驾驶
Want5951 小时前
Rokid AI Glasses应用开发实战:把记账助手“戴”在脸上
人工智能
bryant_meng1 小时前
【Reading Notes】(4)Favorite Articles from 2021
人工智能·深度学习·业界资讯
雷工笔记1 小时前
MES / WMS / AGV 交互时序图及生产管理模块界面设计清单
人工智能·笔记
海兰1 小时前
使用 Spring AI 打造企业级 RAG 知识库第二部分:AI 实战
java·人工智能·spring
清空mega1 小时前
动手学深度学习——多尺度锚框
人工智能·深度学习·目标跟踪