当前位置:   article > 正文

peft模型微调_IA3_few-shot parameter-efficient fine-tuning is better

few-shot parameter-efficient fine-tuning is better and cheaper than in-conte

IA3(论文:Few-Shot Parameter-Efficient Fine-Tuning is Better and Cheaper than In-Context Learning),通过学习向量来对激活层加权进行缩放,从而获得更强的性能,同时仅引入相对少量的新参数,如下图左边所示,它的诞生背景是为了改进 LoRA。在这里插入图片描述
为了使微调更有效,IA3(通过抑制和放大内部激活注入适配器)使用学习向量重新调整内部激活。 这些学习到的向量被注入到典型的基于transformer的架构中的attention和feedforward模块中。 原始权重保持冻结,这些学习到的向量是微调期间唯一可训练的参数。 与学习 LoRA 更新低秩权重矩阵不同,处理学习向量可以使可训练参数的数量少得多。

与 LoRA 类似,IA3 具有许多相同的优点:

IA3 通过大幅减少可训练参数的数量,使微调更加高效。对于 T0 模型,使用 IA3 的可训练参数更少。
原始的预训练权重保持冻结状态,这意味着您可以拥有多个轻量级、便携式 IA3 模型,用于在其之上构建的各种下游任务。
使用 IA3 微调的模型的性能与完全微调的模型的性能相当。
IA3 不会增加任何推理延迟,因为适配器(adapter)权重可以与基础模型合并。
原则上,IA3 可以应用于神经网络中权重矩阵的任何子集,以减少可训练参数的数量。 根据作者的实现,IA3 权重被添加到 Transformer 模型的 key, value 和 feedforward 层。 给定注入 IA3 参数的目标层,可训练参数的数量可以根据权重矩阵的大小确定。

下面简单介绍通过peft实现IA3对模型进行微调的大致流程。

# 基于peft使用IA3对生成式对话模型进行微调 
from datasets import Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForSeq2Seq, TrainingArguments, Trainer
  • 1
  • 2
  • 3
# 加载数据集
ds = Dataset.load_from_disk("../transformers-code-master/02-NLP Tasks/16-generative_chatbot/alpaca_data_zh")
print(ds[:3])
  • 1
  • 2
  • 3
# 数据集处理
tokenizer = AutoTokenizer.from_pretrained("../models/bloom-1b4-zh")
# 数据处理函数
def process_func(example):
    MAX_LENGTH = 256
    input_ids, attention_mask, labels = [], [], []
    instruction = tokenizer("\n".join(["Human: " + example["instruction"], example["input"]]).strip() + "\n\nAssistant: ")
    response = tokenizer(example["output"] + tokenizer.eos_token)
    input_ids = instruction["input_ids"] + response["input_ids"]
    attention_mask = instruction["attention_mask"] + response["attention_mask"]
    labels = [-100] * len(instruction["input_ids"]) + response["input_ids"]
    if len(input_ids) > MAX_LENGTH:
        input_ids = input_ids[:MAX_LENGTH]
        attention_mask = attention_mask[:MAX_LENGTH]
        labels = labels[:MAX_LENGTH]
    return {
        "input_ids": input_ids,
        "attention_mask": attention_mask,
        "labels": labels
    }
# 数据处理
tokenized_ds = ds.map(process_func, remove_columns=ds.column_names)
tokenized_ds
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
# 创建模型
model = AutoModelForCausalLM.from_pretrained("../models/bloom-1b4-zh", low_cpu_mem_usage=True)
  • 1
  • 2
from peft import IA3Config, TaskType, get_peft_model

# 配置微调参数
config = IA3Config(task_type=TaskType.CAUSAL_LM)
# 创建模型
model = get_peft_model(model, config)

# 查看模型微调参数
model.print_trainable_parameters()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
# 配置训练参数
args = TrainingArguments(
    output_dir="./peft_model",
    per_device_train_batch_size=4,
    gradient_accumulation_steps=8,
    logging_steps=10,
    num_train_epochs=2,
    learning_rate=3e-3
)

# 创建训练器
trainer = Trainer(
    model=model,
    args=args,
    train_dataset=tokenized_ds,
    data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, padding=True),
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
# 模型训练
trainer.train()
  • 1
  • 2
# 模型推理
model = model.cuda()
ipt = tokenizer("Human: {}\n{}".format("周末去重庆怎么玩?", "").strip() + "\n\nAssistant: ", return_tensors="pt").to(model.device)
res =tokenizer.decode(model.generate(**ipt, max_length=128, do_sample=True)[0], skip_special_tokens=True)
print(res)
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/723562
推荐阅读
  

闽ICP备14008679号