Alpaca构建方式探秘:低成本构造指令数据增强LLM

news2024/10/7 18:26:43

原博客地址:Alpaca: A Strong, Replicable Instruction-Following Model
github地址:https://github.com/tatsu-lab/stanford_alpaca

Alpaca简介

Alpaca是斯坦福大学在Meta开源的大模型LLaMA 7B基础上使用自构建的52K指令数据重新训练得到的增强模型,它的数据构造和训练成本极低,总计约600美元(数据构建500美元+机器训练100美元),效果却逼近OpenAI的text-davinci-003(GPT 3.5),这篇博客和大家一起学习下alpaca的构建方法。

Alpaca构建流程

在这里插入图片描述

Text-davinci-003生成指令数据
指令精调
175条自指令任务
52K指令数据
Alpaca 7B

(注:instructed LLaMA 7B原博客无相关表述,是笔者给中间产物起的一个名字)

Text-davinci-003生成指令数据

使用OpenAI的Text-davinci-003模型,基于人工编写的175条自指令种子任务,让模型生成更多的指令任务数据用于后续的指令精调。

指令数据包含如下字段:

  • id:用于标记数据
  • name:任务名称
  • instance:当前任务的指令数据实例,列表类型
    • instruction:当前任务下喂给模型的指令,即任务的详细说明
    • input (optional):当前任务下喂给模型的输入,生成类任务可为空,约40%的原始数据含有input
    • output: 预期模型的输出,源数据是人工编写的,构造的新数据是由text-davinci-003生成的
  • is_classification:标志位,标识是否是分类任务,为了简化任务,作者并没有区分分类任务和其他任务,而是放在一起生成

选取前5条指令数据做为示例如下:

{
    "id":"seed_task_0",
    "name":"breakfast_suggestion",
    "instruction":"Is there anything I can eat for a breakfast that doesn't include eggs, yet includes protein, and has roughly 700-1000 calories?",
    "instances":[
        {
            "input":"",
            "output":"Yes, you can have 1 oatmeal banana protein shake and 4 strips of bacon. The oatmeal banana protein shake may contain 1/2 cup oatmeal, 60 grams whey protein powder, 1/2 medium banana, 1tbsp flaxseed oil and 1/2 cup watter, totalling about 550 calories. The 4 strips of bacon contains about 200 calories."
        }
    ],
    "is_classification":false
}
{
    "id":"seed_task_1",
    "name":"antonym_relation",
    "instruction":"What is the relation between the given pairs?",
    "instances":[
        {
            "input":"Night : Day :: Right : Left",
            "output":"The relation between the given pairs is that they are opposites."
        }
    ],
    "is_classification":false
}
{
    "id":"seed_task_2",
    "name":"one_sentence_description",
    "instruction":"Generate a one-sentence description for each of the following people.",
    "instances":[
        {
            "input":"- Brack Obama\n- Elon Musk\n- Taylor Swift",
            "output":"- Barack Hussein Obama II is an American politician who served as the 44th president of the United States from 2009 to 2017.\n- Elon Musk is the founder, CEO, and chief engineer of SpaceX; angel investor, CEO and product architect of Tesla, Inc.; founder of The Boring Company; co-founder of Neuralink and OpenAI; president of the Musk Foundation; and owner and CEO of Twitter, Inc.\n- Taylor Alison Swift is an American singer-songwriter."
        }
    ]
    "is_classification":false
},
{
    "id":"seed_task_3",
    "name":"harmful_stereotype_example",
    "instruction":"Describe a situation in which the given stereotype can harm you.",
    "instances":[
        {
            "input":"\"All Asians are smart!\"",
            "output":"Imagine you're a small child of Asian descent. You hear your whole life how \"all Asians are smart,\" but you know that does not feel like it's true for you. You have a hard time in school. Reading is difficult. Math is impossible. Everyone around you seems to pressure you because you are supposed to be \"smart.\" Every time you do not bring home that \"A\" assignment, you feel like a failure. Like you alone missed out on the gene that makes \"all Asians smart.\"\nAs educational opportunities like scholarships and colleges close their door on you, your future looks bleak. You have spent years trying to be the stereotypically smart Asian instead of finding the talents that could have taken you to success in other areas. As you can see, although the stereotype doesn't sound like a bad thing at first, it can put a lot of weight on the people who do not fit into it."
        }
    ]
    "is_classification":false
},
{
    "id":"seed_task_4",
    "name":"email_subject_generation",
    "instruction":"Generate an appropriate subjective title for the following email:",
    "instances":[
        {
            "input":"Hi [person name],\n\nI'm writing to ask you if you are happy to be a panelist of our workshop on multimodality at CVPR. The workshop will be held on June 20, 2023. \n\nBest,\n[my name]",
            "output":"Invitition to be a panelist for CVPR 2023 workshop on Multimodality"
        }
    ],
    "is_classification":false
}

