【XTuner 大模型单卡低成本微调实战】学习笔记

news2024/10/3 0:25:23

参考学习教程【XTuner 大模型单卡低成本微调实战】

理论

Finetune简介

大语言模型

在这里插入图片描述

微调模式

增量预训练

在这里插入图片描述

指令跟随微调

在这里插入图片描述

LoRA和QLoRA

在这里插入图片描述

Xtuner介绍

在这里插入图片描述

实战

自定义微调

用 Medication QA 数据集进行微调

将数据转为 XTuner 的数据格式

目标格式:(.jsonL)

引自教程文档

  • 写提示词请Chatgpt完成,提示词如下:

Write a python file for me. using openpyxl. input file name is MedQA2019.xlsx
Step1: The input file is .xlsx. Exact the column A and column D in the sheet named “DrugQA” .
Step2: Put each value in column A into each “input” of each “conversation”. Put each value in column D into each “output” of each “conversation”.
Step3: The output file is .jsonL. It looks like:
[{
“conversation”:[
{
“system”: “xxx”,
“input”: “xxx”,
“output”: “xxx”
}
]
},
{
“conversation”:[
{
“system”: “xxx”,
“input”: “xxx”,
“output”: “xxx”
}
]
}]
Step4: All “system” value changes to “You are a professional, highly experienced doctor professor. You always provide accurate, comprehensive, and detailed answers based on the patients’ questions.”
(引自教程文档)

  • 下载相对应的安装包
pip install openpyxl

在这里插入图片描述

  • 执行python脚本,获得格式化后的数据集
 python xlsx2jsonl.py

python脚本如下:

import openpyxl
import json

# Step 1: Extract columns A and D from the sheet named "DrugQA"
def extract_data(file_path):
    workbook = openpyxl.load_workbook(file_path)
    sheet = workbook["DrugQA"]

    column_a = [cell.value for cell in sheet['A']]
    column_d = [cell.value for cell in sheet['D']]

    return column_a, column_d

# Step 2: Create conversations from extracted data
def create_conversations(column_a, column_d):
    conversations = []

    for input_value, output_value in zip(column_a, column_d):
        conversation = {
            "system": "You are a professional, highly experienced doctor professor. You always provide accurate, comprehensive, and detailed answers based on the patients' questions.",
            "input": str(input_value),
            "output": str(output_value)
        }

        conversations.append({"conversation": [conversation]})

    return conversations

# Step 3: Write conversations to a JSONL file
def write_to_jsonl(conversations, output_file):
    with open(output_file, 'w') as jsonl_file:
        for conversation in conversations:
            jsonl_file.write(json.dumps(conversation) + '\n')

if __name__ == "__main__":
    # Input and output file paths
    input_file_path = "MedQA2019.xlsx"
    output_file_path = "output.jsonl"

    # Step 1: Extract data from the input file
    column_a, column_d = extract_data(input_file_path)

    # Step 2: Create conversations
    conversations = create_conversations(column_a, column_d)

    # Step 3: Write conversations to JSONL file
    write_to_jsonl(conversations, output_file_path)

    print("Conversion completed. JSONL file created at:", output_file_path)
格式化后的数据集

在这里插入图片描述

划分训练集和测试集

  • 写提示词请Chatgpt完成,提示词如下:

my .jsonL file looks like:
[{
“conversation”:[
{
“system”: “xxx”,
“input”: “xxx”,
“output”: “xxx”
}
]
},
{
“conversation”:[
{
“system”: “xxx”,
“input”: “xxx”,
“output”: “xxx”
}
]
}]
Step1, read the .jsonL file.
Step2, count the amount of the “conversation” elements.
Step3, randomly split all “conversation” elements by 7:3. Targeted structure is same as the input.
Step4, save the 7/10 part as train.jsonl. save the 3/10 part as test.jsonl
(引自教程文档)

  • 生成的python脚本如下:
import json
import random

# Step 1: Read the .jsonL file
def read_jsonl(file_path):
    with open(file_path, 'r') as jsonl_file:
        data = jsonl_file.readlines()
        conversations = [json.loads(line.strip()) for line in data]

    return conversations

# Step 2: Count the amount of "conversation" elements
def count_conversations(conversations):
    return len(conversations)

# Step 3: Randomly split "conversation" elements by 7:3
def split_conversations(conversations):
    random.shuffle(conversations)
    total_conversations = len(conversations)
    split_index = int(0.7 * total_conversations)

    train_set = conversations[:split_index]
    test_set = conversations[split_index:]

    return train_set, test_set

