首页 > 其他分享 >medical custom dataset for fine-tuning llama2

medical custom dataset for fine-tuning llama2

时间:2023-11-29 09:23:46浏览次数:43  
标签:prompt tuning -- medical llama2 dataset path csv

data preparation

we use huggingface shibin6624/medical to fine-tuning llama2, please note that this dataset is consist of en and cn data, here we just use en data.

dataset structure

 now we download and load dataset, then save them into train.csv, validation.csv and test.csv.

from datasets import load_dataset
import os


dataset = load_dataset("shibing624/medical", "finetune")


save_path = "../medical"
os.makedirs(save_path, exist_ok=True)  


dataset['train'].to_csv(os.path.join(save_path, 'train.csv'), index=False)
dataset['validation'].to_csv(os.path.join(save_path, 'validation.csv'), index=False)
dataset['test'].to_csv(os.path.join(save_path, 'test.csv'), index=False)

then we split English part from their name as test_en.csv, validation_en.csv, train_en.csv, shown as bellow.

 

 

change code 

in repository llama2-tutorial, replace the dataset.py as the following code

def get_preprocessed_medical(dataset_config, tokenizer, split):
    if split == "train":
        data_path = "../dataset/medical/train_en.csv"
    elif split == "validation":
        data_path = "../dataset/medical/validation_en.csv"
    elif split == "test":
        data_path = "../dataset/medical/test_en.csv"

    dataset = datasets.load_dataset(
        "csv",
        data_files={split: "../dataset/medical/train_en.csv"}
    )[split]

    prompt = (
        f"answer the question in instruction:\n{{instruction}}\n---\noutput:\n"
    )

    def apply_prompt_template(sample):
        return {
            "prompt": prompt.format(instruction=sample["instruction"]),
            "output": sample["output"],
        }

    dataset = dataset.map(apply_prompt_template, remove_columns=list(dataset.features))

    def tokenize_add_label(sample):
        prompt = tokenizer.encode(tokenizer.bos_token + sample["prompt"], add_special_tokens=False)
        answer = tokenizer.encode(sample["output"] + tokenizer.eos_token, add_special_tokens=False)

        sample = {
            "input_ids": prompt + answer,
            "attention_mask": [1] * (len(prompt) + len(answer)),
            "labels": [-100] * len(prompt) + answer,
        }

        return sample

    dataset = dataset.map(tokenize_add_label, remove_columns=list(dataset.features))

    return dataset

 

clone llama-recipes repository tied with llama2-tutorial, here is the directory structure, no matter where you put your data, but needs to be specified in your dataset.py code

 fine tuning

run the following code under llama2-tutorial folder.

python -m llama_recipes.finetuning \
    --use_peft \
    --peft_method lora \
    --quantization \
    --model_name ./llama/models_hf/7B \
    --dataset custom_dataset \
    --custom_dataset.file "dataset.py:get_preprocessed_medical" \
    --output_dir ../llama/fine-tuning/medical \
    --batch_size_training 1 \
    --num_epochs 3 

 

 

  

 

reference

1 llama2-tutorial: https://github.com/mmdatong/llama2-tutorials/tree/v1.0

2 llama-recipes: https://github.com/facebookresearch/llama-recipes/tree/main

3 llama: https://github.com/facebookresearch/llama

 

标签:prompt,tuning,--,medical,llama2,dataset,path,csv
From: https://www.cnblogs.com/ldzbky/p/17863734.html