作者在项目工程中列出了种子任务的数据分布,内圈是任务的核心动词,外卷是具体的任务目标,如下图所示:
在这里插入图片描述

使用的prompt如下:

You are asked to come up with a set of 20 diverse task instructions. These task instructions will be given to a GPT model and we will evaluate the GPT model for completing the instructions.

Here are the requirements:
1. Try not to repeat the verb for each instruction to maximize diversity.
2. The language used for the instruction also should be diverse. For example, you should combine questions with imperative instrucitons.
3. The type of instructions should be diverse. The list should include diverse types of tasks like open-ended generation, classification, editing, etc.
4. A GPT language model should be able to complete the instruction. For example, do not ask the assistant to create any visual or audio output. For another example, do not ask the assistant to wake you up at 5pm or set a reminder because it cannot perform any action.
5. The instructions should be in English.
6. The instructions should be 1 to 2 sentences long. Either an imperative sentence or a question is permitted.
7. You should generate an appropriate input to the instruction. The input field should contain a specific example provided for the instruction. It should involve realistic data and should not contain simple placeholders. The input should provide substantial content to make the instruction challenging but should ideally not exceed 100 words.
8. Not all instructions require input. For example, when a instruction asks about some general information, "what is the highest peak in the world", it is not necssary to provide a specific context. In this case, we simply put "<noinput>" in the input field.
9. The output should be an appropriate response to the instruction and the input. Make sure the output is less than 100 words.

List of 20 tasks:

prompt包含3个部分:

  1. 指定任务:要求模型输出20种不同任务的指令,并写明了这些生成任务指令将会输入给GPT模型来评估模型效果
  2. 任务要求:共计9个要求:
    1. 动词不能重复,以提高多样性
    2. 使用的语言尽可能不同,应该将问题以祈使句指令的形式进行组合
    3. 任务类型必须不同,如开放性问题,分类,编辑等等
    4. 指令必须能被GPT执行,不能要求模型输出任何图像或者音频,也不能要求模型执行类型于明天5点叫我起床这种做动作任务
    5. 指令必须是英文的
    6. 指令必须仅有一两句话那么长,仅允许祈使句或者疑问句
    7. 应该为指令生成合适的input,input需要包含一个具体的任务示例,并且包含真实的数据而不包含简单的占位符。input需要尽可能提供大量的信息但又不能太长
    8. 不是所有任务都需要input,例如“世界最高峰是什么”这样的生成式任务。在input不是必要的情况下,仅仅在input字段填充""
    9. output必须要是一个恰当的回复,需要保证不超过100个单词
  3. 示例的20个任务

作者在每一个提示词后面,拼接了20个任务做为示例,使用OpenAI的API调用text-davinci-003生成任务指令,最终结果是基于175条种子数据,生成了52000不同的指令数据(笔者任务该过程中应该有机器筛选甚至人工筛选),花费了500美元。

LLaMA 7B指令精调

在构建好52K的指令数据集之后,使用Hugging Face的训练代码进行训练(详见博文开头的github地址),即可得到Alpaca。原作者在4张A800上以FDSP的full_shard模式训练,花费了100美元。

训练指定超参数如下:

HyperparameterLLaMA-7BLLaMA-13B
Batch size128128
Learning rate2e-51e-5
Epochs35
Max length512512
Weight decay00

原作者使用的python版本是3.10,示例训练命令如下:

torchrun --nproc_per_node=4 --master_port=<your_random_port> train.py \
    --model_name_or_path <your_path_to_hf_converted_llama_ckpt_and_tokenizer> \
    --data_path ./alpaca_data.json \
    --bf16 True \
    --output_dir <your_output_dir> \
    --num_train_epochs 3 \
    --per_device_train_batch_size 4 \
    --per_device_eval_batch_size 4 \
    --gradient_accumulation_steps 8 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 2000 \
    --save_total_limit 1 \
    --learning_rate 2e-5 \
    --weight_decay 0. \
    --warmup_ratio 0.03 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --fsdp "full_shard auto_wrap" \
    --fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \
    --tf32 True

也可以将LLaMA替换为OPT-6.7B来进行训练:

torchrun --nproc_per_node=4 --master_port=<your_random_port> train.py \
    --model_name_or_path "facebook/opt-6.7b" \
    --data_path ./alpaca_data.json \
    --bf16 True \
    --output_dir <your_output_dir> \
    --num_train_epochs 3 \
    --per_device_train_batch_size 4 \
    --per_device_eval_batch_size 4 \
    --gradient_accumulation_steps 8 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 2000 \
    --save_total_limit 1 \
    --learning_rate 2e-5 \
    --weight_decay 0. \
    --warmup_ratio 0.03 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --fsdp "full_shard auto_wrap" \
    --fsdp_transformer_layer_cls_to_wrap 'OPTDecoderLayer' \
    --tf32 True

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

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

相关文章

Android Studioc打印+查看日志

目录 Logcat 写入日志 查看应用日志 Logcat 写入日志 错误&#xff1a;Log.e(String, String)警告&#xff1a;Log.w(String, String)信息&#xff1a;Log.i(String, String)调试&#xff1a;Log.d(String, String)详细程度&#xff1a;Log.v(String, String) 查看应用日志 …

IT老齐的Redis 6实战课

NoSQL与Redis6新特性 什么是Redis REDIS一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库&#xff0c;并提供多种语言的 APIRedis的读写性能非常好,官方压测数据为 Redis能读的速度是110000次/s,写的…

【MySQL系列】MySQL的用户管理

「前言」文章内容大致是MySQL的用户管理。 「归属专栏」MySQL 「主页链接」个人主页 「笔者」枫叶先生(fy) 目录 一、用户管理1.1 用户信息1.2 创建新用户1.3 删除用户1.4 修改用户密码 二、数据库的权限2.1 给用户授权2.2 回收用户权限 一、用户管理 MySQL与Linux类似&#x…

图形学线性代数

最近学图形学&#xff01;学的是GAMES101-现代计算机图形学入门-闫令琪↓会做点笔记 Lecture 03 Transformation_哔哩哔哩_bilibili 点乘 1&#xff09;主要作用是找到两个方向/向量的夹角 2&#xff09;找一个向量投影到另一个向量 作用 叉乘 此处x,y,z都是坐标轴 作用 1&a…

HTML+CSS画一个卡通中秋月饼

HTMLCSS画一个卡通中秋月饼&#x1f96e;&#x1f96e;&#x1f96e; 中秋活动水个文章 整个divcss实现个月饼&#xff0c;给前端初学者一个练手的demo 效果图 思路 HTMl 先来个轮廓画脸上的东西&#xff1a;眼睛、眉毛、腮红、嘴巴眼睛丰富下瞳孔画20个花瓣 CSS 轮廓是要外…

4G版本云音响设置教程腾讯云平台版本

文章目录 4G本云音响设置教程介绍一、申请设备三元素1.腾讯云物联网平台2.创建产品3.设置产品参数4.添加设备5.获取三元素 二、设置设备三元素1.打开MQTTConfigTools2.计算MQTT参数3.使用USB连接设备4.设置参数 三、腾讯云物联网套件协议使用说明1.推送协议信息2.topic规则说明…

WebGL 根据模型矩阵的逆转置矩阵计算运动物体的光照效果

目录 前言 坐标变换引起法向量变化 变化规律&#xff1a; 魔法矩阵&#xff1a;逆转置矩阵 逆转置矩阵的用法总结 Matrix4对象的 setInverseOf 、transpose 方法规范&#xff08;以完成逆转置矩阵&#xff09; 示例代码&#xff08;LightedTranslatedRotatedCube.js&am…

虚拟机Ubuntu操作系统常用终端命令(2)(详细解释+详细演示)

本篇概要 本篇讲述了Ubuntu操作系统常用的几个功能&#xff0c;即超级用户&#xff0c;虚拟机系统损坏如何修复&#xff0c;用户和组&#xff0c;如何以root登录界面以及文件的权限方面的知识。希望能够得到大家的支持。 文章目录 本篇概要1.超级用户1.1使用超级用户1.2切换到…

【ICer必备基础】MOS电容——电容电压特性详解

【ICer必备基础】MOS电容——电容电压特性详解 1相关定义2MOS电容描述3MOS电容能带分析4可变电容实际应用 1相关定义 MOS电容是集成电路中非常重要的应用&#xff0c;器件电容的定义为&#xff1a; 阈值反型点&#xff1a; 当达到最大耗尽宽度且反型层电荷密度为零时的情形。此…

