山大软院创新实训之大模型篇(二)——Llama Factory微调Qwen实践 ...

金歌  金牌会员 | 2024-8-16 10:52:59 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 715|帖子 715|积分 2145

山大软院创新实训之大模型篇(二)——Llama Factory微调Qwen实践

LLaMA Factory 是一个用于微调和摆设 LLaMA (Large Language Model Applications) 模型的框架。它旨在简化大语言模型的使用和管理,提供了强盛的工具来支持从模型训练、微调到摆设的整个流程。
固然基于LLaMA,但LLaMA Factory也提供了许多其他领域大模型的微调功能。
我本想基于ChatGLM举行微调,但微调过程中有部门总是报错,所以我改为基于Qwen举行微调。
起首,我安装并运行了llama factory。
  1. git clone https://github.com/hiyouga/LLaMA-Factory.git
  2. cd LLaMA-Factory
  3. pip install -r requirements.txt
  4. pip install transformers_stream_generator bitsandbytes tiktoken auto-gptq optimum autoawq
  5. pip install --upgrade tensorflow
  6. pip uninstall flash-attn -y
  7. CUDA_VISIBLE_DEVICES=0 USE_MODELSCOPE_HUB=1 python src/webui.py
复制代码

根据格式构造数据集,数据集部门内容如下。llama factory微调Qwen时,要求的数据集格式如下:
  1. [
  2.     {
  3.         "instruction": "用户指令(必填)",
  4.         "input": "用户输入(选填)",
  5.         "output": "模型回答(必填)",
  6.         "system": "系统提示词(选填)",
  7.         "history": [
  8.             ["第一轮指令(选填)", "第一轮回答(选填)"],
  9.             ["第二轮指令(选填)", "第二轮回答(选填)"]
  10.         ]
  11.     }
  12. ]
复制代码
参考Huatuo-GPT微调ChatGLM的数据集,我制作数据集格式如下,样本如下:
  1. [
  2.       {
  3.             "instruction": "If you are a doctor, please answer the medical questions based on the patient's description.",
  4.             "input": "Doctor, I have been experiencing sudden and frequent panic attacks. I don't know what to do.",
  5.             "output": "Well, based on what you're telling me, it sounds like you may be suffering from panic disorder. The best course of action is to start with psychotherapy and mental health counseling. Additionally, we should conduct an electrocardiogram to make sure that there are no physical issues causing your panic attacks. We will also need to perform a depression screen and a toxicology screen to rule out any other underlying causes. Finally, I would recommend a comprehensive psychological and psychiatric evaluation and therapy to help manage your symptoms."
  6.       },
  7.       {
  8.             "instruction": "If you are a doctor, please answer the medical questions based on the patient's description.",
  9.             "input": "Doctor, I have been experiencing a hoarse voice for a few weeks now and it's not getting any better despite taking medication. What could be the problem?",
  10.             "output": "It's possible that you have a vocal cord polyp. To confirm this, we need to conduct tracheoscopy and laryngoscopy with a biopsy. We may also need to conduct an occupational therapy assessment such as speech therapy, other diagnostic procedures like an interview or consultation, physical therapy exercises, as well as diagnostic procedures on your nose, mouth, and pharynx to rule out any other underlying conditions. Additionally, we may need to evaluate your eyes and ears for any related conditions, such as ophthalmologic and otologic diagnosis and treatment."
  11.       },
  12.       {
  13.             "instruction": "If you are a doctor, please answer the medical questions based on the patient's description.",
  14.             "input": "Hi, Doctor. I think I might have Turner syndrome. ",
  15.             "output": "Okay, in that case, you will need to undergo a complete physical skin exam, an ultrasonography, and some other diagnostic procedures such as an interview, evaluation, and consultation. Additionally, we'll need to perform an echocardiography, examine your breast, and check your eyes and ears for any issues. We'll also do a depression screening to assess your mental health."
  16.       },
  17.       {
  18.             "instruction": "If you are a doctor, please answer the medical questions based on the patient's description.",
  19.             "input": "Doctor, I've been experiencing a condition called cryptorchidism. My testicles have not descended properly into the scrotum. What medical tests do I need to take?",
  20.             "output": "You will need to undergo a complete physical skin exam to see the extent of the undescended testicles. Then, we need to conduct Ultrasonography (Ultrasound) to find out the exact location of the testicles. After that, a pelvis exam and rectal examination will be done to determine if the testicles have descended into the pelvic region. If not, then other OR therapeutic procedures related to male genital or nervous system procedures may be required. We will also do an occupational therapy assessment to assess your speech therapy."
  21.       },
  22.       ...
  23. ]
复制代码

我的预计工作流程如下:先准备好训练和验证数据集,举行必要的数据预处置惩罚和格式转换。然后选择合适的预训练模型,配置训练参数和超参数。使用 LLaMA Factory 的训练脚本和工具举行模型训练和微调,监控训练过程中的性能指标。对训练好的模型举行评估和验证,确保模型在验证集上的表现符合预期。最后将微调好的模型摆设到实际应用中,举行推理和预测。
需要现在llama factory中计算数据集存放路径的sha值并放入data文件中,计算sha值的python程序如下。通过逐块读取文件内容,计算文件的SHA-1哈希值,并在文件不存在时处置惩罚非常。它适用于需要验证文件完整性或唯一性的场景。
  1. import hashlib
  2. def calculate_sha1(file_path):
  3.     sha1 = hashlib.sha1()
  4.     try:
  5.         with open(file_path, 'rb') as file:
  6.             while True:
  7.                 data = file.read(8192)  # Read in chunks to handle large files
  8.                 if not data:
  9.                     break
  10.                 sha1.update(data)
  11.         return sha1.hexdigest()
  12.     except FileNotFoundError:
  13.         return "File not found."
  14. file_path = './Desktop/self_cognition.json'
  15. sha1_hash = calculate_sha1(file_path)
  16. print("SHA-1 Hash:", sha1_hash)
复制代码
举行微调,设置微调参数。我的主要微调参数设置如下:参数设置

  • 训练参数

    • batch_size:32
    • learning_rate:2e-5(通常用于微调的初始学习率,可以根据需要调整)
    • num_train_epochs:3-5(根据数据集大小和模型的收敛情况调整)
    • max_seq_length:512(根据数据和模型的本事设置)
    • gradient_accumulation_steps:2-4(用于有效地增大批次大小)

  • 优化器和调理器

    • optimizer:AdamW(适用于大多数NLP任务)
    • weight_decay:0.01(防止过拟合)
    • learning_rate_scheduler:线性调理器(学习率随训练过程渐渐减小)

  • 其他参数

    • warmup_steps:500(在训练初期徐徐增长学习率)
    • logging_steps:50(日记记录频率)
    • save_steps:500(保存查抄点的频率)
    • evaluation_strategy:steps(评估频率)



使用llama-factory直接chat,测试微调结果。可以看出,我所微调过的模型对于医疗问答特定任务,结果比原Qwen结果好。

参考博客:https://blog.csdn.net/weixin_44480960/article/details/137092717

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表