首页 > 其他分享 >创新实训 (十)大模型微调

创新实训 (十)大模型微调

时间:2024-06-23 23:55:00浏览次数:3  
标签:box candies 模型 微调 Tanya ----- 实训 eat

1.数据部分

CodeGeeX 基于 ChatGLM 基座语言模型,所以需要关注 ChatGLM 的训练数据。

训练数据为jsonl格式,每一行的数据格式如下,其中chat_rounds字段是必需的,可以根据实际需求添加或删除其他字段。

在本项目当中,我们更加关注的是模型的单轮对话能力,所以只需要单轮对话数据。

推理数据格式为模型在训练数据格式下拼接的字符串形式,它也是推理时输入prompt拼接的方式:

<s>system
这是System指令
<s>human
这是第1轮用户输入的问题
<s>bot
这是第1轮模型生成的内容{EOS_TOKEN}
<s>human
这是第2轮用户输入的问题
<s>bot
这是第2轮模型生成的内容{EOS_TOKEN}
...
...
...
<s>human
这是第n轮用户输入的问题
<s>bot
{模型现在要生成的内容}{EOS_TOKEN}

将数据处理成以上的格式:

还需要知道 {EOS_TOKEN} 的值,可以直接通过 tokenizer 的 eos_token 属性查看:

得知 {EOS_TOKEN} 为 </s> ,将数据进一步进行处理:

最终数据为

'<s>human\nYou are an intelligent code assistant who can respond to users\' questions related to codeThere are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values \u200b\u200b— red,green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$).\n\nInitially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second.\n\nIf Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled.\n\nIt is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya\'s appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one.\n\nNote that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies.\n\nTanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements.\n\n\n-----Input-----\n\nThe first line contains three integers $n$, $s$ and $k$ ($1 \\le n \\le 50$, $1 \\le s \\le n$, $1 \\le k \\le 2000$) — number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \\le r_i \\le 50$) — numbers of candies in the boxes. The third line contains sequence of $n$ letters \'R\', \'G\' and \'B\', meaning the colors of candies in the correspondent boxes (\'R\' for red, \'G\' for green, \'B\' for blue). Recall that each box contains candies of only one color. The third line contains no spaces.\n\n\n-----Output-----\n\nPrint minimal number of seconds to eat at least $k$ candies. If solution doesn\'t exist, print "-1".\n\n\n-----Examples-----\nInput\n5 3 10\n1 2 3 4 5\nRGBRR\n\nOutput\n4\n\nInput\n2 1 15\n5 6\nRG\n\nOutput\n-1\n\n\n\n-----Note-----\n\nThe sequence of actions of Tanya for the first example:\n\n  move from the box $3$ to the box $2$;  eat candies from the box $2$;  move from the box $2$ to the box $3$;  eat candy from the box $3$;  move from the box $3$ to the box $4$;  move from the box $4$ to the box $5$;  eat candies from the box $5$. \n\nSince Tanya eats candy instantly, the required time is four seconds.<s>bot\nINF = 10000000000.0\nmax_n = 50\nmax_k = 2000\n\ndef main():\n\t(n, s, k) = map(int, input().split())\n\ts -= 1\n\tbuf = [\'\'] * (max_n + 1)\n\tdp = [[0 for i in range(max_n + 1)] for j in range(max_k + 1)]\n\tr = list(map(int, input().split()))\n\tc = input()\n\tanswer = INF\n\tfor i in range(len(c)):\n\t\tbuf[i] = c[i]\n\tfor i in range(k, -1, -1):\n\t\tfor j in range(n):\n\t\t\tdp[i][j] = INF\n\tfor j in range(n):\n\t\tvalue = abs(j - s)\n\t\tif k - r[j] <= 0:\n\t\t\tanswer = min(answer, value)\n\t\telse:\n\t\t\tdp[k - r[j]][j] = value\n\tfor i in range(k, 0, -1):\n\t\tfor j in range(n):\n\t\t\tif dp[i][j] < INF:\n\t\t\t\tfor l in range(n):\n\t\t\t\t\tif buf[j] != buf[l] and r[j] < r[l]:\n\t\t\t\t\t\tvalue = dp[i][j] + abs(j - l)\n\t\t\t\t\t\tif i - r[l] <= 0:\n\t\t\t\t\t\t\tanswer = min(answer, value)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tdp[i - r[l]][l] = min(dp[i - r[l]][l], value)\n\tif answer == INF:\n\t\tprint(-1)\n\t\treturn\n\tprint(answer)\n\ndef __starting_point():\n\tmain()\n__starting_point()\n</s>'

2.模型加载

使用 https://huggingface.co/THUDM/codegeex2-6b 中的方法,对模型进行量化加载。

加载之后可以显示一些模型的结构:

我们使用模型微调的主要会是模型中的线性层,我们从中选取出要增加 LoRa 参数的层。

输出的层即为要添加参数的层,均为线性层。

3.使用 PEFT 微调

加载 PEFT 配置,每个 PEFT 方法都需要一个配置,其中包含了指定 PEFT 方法应该如何应用的所有参数。一旦配置设置好了,就将其传递给 get_peft_model() 函数,同时还要传递基础模型,以创建一个可训练的 PeftModel。

