1.准备中文数据集,例如从huggingface上下载开源数据集
https://huggingface.co/datasets/BelleGroup/train_1M_CN/resolve/main/Belle_open_source_1M.json
参考以下数据格式生成数据集(你想让模型擅长啥你就多灌入啥,跟“养娃”一样不断地练成“牛人”。也可以采用斯坦福套路调用ChatGPT生成答案):
{
"instruction":"帮助用户解决以下数学问题。\n问题: 计算 45 ÷ 5 6 × 2",
"input":"",
"output":"根据算术运算法则, 先计算乘除再计算加减:\n45 ÷ 5 6 × 2 = 9 12 = 21"
}
2.采用LLaMA-7B转换为huggingface的模型decapoda-research/llama-7b-hf(https://huggingface.co/decapoda-research/llama-7b-hf)
3.生成训练配置文件 Llama_config.json
{
"model_type": "Llama",
"model_name_or_path": "decapoda-research/llama-7b-hf",
"data_path": "data_dir/Belle_open_source_0.5M.json",
"output_dir": "trained_models/Llama",
"batch_size": 8,
"per_device_train_batch_size": 1,
"num_epochs": 2,
"learning_rate": 2e-5,
"cutoff_len": 1024,
"val_set_size": 1000,
"val_set_rate": 0.1,
"save_steps": 1000,
"eval_steps":1000,
"warmup_steps":10,
"logging_steps":10,
"gradient_accumulation_steps":16
}
4.采用lora技术降低成本提升训练速度,生成对应配置文件lora_hyperparams_llama.json
{
"lora_r": 8,
"lora_alpha": 16,
"lora_dropout": 0.05,
"lora_target_modules": [
"q_proj",
"v_proj"
]
}
5.开启微调模式训练
采用10*16G*V100 GPU资源大约"炼"了大约28个小时
torchrun --nproc_per_node=10 finetune.py --model_config_file Llama_config.json --lora_hyperparams_file lora_hyperparams_llama.json --use_lora
训练完成后的模型查看trained_models/Llama目录的,如下图:
6.使用训练后的model生成内容
采用LoRA权重
https://huggingface.co/tloen/alpaca-lora-7b
将自己的数据灌入以下文件,也可以直接用公开数据集
Belle_open_source_0.5M.dev.json
进行内容生成
执行命令:
python generate.py --dev_file data_dir/Belle_open_source_0.5M.dev.json --model_name_or_path trained_models/Llama/ --lora_weights trained_models/lora-llama
查看生成结果:
参考项目:
https://github.com/tloen/alpaca-lora
https://github.com/LianjiaTech/BELLE
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved