基于huggingface的Dataset加载Lora的数据集预处理

其中可供参考的json配置,比如我的数据是以下这样的:

[
    {
        "instruction": "你的问题",
        "output": "大模型的答案。"
    },
    {
        "instruction": "你的问题第二个",
        "output": "大模型对应的答案。"
    }
]

datasets加载预处理过程,我的数据是json格式

from datasets import load_dataset
 
dataset = load_dataset("json", data_files="你的数据.json", split=["train","test"])
 
def Preprocess(example):
    MAX_LEN = 128
    input_ids, attention_mask, labels = [], [], []
    instruction = tokenizer(f"User: {example['instruction']}Assistant: ", add_special_tokens=False)
    response = tokenizer(example["output"] + tokenizer.eos_token, add_special_tokens=False)
    input_ids = instruction["input_ids"] + response["input_ids"]
    attention_mask = instruction["attention_mask"] + response["attention_mask"]
    labels = [-1] * len(instruction["input_ids"]) + response["input_ids"]
    if len(input_ids) > MAX_LEN:
        input_ids = input_ids[:MAX_LEN]
        attention_mask = attention_mask[:MAX_LEN]
        labels = labels[:MAX_LEN]
    return {
        "input_ids": input_ids,
        "attention_mask": attention_mask,
        "labels": labels
    }
 
dataset = dataset.map(Preprocess, remove_columns=dataset.column_names).shuffle()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值