# Step 4: Save the 7/10 part as train.jsonl, save the 3/10 part as test.jsonl
def save_to_jsonl(data, file_path):
    with open(file_path, 'w') as jsonl_file:
        for item in data:
            jsonl_file.write(json.dumps(item) + '\n')

if __name__ == "__main__":
    # Input and output file paths
    output_file_path = "output.jsonl"
    train_file_path = "train.jsonl"
    test_file_path = "test.jsonl"

    # Step 1: Read the .jsonL file
    conversations = read_jsonl(output_file_path)

    # Step 2: Count the amount of "conversation" elements
    total_conversations = count_conversations(conversations)
    print("Total conversations:", total_conversations)

    # Step 3: Randomly split "conversation" elements by 7:3
    train_set, test_set = split_conversations(conversations)

    # Step 4: Save the 7/10 part as train.jsonl, save the 3/10 part as test.jsonl
    save_to_jsonl(train_set, train_file_path)
    save_to_jsonl(test_set, test_file_path)

    print("Splitting completed. Train and test sets saved at:", train_file_path, "and", test_file_path)

运行数据集划分结果

pth 转 huggingface

 xtuner convert pth_to_hf ./internlm_chat_7b_qlora_medqa2019_e3.py ./work_dirs/internlm_chat_7b_qlora_medqa2019_e3/epoch_3.pth ./hf

在这里插入图片描述

训练结果

在这里插入图片描述

用 MS-Agent 数据集赋予 LLM 以 Agent 能力

在这里插入图片描述

作业

构建数据集,使用 XTuner 微调 InternLM-Chat-7B 模型, 让模型学习到它是你的智能小助手

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1394936.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

清晰光谱空间:全自动可调波长系统的高光谱成像优势

高光谱成像技术 高光谱成像技术是一种捕获和分析宽波长信息的技术,能够对材料和特征进行详细的光谱分析和识别。高光谱成像技术的实现通过高光谱相机,其工作原理是使用多个光学传感器或光学滤波器分离不同波长的光,并捕获每个波段的图像&…

CSS笔记II

CSS第二天笔记 复合选择器后代选择器子选择器并集选择器交集选择器伪类选择器 三大特性继承性层叠性优先级优先级-叠加计算规则 Emmet写法 背景属性背景图平铺方式位置缩放固定复合属性 显示模式转换显示模式 复合选择器 定义:由两个或多个基础选择器,通…

16.鸿蒙HarmonyOS App(JAVA)滑块组件Slider与评级组件Rating

16.鸿蒙HarmonyOS App(JAVA)滑块组件Slider与评级组件Rating ability_main.xml <?xml version"1.0" encoding"utf-8"?> <DirectionalLayoutxmlns:ohos"http://schemas.huawei.com/res/ohos"ohos:height"match_parent"oh…

格密码基础:最短格基与KZ基

目录 一. 介绍 二. 最短向量长度 三. GapSVP问题的困难性 四. 如何解决近似SVP问题 五. 推荐论文 一. 介绍 KZ基的全称叫Korkine-Zolotarev格基&#xff0c;KZ基也被称之为最短的格基。接下来我们介绍什么是KZ基&#xff1f; 给定任意秩为n的格&#xff0c;首先第一步寻…

解决Python安装库时出现的Requirement already satisfied问题

uirement already satisfied的问题当我们用pip install 库名时&#xff0c;出现了下面 Requirement already satisfied WARNING: Ignoring invalid distribution -ip 的问题 对于这样的问题&#xff0c;解决办法就是在 pip install 后加 - -target你所要添加的库文件地址(注意…

网络知识梳理:HTTP和HTTPS

HTTP&#xff08;超文本传输协议&#xff09;和HTTPS&#xff08;安全超文本传输协议&#xff09;是两种用于在互联网上传输数据的协议&#xff0c;主要用于网页浏览。它们在功能上相似&#xff0c;但在安全性方面有重要区别&#xff1a; 1.HTTP 定义&#xff1a;HTTP是一种用…

Flutter编译报错Connection timed out: connect

背景&#xff1a;用Android Studo 创建了Flutter项目&#xff0c;编译运行报错java.net.ConnectException: Connection timed out: connect 我自己的环境&#xff1a; windows11 Android Studio Flutter 截图如下&#xff1a; 将错误日志展开之后&#xff1a; Exception…

小白准备蓝桥杯之旅(c/c++b组)

前言&#xff1a;省赛获奖比例高达百分之60,只要比一半的人努力&#xff0c;你就能大概率获奖。 寒假做的3件事 1.稳基础 熟练掌握基础语法部分&#xff0c;c比c多个stl库优势&#xff0c;c语言的同学需要会实现c中stl库部分 2.刷真题 大概比赛前30天&#xff0c;坚持每天做…