相关文章

  • 快速上手llama2.c
    title:快速上手llama2.cbanner_img:https://github.com/karpathy/llama2.c/blob/master/assets/llama_cute.jpgdate:2023-7-2516:19:00tags:-踩坑快速上手llama2.cllama2.c一个完整的解决方案,可以使用PyTorch从头开始训练的Llama2LLM(LightweightLanguageModel)模型......
  • 快速上手llama2.c(更新版)
    title:快速上手llama2.c(更新版)banner_img:https://github.com/karpathy/llama2.c/blob/master/assets/llama_cute.jpgdate:2023-7-2816:31:00tags:-踩坑快速上手llama2.c(更新版)在上一次我同时在我的博客和知乎发布了快速上手llama2.c之后,我一个小透明也收获了不......
  • 全新Self-RAG框架亮相,自适应检索增强助力超越ChatGPT与Llama2,提升事实性与引用准确性
    全新Self-RAG框架亮相,自适应检索增强助力超越ChatGPT与Llama2,提升事实性与引用准确性1.基本思想大型语言模型(LLMs)具有出色的能力,但由于完全依赖其内部的参数化知识,它们经常产生包含事实错误的回答,尤其在长尾知识中。为了解决这一问题,之前的研究人员提出了检索增强生成(RAG),它通......
  • 什么是人工智能领域的 SFT - Supervised Finetuning
    在人工智能(AI)领域,SupervisedFinetuning是一个重要的概念。它涉及到在预训练模型的基础上,利用有标签的数据进行微调,以适应特定的任务或领域。这个过程可以被视为在更广泛的知识基础上进行特定任务的训练,从而在新任务上获得更好的性能。SupervisedFinetuning这个名词可以被分解......
  • 论文精读:用于少样本目标检测的元调整损失函数和数据增强(Meta-tuning Loss Functions a
    论文链接:Meta-TuningLossFunctionsandDataAugmentationforFew-ShotObjectDetectionAbstract现阶段的少样本学习技术可以分为两类:基于微调(fine-tuning)方法和基于元学习(meta-learning)方法。基于元学习的方法旨在学习专用的元模型,使用学到的先验知识处理新的类,而基于微......
  • TALLRec: An Effective and Efficient Tuning Framework to Align Large Language Mod
    目录概TallRec代码BaoK.,ZhangJ.,ZhangY.,WangW.,FengF.andHeX.TALLRec:Aneffectiveandefficienttuningframeworktoalignlargelanguagemodelwithrecommendation,2023.概LoRA微调在推荐上的初步尝试.TallRecTallRec实际上就是一种特殊的指令......
  • 通义千问, 文心一言, ChatGLM, GPT-4, Llama2, DevOps 能力评测
    引言“克隆dev环境到test环境,等所有服务运行正常之后,把访问地址告诉我”,“检查所有项目,告诉我有哪些服务不正常,给出异常原因和修复建议”,在过去的工程师生涯中,也曾幻想过能够通过这样的自然语言指令来完成运维任务,如今AI助手Appilot利用LLM蕴藏的神奇力量,将这一切变成了......
  • 通义千问, 文心一言, ChatGLM, GPT-4, Llama2, DevOps 能力评测
    引言“克隆dev环境到test环境,等所有服务运行正常之后,把访问地址告诉我”,“检查所有项目,告诉我有哪些服务不正常,给出异常原因和修复建议”,在过去的工程师生涯中,也曾幻想过能够通过这样的自然语言指令来完成运维任务,如今AI助手Appilot利用LLM蕴藏的神奇力量,将这一切变成......
  • Langchain-Chatchat项目:4.2-P-Tuning v2使用的数据集
      本文主要介绍P-tuning-v2论文中的5种任务,分别为Glue任务、NER任务、QA任务、SRL任务、SuperGlue任务,重点介绍了下每种任务使用的数据集。一.Glue任务  GLUE(GeneralLanguageUnderstandingEvaluation)是纽约大学、华盛顿大学等机构创建了一个多任务的自然语言理解基准和分......
  • 使用 Appilot 部署 Llama2,会聊天就行!
    Walrus是一款基于平台工程理念的应用管理平台,致力于解决应用交付领域的深切痛点。借助Walrus将云原生的能力和最佳实践扩展到非容器化环境,并支持任意应用形态统一编排部署,降低使用基础设施的复杂度,为研发和运维团队提供易用、一致的应用管理和部署体验,进而构建无缝协作的软件交......