你也想要这样的赛博女友吗?
这几年大模型发展遍地开花,让我这种不懂深层次原理的也可以感受AI带来的美好,花了几天时间训练完成,这个教程手把手教你训练出属于自己的AI女友
- 1.首先是准备硬件和环境
最好是30系或者40系,我是云服务器租的4090,也很便宜,如果你是本地环境可以参考,这里只讲linux系统,其他系统需要自己摸索
云服务商:passport.compshare.cn/register?re… 注册之后,选择部署实例,这里我选择添加平台已有镜像,这里选择LlamaFactory微调环境,但是其他的也没什么太大区别,因为他这个环境不是最新的,还要进行更新
右边输入磁盘200GB,选择部署
部署好了以后点击jupyterlib,不用登录,这是一个可视化的编辑器,非常方便友好
打开之后新建一个终端
这个编辑器默认打开是/app,有些不习惯,一般我习惯打开/root,
所以需要自己修改,在终端输入
jupyter --config-dir查看目录,调用jupyter notebook--generate-config
生成config文件
打开这个目录,vim这个文件
按i进入编辑模式,在最开始处添加
c.NotebookApp.notebook_dir = '/root'
按Esc退出编辑模式,输入:wq保存退出
重启生效
- 2.下载微调框架LlamaFactory以及大模型
这里是详细的文档:llamafactory.readthedocs.io/zh-cn/lates…
由于基础环境是配好的,首先要移动到/root目录而不是app目录,只需要做这几步:
git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git
conda create -n llama_factory python=3.10
conda activate llama_factory
cd LLaMA-Factory
pip install -e ".[torch,metrics]"
安装完之后输入python,然后输入以下代码:
import torch
torch.cuda.current_device()
torch.cuda.get_device_name(0)
torch.__version__
再调
llamafactory-cli train -h
没问题就可以继续了
有可能显示找不到torch什么的,需要自己安装torch相关库
模型下载: 可以去huggingface下载或者modelscope(魔搭社区), 这里下载的是Glm4-9b-chat-hf:hf-mirror.com/THUDM/glm-4…
你也可以下载别的大模型,模型支持最好是LlamaFactory支持模型列表里,列表是:github.com/hiyouga/LLa… 没有列表里应该也可以的,这个自己尝试吧
如果你是在国内,你也可以用镜像环境,调用:
export HF_ENDPOINT=https://hf-mirror.com
开始下载,这里我把下载地址设置为/root/models/目录下
huggingface-cli download --resume-download THUDM/glm-4-9b-chat-hf --local-dir /root/models
注意:魔搭需要安装工具链: modelscope.cn/docs/intro/…
- 3.数据集准备
这里选的魔搭的沐雪:modelscope.cn/datasets/Mo…
跟着流程:
下载好之后移到/root/下 这里的数据有点少只有1000多条,可以自己多复制几倍然后打乱,具体步骤:
打开数据集目录cd Muice-Dataset/
这里有几种数据:
train:沐雪的主要交流数据
self_cognition:身份强化
wikihow:wiki
COIG-CQIA:弱智吧
我选择train和self_cognition来打乱
awk 'NR > 1 && NF == 0 {next} {print}' train.jsonl train.jsonl train.jsonl self_cognition.jsonl> repeated-data.jsonl
上面命令是比例看得出来是3:1,我真实输入的是20:10,你可以根据自己的喜好调节比例
然后打乱调用:
shuf repeated-data.jsonl > shuffled-data.jsonl
我们需要把jsonl转为我们需要的格式
目前就支持两种,详见:llamafactory.readthedocs.io/zh-cn/lates…
这里我选择的是Alpaca,格式如下:
{
"instruction": "计算这些物品的总费用。 ",
"input": "输入:汽车 - $3000,衣服 - $100,书 - $20。",
"output": "汽车、衣服和书的总费用为 $3000 + $100 + $20 = $3120。"
},
转换的python代码参考:
import json as JSON
import os
def jsonl_to_json(csv_folder, json_file):
dict_list = []
for csvfile in os.listdir(csv_folder):
if not csvfile.endswith('.jsonl'):
continue
csvfile_path = os.path.join(csv_folder, csvfile)
with open(csvfile_path, 'r', encoding='utf-8') as f:
jsonl_data = f.readlines()
for line in jsonl_data:
line_dict = JSON.loads(line)
# del line_dict['history']
if 'respond' in line_dict:
line_dict['output'] = line_dict.pop('respond')
dict_list.append(line_dict)
with open(json_file, 'w', encoding='utf-8') as f:
JSON.dump(dict_list, f, indent=4, ensure_ascii=False)
csv_folder = '/root/Muice-Dataset/'
json_file = './data/result.json'
if __name__ == '__main__':
jsonl_to_json(csv_folder, json_file)
你可以找一个文件夹专门存放数据处理相关的代码,比如/root/make_data,然后新建一个txt文件,复制代码,更改后缀即可,再说明一下部分代码,因为有些数据是respond有些数据又是output,所以我们需要手动统一转为output,如图就是不对的:
调用
python /root/make_data/converjson.py
转换后的是在LLaMA-Factory/data/result.json,这里再修改/data/dataset_info.json,这个文件的含义就是指定数据集地址和格式方便大模型来寻找
需要右击文件,打开方式,选择editor,不然无法修改
添加内容:
"muxue": {
"file_name": "result.json",
"columns": {
"prompt": "prompt",
"response":"output"
}
},
再更改微调参数文件examples/train_lora/llama3_lora_sft.yaml,这里我自己单独写了一个.yaml,内容修改如下:
### model
model_name_or_path: /root/models/glm4-9b-chat-hf
trust_remote_code: true
### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all
### dataset
dataset: muxue
template: glm4
cutoff_len: 1024
max_samples: 1000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: saves/glm4/lora/sft/
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true
### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 5.0e-5
num_train_epochs: 3
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000
### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500
你肯定不知道这些参数什么含义,详情看:llamafactory.readthedocs.io/zh-cn/lates… 也可以自己去搜集相关资料,不是很难理解
- 开始训练
在训练之前,我们什么都不做就跟大模型聊天了,体验一下也不错,调用:
llamafactory-cli chat --model_name_or_path /root/models/glm4-9b-chat-hf/ --template glm4
然后:
输入exit退出,然后开始训练吧,调用:
llamafactory-cli train examples/train_lora/llama3_lora_sft.yaml
跑完了之后调用:
llamafactory-cli chat --model_name_or_path /root/models/glm4-9b-chat-hf/ --template glm4 --adapter_name_or_path ./saves/glm4/lora/sft/ --finetuning_type lora
到这里你已经训练属于自己的ai女友了,下一章节我们再见