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

这篇具有很好参考价值的文章主要介绍了【XTuner 大模型单卡低成本微调实战】学习笔记。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

理论

Finetune简介

大语言模型

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

微调模式

增量预训练

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

指令跟随微调

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

LoRA和QLoRA

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

Xtuner介绍

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

实战

自定义微调

用 Medication QA 数据集进行微调

将数据转为 XTuner 的数据格式

目标格式:(.jsonL)

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

  • 写提示词请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

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

  • 执行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)
格式化后的数据集

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

划分训练集和测试集

  • 写提示词请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)

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

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

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

训练结果

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

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

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

作业

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

【XTuner 大模型单卡低成本微调实战】学习笔记,学习,笔记文章来源地址https://www.toymoban.com/news/detail-804852.html

到了这里,关于【XTuner 大模型单卡低成本微调实战】学习笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【通义千问】大模型Qwen GitHub开源工程学习笔记(5)-- 模型的微调【全参数微调】【LoRA方法】【Q-LoRA方法】

    本文介绍了使用微调技术进行自然语言生成的方法。通过使用transformers库中的AutoModelForCausalLM和AutoTokenizer,可以在多节点环境下进行微调。 你需要将所有样本放到一个列表中并存入json文件中。每个样本对应一个字典,包含id和conversation,其中后者为一个列表。示例如下所示:

    2024年01月23日
    浏览(52)
  • AI大模型预先学习笔记二:prompt提问大模型、langchain使用大模型框架、fine tune微调大模型

    1)环境准备 ①安装OpenAI库 附加 安装来源 ②生成API key ③设定本地的环境变量 ④代码的准备工作 ⑤在代码运用prompt(简单提问和返回) 2)交互代码的参数备注 temperature:随机性(从0到2可以调节,回答天马行空变化大可以选2) model:跟什么类型的model互动 role:(定义交互

    2024年01月17日
    浏览(37)
  • 【LLM】Prompt tuning大模型微调实战

    prompt tuning可看做是prefix tuning的简化版本,在输入层加入prompt tokens,并不需要加入MLP进行调整来解决难训练的问题,作者实验表明随着预训练模型参数量的增加,prompt tuning效果逼近fine tuning效果 之前提到过可以借助 peft 库(Parameter-Efficient Fine-Tuning)进行微调,支持如下tuni

    2024年02月13日
    浏览(36)
  • 【LLM】金融大模型场景和大模型Lora微调实战

    金融行业需要垂直领域LLM,因为存在金融安全和数据大多数存储在本地,在风控、精度、实时性有要求 (1)500亿参数的BloombergGPT BloombergGPT金融大模型也是用transformer架构,用decoder路线, 构建目前规模最大的金融数据集FINPILE,对通用文本+金融知识的混合训练。 用了512块40

    2024年02月12日
    浏览(41)
  • 书生·浦语大模型实战营-学习笔记4

    常见的两种微调策略:增量预训练、指令跟随 指令跟随微调 数据是一问一答的形式 对话模板构建 每个开源模型使用的对话模板都不相同 指令微调原理: 由于只有答案部分是我们期望模型来进行回答的内容,所以我们只对答案部分进行损失的计算 增量预训练微调 数据都是

    2024年01月22日
    浏览(33)
  • 【书生·浦语大模型实战】“PDF阅读小助手”学习笔记

    《新版本Lmdeploy量化手册与评测》 项目主页:【tcexeexe / pdf阅读小助手】 在InternStudio平台中选择 A100 (1/4) 的配置,镜像选择 Cuda11.7-conda ,可以选择已有的开发机 langchain ; Note: /home/tcexeexe/data/model/sentence-transformer :此路径来自于make_knowledge_repository.py 以上脚本会生成数据库文

    2024年01月24日
    浏览(33)
  • 【书生·浦语大模型实战营05】《(5)LMDeploy 大模型量化部署实践》学习笔记

    课程文档:《LMDeploy 的量化和部署》 定义 将训练好的模型在特定软硬件环境中启动的过程,使模型能够接收输入并返回预测结果 为了满足性能和效率的需求,常常需要对模型进行优化,例如模型压缩和硬件加速 产品形态 云端、边缘计算端、移动端 内存开销巨大 庞大的参数

    2024年01月22日
    浏览(38)
  • ChatGPT实战100例 - (11) 零成本学习Python

    用ChatGPT列一个培训大纲, 然后:哪里不会点哪里! 问题: 回答

    2024年02月06日
    浏览(28)
  • GPT实战系列-如何用自己数据微调ChatGLM2模型训练

    广告文案生成模型 输入文字 :类型#裙 颜色#蓝色 风格#清新*图案#蝴蝶结 输出文案 :裙身处采用立体蝴蝶结装饰辅以蓝色条带点缀,令衣身造型饱满富有层次的同时为其注入一丝甜美气息。将女孩清新娇俏的一面衬托而出。 训练和测试数据组织: 数据可以从 下载链接,t

    2024年02月06日
    浏览(37)
  • Gemma谷歌(google)开源大模型微调实战(fintune gemma-2b/7b)

    Gemma-SFT(谷歌, Google), gemma-2b/gemma-7b微调(transformers)/LORA(peft)/推理 v1, 20240222, transformers==4.38.0时候, 微调只跑通了gemma-2b-it(因为只计算了output的loss, 且使用的是fp16), 同时该版本transformers实现有些问题, 比如说1.tokenizer要加bos, 2.RoPE计算精度问题(float32), 3.激活函数gelu_pytorch_tanh; v2,

    2024年04月11日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包