SD-WAN解决跨国公司海外工厂网络安全问题

在跨境业务蓬勃发展的今天&#xff0c;越来越多的大型企业出于人力成本的考虑&#xff0c;在人力成本较低的发展中国家建立工厂。然而&#xff0c;传统基于路由器的网络架构已无法为这些跨国企业提供可靠的安全网络。那么&#xff0c;如何解决跨国企业海外工厂的网络难题呢&…

JAVA基础-----认识异常

文章目录 1. 异常的概念与体系结构1.1 异常的概念1.2 异常的体系结构1.3 异常的分类 2. 异常的处理2.1 防御式编程2.2 异常的抛出2.3 异常的捕获2.3.1 异常声明throws2.3.2 try-catch捕获并处理2.3.3 finally 2.4 异常的处理流程 3. 自定义异常类 1. 异常的概念与体系结构 1.1…

JavaWeb:Request Response

文章目录 1、Request和Response的概述2、Request继承体系3、Request获取请求数据3.1、获取请求行数据3.2、获取请求头3.3、获取请求体 4、Request通用方式请求参数5、POST请求参数乱码解决6、Request请求转发7、Response的响应状态码和响应头8、Response重定向9、Response响应字…

关于linux 救援模式出现xfs 文件系统挂载报 bad supperblock

关于linux 救援模式出现xfs 文件系统挂载报 bad supperblock 一种情况说明 挂载ISO文件进入救援模式&#xff0c;无法挂载XFS文件系统&#xff0c;xfs_repair也是报未知的超级块 使用 xfs_info 可以取到 xfs文件系统分区信息 xfs_db -c “sb 0” -c “p” $your_xfs_dev 也能…

2024年【建筑电工(建筑特殊工种)】考试报名及建筑电工(建筑特殊工种)免费试题

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 建筑电工(建筑特殊工种)考试报名是安全生产模拟考试一点通总题库中生成的一套建筑电工(建筑特殊工种)免费试题&#xff0c;安全生产模拟考试一点通上建筑电工(建筑特殊工种)作业手机同步练习。2024年【建筑电工(建筑特…

HarmonyOS 转场动画 ForEach控制

本文 我们继续说组件的专场特效 上文 HarmonyOS 转场动画 我们通过if控制了转场效果 本文 我们通过 ForEach 控制它的加载和删除 这时候就有人会好奇 ForEach 怎么控制删除呢&#xff1f; 很简单 循环次数不同 例如 第一次 10个 第二次 5个 那么后面的五个就相当于删除啦 我们…

ubuntu开放ssh服务

&#x1f4d1;前言 本文主要是【ubuntu】——ubuntu开放ssh服务的文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是听风与他&#x1f947; ☁️博客首页&#xff1a;CSDN主页听风与他 &#x1f304;每日一…

由于找不到d3dcompiler_43.dll缺失,无法打开软件的解决方法分享

d3dcompiler43.dll是什么文件&#xff1f;为什么会出现丢失的情况&#xff1f;又该如何解决呢&#xff1f;本文将详细介绍d3dcompiler43.dll的作用和影响&#xff0c;并提供6个有效的解决方法。 一、d3dcompiler43.dll是什么文件&#xff1f; d3dcompiler43.dll是DirectX SDK…

Linux网络--- SSH服务

一、ssh服务简介 1、什么是ssh SSH&#xff08;Secure Shell&#xff09;是一种安全通道协议&#xff0c;主要用来实现字符界面的远程登录、远程复制等功能。SSH 协议对通信双方的数据传输进行了加密处理&#xff0c;其中包括用户登录时输入的用户口令&#xff0c;SSH 为建立在…

linux 更新镜像源

打开终端&#xff0c;备份一下旧的 源 文件&#xff0c;以防万一 cd /etc/apt/ ls sudo cp sources.list sources.list.bak ls然后打开清华大学开源软件镜像站 搜索一下你的linux发行版本&#xff0c;我这里是ubuntu发行版本 点击这个上面图中的问号 查看一下自己的版本号&a…

Docker之nacos的安装和使用

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是君易--鑨&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《Docker之Dockerfile构建镜像》。&#x1f3af;&…

推荐 10 个基于 Stable Diffusion 的 AI 绘画网站

在当今快速发展的人工智能领域&#xff0c;AI 绘画已经成为一个不可忽视的趋势。特别是基于 Stable Diffusion 技术的 AI 绘画工具&#xff0c;以其强大的图像生成能力和丰富的创意潜力吸引了众多艺术家和设计师的目光。对于那些热爱艺术创作&#xff0c;但又缺乏专业绘画技巧的…