从 peft 库中导入 LoraConfig 和 get_peft_model 函数,设置 LoRa 微调的一些超参数 (lora_alpha: LORA 适配器的比例因子;lora_dropout: LORA 适配器的dropout率;lora_r: LORA 适配器的秩)。"CAUSAL_LM",表示这是一个因果语言模型微调任务。

使用 Transformers 库和 TRL 库来进行 SFT 训练,即是有监督的学习,开启了混合精度训练加速训练。

  • out_dir: 模型保存的输出目录
  • per_device_train_batch_size: 每个设备的训练批次大小
  • gradient_accumulation_steps: 梯度累积步数
  • warmup_steps: 学习率预热步数
  • max_steps: 最大训练步数
  • learning_rate: 初始学习率
  • logging_steps: 多少步打印一次日志

对模型进行训练,会得到训练过程中的 Loss 变化:

标签:box,candies,模型,微调,Tanya,-----,实训,eat
From: https://www.cnblogs.com/asuldb/p/18249628

相关文章

  • AI 大模型企业应用实战(10)-LLMs和Chat Models
    1模型来看两种不同类型的模型--LLM和聊天模型。然后,它将介绍如何使用提示模板来格式化这些模型的输入,以及如何使用输出解析器来处理输出。LangChain中的语言模型有两种类型:1.1ChatModels聊天模型通常由LLM支持,但专门针对会话进行了调整。提供者API使用与纯文本补全模......
  • 基于CDMA的多用户水下无线光通信(2)——系统模型和基于子空间的延时估计
      本文首先介绍了基于CDMA的多用户UOWC系统模型,并给出了多用户收发信号的数学模型。然后介绍基于子空间的延时估计算法,该算法只需要已知所有用户的扩频码,然后根据扩频波形的循环移位在观测空间的信号子空间上的投影进行延时估计。1、基于CDMA的多用户UOWC系统模型  首......
  • 搭建yolov8模型训练的环境_制作docker镜像_模型训练
    搭建一个能进行yolov8模型训练的环境,包括CUDA11.x、cuDNN8.x、Ubuntu18.04、Python3.8、Cython、NumPy、PyTorch、YOLOv8、Ultralytics等依赖(其实ultralytics 包会包含YOLOv8及其相关工具)。在Docker容器中,不需要创建虚拟环境,每个容器本身就像是一个隔离的环境,所以可以直......
  • 创新实训(十)——代码美化部分:导航栏的active
    代码美化部分————导航栏的active对于导航栏来说,当选定在某个功能部分时,当前模块会有高亮显示。查看main-nav.php中有管导航栏的代码<divclass="collapsenavbar-collapse"id="navbarSupportedContent"> <ulclass="navnav-pillsmain-navmr-auto"> <liclass="......
  • Vienna 整流器的基本原理及数学模型
    2.1Vienna整流器基本工作原理2.1.1主电路拓扑结构分析Vienna整流器系统的主电路包含用于升压的三相电感、三相桥臂和两个直流侧均压电容。通过有规律的对双向开关进行控制不仅能实现功率双向流动,还能使网侧电流时刻跟踪电网电压,使系统运行在高功率因数状态下。拓扑如图2......
  • AI 大模型企业应用实战(07)-LangChain的Hello World项目
    pipinstall--upgradelangchain==0.0.279-ihttps://pypi.org/simple1创建一个LLM自有算力平台+开源大模型(需要有庞大的GPU资源)企业自己训练数据第三方大模型API(openai/百度文心/阿里通义千问...)数据无所谓让LLM给孩子起具有中国特色的名字。在LangChain中最基本的功......
  • AI 大模型企业应用实战(06)-初识LangChain
    LLM大模型与AI应用的粘合剂。1langchain是什么以及发展过程LangChain是一个开源框架,旨在简化使用大型语言模型构建端到端应用程序的过程,也是ReAct(reason+act)论文的落地实现。2022年10月25日开源54K+star种子轮一周1000万美金,A轮2500万美金11个月里累计发布200多次,提交4......
  • javascript浏览器对象模型
    BOM对象:BOM是浏览器对象模型的简称。JavaScript将整个浏览器窗口按照实现的功能不同拆分成若干个对象;包含:window对象、history对象、location对象和document对象等window对象:常用方法:1.prompt();显示可提示用户输入的对话框window.prompt("请输入您的名字");返......
  • 【Matlab】CNN-LSTM分类 卷积神经网络-长短期记忆神经网络组合模型(附代码)
     资源下载: https://download.csdn.net/download/vvoennvv/89466499分类算法资源合集:https://download.csdn.net/download/vvoennvv/89466519目录MatlabSVM支持向量机分类算法MatlabRF随机森林分类算法MatlabRBF径向基神经网络分类算法MatlabPSO-BP基于粒子群算法......
  • 【06】数据模型和工作量证明-工作量证明
    1.工作量证明的背景比特币是通过工作量证明来竞争记账权,并获得比特币奖励。简单来讲就是谁能够根据区块数据更快的计算得到满足条件的哈希值,谁就可以胜出,这个块才会被添加到区块链中。我们把这个过程称为挖矿。比特币每10分钟产生1个区块。2.工作量证明算法1.获取区块头......