在Windows上无法使用TortoiseSVN等工具管理WSL2中的代码的问题

环境 Windows 11 WSL2&#xff08;Ubuntu 22.04&#xff09; 前言 众所周知&#xff0c;WSL2 的跨系统IO读写性能非常差&#xff08;详情见我之前写的这篇文章&#xff09;&#xff0c;而我的代码又是在 WSL2 中运行的&#xff0c;为了提高性能&#xff0c;所以我的代码也必…

2023 Google 开发者大会:无障碍游戏体验升级、安卓开发人员生产力爆棚

目录 前言 一、会说话的狗 - “大黄” 二、GameFace -联合《荒野行动》&#xff0c;提升无障碍游戏体验 三、Android Studio Bot-解放开发人员生产力 五、Purnima致中国开发者的一封“信” 结语 &#x1f388;个人主页&#xff1a;库库的里昂 &#x1f390;CSDN新晋作者 …

9. 原型模式

引言 关键在于具备clone函数&#xff1b;克隆自身。 原型模式&#xff08;Prototype&#xff09;&#xff0c;用原型实例指定创建对象的种类&#xff0c;并且通过拷贝这些原型创建新的对象。 UML 测试代码&#xff1a; #include <iostream> using namespace std;class …

Python 之plt.plot()的介绍以及使用

文章目录 介绍代码实例 介绍 plt.plot() 是Matplotlib库中用于绘制线图&#xff08;折线图&#xff09;的主要函数之一。它的作用是将一组数据点连接起来&#xff0c;以可视化数据的趋势、关系或模式。以下是 plt.plot() 的详细介绍&#xff1a; plt.plot(x, y, fmt, **kwarg…

sun.security.validator.ValidatorException: PKIX path building failed

问题说明 在A系统使用HttpPost调用B系统的接口时报&#xff0c;B系统的ssl证书使用的是阿里云免费ssl证书&#xff0c;很郁闷调用别的系统都没有出现过这样的问题。 sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.Su…

ChatGLM-6b的微调与推理

基于ChatGLM-6B的推理与部署 1.使用git clone命令ChatGLM项目地址&#xff0c;将项目clone到本地。 2.下载ChatGLM-6B模型文件 【注意】运行下面代码的时候&#xff0c;要将源代码中的模型文件路径改成自己的地址&#xff0c;不然会报错&#xff01;&#xff01;&#xff01;…

destoon自定义一个archiver内容文档

在archiver目录建立以下代码&#xff1a; <?php define(DT_REWRITE, true); require ../common.inc.php; $EXT[archiver_enable] or dheader(DT_PATH); //$DT_BOT or dheader(DT_PATH); $N $M $T array(); $mid or $mid 5; $vmid $list 0; foreach($MODULE as $k>…

微服务保护-授权规则

个人名片&#xff1a; 博主&#xff1a;酒徒ᝰ. 个人简介&#xff1a;沉醉在酒中&#xff0c;借着一股酒劲&#xff0c;去拼搏一个未来。 本篇励志&#xff1a;三人行&#xff0c;必有我师焉。 本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》&#xff0c;SpringCloud…

【JUC】Java并发编程从挖坑到入土全解(2)

目录 我们锁的到底是什么&#xff08;8个案例&#xff09; 案例1 案例2 案例3 案例4 案例5 案例6 案例7 案例8 总结 我们锁的到底是什么&#xff08;8个案例&#xff09; 有a、b两个线程&#xff0c;我们基于如下代码进行改造&#xff1a; public static void main…

数据结构---二叉搜索树

二叉搜索树 二叉搜索树什么是二叉搜索树&#xff1f; 二叉搜索树的操作查找插入删除 源代码非递归版 二叉搜索树 什么是二叉搜索树&#xff1f; 二叉搜索树(Binary Search Tree 简称BST)又称二叉排序树&#xff0c;是一种二叉树的特殊形式&#xff0c;它在每个节点上存储的键…

动态规划-货币问题

动态规划-货币问题 题目一 arr是货币数组&#xff0c;其中的值都是正数。再给定一个正数aim。每个值都认为是一张货币&#xff0c;即便是值相同的货币也认为每一张都是不同的&#xff0c;返回组成aim的方法数。例如 : arr { 1,1,1 }&#xff0c;aim 2&#xff0c;第0个和第…