本文主要是介绍10.0大模型微调 bitfit,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 介绍
参数高效微调方法分类。主要基于三大类方法:基于additive、基于selective和基于reparametrization-based。在additive方法中,主要两大类:adapters方法和soft prompts。
1 基于additive方法
additive方法,顾名思义“增量式”,通常向预训练模型添加额外的小型网络层或模块,而不直接修改原有模型的权重。这种方法能够实现在保留预训练模型通用性能的同时,针对特定任务进行优化。下面是几种常见的additive微调方法:
1. 适配器模块(Adapter Modules) - 适配器是一种轻量级的神经网络层,嵌入到预训练模型的各个层之间或之内。这些适配器只有少量的参数需要训练,而模型的其他参数保持固定。适配器通常包括一些简单的前馈神经网络层,例如,一个下降维度的线性层,一个激活函数,以及一个恢复原始维度的线性层。适配器可以非常有效地针对特定任务调整模型的行为,而不需要重新训练整个模型。
2. 提示调整 (Prompt-Tuning) - 虽然提示调整不涉及到添加新的网络层,但它可以被视为一种additive策略,因为它通过添加或修改输入序列的一部分(即提示)来影响模型的输出。这种方法利用了预训练模型的灵活性,通过最优化一组固定的词(prompt tokens),以达到微调模型的目的。
3. 前缀调整 (Prefix-Tuning) - 类似于提示调整,前缀调整在模型处理每个输入之前添加一系列可训练的向量(称为前缀)。这些前缀在模型的解码器或注意力机制中作为额外的上下文使用,从而引导模型生成特定于任务的响应。
2 基于selective方法
selective方法,顾名思义“选择式”,涉及选择性地调整或优化模型的部分参数,而不是对整个模型的所有参数进行微调。这种方法旨在在维持大部分预训练参数不变的情况下,仅对影响最大的部分进行优化。
BitFit(BIas-Term FIne-Tuning)的方法实质上是冻结了大部分transformer-encoder参数,只训练bais和特定任务的classification layer。该方法是参数高效的:每个新任务只需要存储bais参数向量(占参数总数的 0.1% 以下)和特定任务的classification layer。
具体来说:BERT 编码器由 L 层组成,其中每层 l 以 M 个自注意力头开始,其中自注意力头 (m, l) 具有key、quary和value 编码器,每个编码器都采用线性层的形式:
2 使用
数据 | 魔搭社区 |
模型 | 魔搭社区 |
1 注意数据集合中有None需要进行剔除;
import pandas as pd
train_df = train_df[~train_df['sentence'].isna()]
train_df[~train_df['label'].isna()].reset_index(drop=True).to_csv('../sentment_data/train_del.csv')
train_df[train_df['label'].isna()].reset_index(drop=True).to_csv('../sentment_data/test_del.csv')
2 计算该模型大小:sum([para.numel() for para in model.parameters()]) / 10000000000
0.0102269186B(十亿) * 4 Byter = 0.04G ,占用内存不大; 整个模型训练 = 模型内存 + 梯度内存 + 优化器内存 = 0.04G * 4 = 0.16GB
3 准备评估指标:https://huggingface.co/spaces/evaluate-metric/f1/tree/main
# pip install evaluate
- 数据部分,数据存在None,需要过滤掉;
- transformers版本问题; transformers 版本太低了!
- 损失函数, 比如这里的二分类,模型输出的是两个值[batch,2],但是input labels是 【batch,1】不匹配导致计算损失函数有问题。
- 模型输入nan, 模型计算的时候我反复调试,但是没有重新加载,导致模型效果越来越差了;
2.1 全调参数
from datasets import Dataset,load_dataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
import torch
import evaluate
import numpy as np# 检测显卡
"cuda" if torch.cuda.is_available() else "cpu"#from datasets import load_metric
# 定义评估指标
metric = evaluate.load("./f1.py")
# 定义计算指标的函数
def compute_metrics(eval_pred):predictions, labels = eval_pred#labels = labels.flatten()predictions = np.argmax(predictions, axis=1)f1 = metric.compute(predictions=predictions, references=labels, average="macro")accuracy = (predictions == labels).mean()return {"accuracy": accuracy, "f1": f1["f1"]}import pandas as pd
train_df = pd.read_csv('../sentment_data/train.csv')
train_df = train_df[~train_df['sentence'].isna()]
train_df[~train_df['label'].isna()].reset_index(drop=True).to_csv('../sentment_data/train_del.csv',index=False)
train_df[train_df['label'].isna()].reset_index(drop=True).to_csv('../sentment_data/test_del.csv',index=False)tra_dataset = load_dataset('csv',data_files='../sentment_data/train_del.csv')
val_dataset = load_dataset('csv',data_files='../sentment_data/dev.csv')
test_dataset = load_dataset('csv',data_files='../sentment_data/test_del.csv')tra_dataset = tra_dataset.filter(lambda x: x["label"] is not None)
tra_dataset = tra_dataset.filter(lambda x: x["sentence"] is not None)tokenizer = AutoTokenizer.from_pretrained('../mengzi-bert-base/')# 自定义模型的输出类别个数
#model.classifier.out_features = 3def process_function(examples):tokenized_examples = tokenizer(examples["sentence"], max_length=32, truncation=True,padding="max_length")#print(examples['label'])tokenized_examples["labels"] = examples['label']#[int(i) for i in examples['label']]return tokenized_examplestokenized_datasets = tra_dataset.map(process_function, batched=True, remove_columns=tra_dataset["train"].column_names)valid_datasets = val_dataset.map(process_function, batched=True, remove_columns=val_dataset["train"].column_names)tokenized_datasets['validation'] = valid_datasets['train']#
from transformers import DataCollatorWithPadding
model = AutoModelForSequenceClassification.from_pretrained("../mengzi-bert-base/",num_labels=2)args = TrainingArguments(output_dir="./checkpoints", # 输出文件夹per_device_train_batch_size=32, # 训练时的batch_sizegradient_accumulation_steps=32, # *** 梯度累加 ***gradient_checkpointing=True, # *** 梯度检查点 ***optim="adafactor", # *** adafactor优化器 *** per_device_eval_batch_size=4, # 验证时的batch_sizenum_train_epochs=3, # 训练轮数logging_steps=10, # log 打印的频率evaluation_strategy="epoch", # 评估策略save_strategy="epoch", # 保存策略save_total_limit=3, # 最大保存数learning_rate=2e-5, # 学习率weight_decay=0.001, # weight_decaymetric_for_best_model="f1", # 设定评估指标load_best_model_at_end=True, # 训练完成后加载最优模型#max_grad_norm=1.0,# 使用GPU# fp16=True, # 使用混合精度训练#device="cuda" if torch.cuda.is_available() else "cpu") # 定义计算指标的函数
def compute_metrics(eval_pred):try:print(eval_pred)predictions, labels = eval_pred#labels = labels.flatten()predictions = np.argmax(predictions, axis=1)f1 = metric.compute(predictions=predictions, references=labels, average="macro")accuracy = (predictions == labels).mean()return {"accuracy": accuracy, "f1": f1["f1"]}except Exception as e:print(e)trainer = Trainer(model=model, args=args, tokenizer=tokenizer,train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=DataCollatorWithPadding(tokenizer=tokenizer),compute_metrics=compute_metrics)# 自定义Trainer类
class CustomTrainer(Trainer):def compute_loss(self, model, inputs, return_outputs=False):"""Compute the loss for the given model and inputs.:param model: The model to compute the loss for.:param inputs: The inputs dictionary containing the data.:param return_outputs: Whether to return the model outputs along with the loss.:return: A tuple (loss, outputs) if return_outputs is True, else just the loss."""# 构造模型输入model_inputs = {"input_ids": inputs['input_ids'],"attention_mask": inputs['attention_mask'],"token_type_ids": inputs['token_type_ids']}# 将预测值转换为logitsoutputs = model(**model_inputs)
# outputs = model(input_ids=inputs['input_ids'],
# attention_mask=inputs['input_ids'],
# token_type_ids=inputs['token_type_ids'],
# )#print(outputs)logits = outputs.logits#print(logits)# 确保预测值的形状为(batch_size, 2)# 如果模型输出已经是二维的,无需额外处理# 将真实标签转换为一维张量labels = inputs["labels"].flatten().long() # 确保标签是整数类型# 计算交叉熵损失loss = torch.nn.functional.cross_entropy(logits, labels)#print(logits)#print(loss)return (loss, outputs) if return_outputs else loss# 创建CustomTrainer实例
trainer = CustomTrainer(model=model,args=args,train_dataset=tokenized_datasets["train"],eval_dataset=valid_datasets["train"], # 确保使用验证数据集data_collator=DataCollatorWithPadding(tokenizer=tokenizer),compute_metrics=compute_metrics
)
#trainer.model = trainer.model.to("cuda")
# 确保使用GPU
if torch.cuda.is_available():trainer.model = trainer.model.to("cuda")
# 训练模型
trainer.train()# # 评估模型
# eval_results = trainer.evaluate()
# print(eval_results)
#trainer.train()#trainer.train()
2.2 bitfit ,固定模型的参数
对bais进行固定:102914 , 占比 1.006%;可见训练的内存非常小了;
- 如果学习率还是用2e-5 会很慢,所以我修改了2e-3;效果也与上面的很接近了
#
from transformers import DataCollatorWithPadding
from transformers.trainer_callback import TrainerCallback
import matplotlib.pyplot as pltmodel = AutoModelForSequenceClassification.from_pretrained("../mengzi-bert-base/",num_labels=2)# 自定义回调类,用于在训练过程中打印损失
class PrintLossCallback(TrainerCallback):def __init__(self):self.losses = []self.steps = []def on_log(self, args, state, control, logs=None, **kwargs):# 打印训练过程中的日志信息try:if logs is not None:print(f"Step {state.global_step}: Loss={logs['loss']:.4f}, Learning Rate={logs['learning_rate']:.6f}")self.losses.append(logs['loss'])self.steps.append(state.global_step)except Exception as e :print(f'on_log error {e}')def plot_losses(self):plt.figure(figsize=(10, 5))plt.plot(self.steps, self.losses, label='Training Loss')plt.xlabel('Steps')plt.ylabel('Loss')plt.title('Training Loss Over Time')plt.legend()plt.show()
num_param = 0
for name, param in model.named_parameters():if "bias" not in name:param.requires_grad = Falseelse:num_param += param.numel()args = TrainingArguments(output_dir="./checkpoints", # 输出文件夹per_device_train_batch_size=32, # 训练时的batch_sizegradient_accumulation_steps=32, # *** 梯度累加 ***gradient_checkpointing=True, # *** 梯度检查点 ***#optim="adafactor", # *** adafactor优化器 *** optim="adamw_hf", # *** adafactor优化器 *** per_device_eval_batch_size=4, # 验证时的batch_sizenum_train_epochs=3, # 训练轮数logging_steps=10, # log 打印的频率evaluation_strategy="epoch", # 评估策略save_strategy="epoch", # 保存策略save_total_limit=3, # 最大保存数learning_rate=2e-3, # 学习率weight_decay=0.001, # weight_decaymetric_for_best_model="f1", # 设定评估指标load_best_model_at_end=True, # 训练完成后加载最优模型#max_grad_norm=1.0,# 使用GPU# fp16=True, # 使用混合精度训练#device="cuda" if torch.cuda.is_available() else "cpu") # 定义计算指标的函数
def compute_metrics(eval_pred):try:print(eval_pred)predictions, labels = eval_pred#labels = labels.flatten()predictions = np.argmax(predictions, axis=1)f1 = metric.compute(predictions=predictions, references=labels, average="macro")accuracy = (predictions == labels).mean()return {"accuracy": accuracy, "f1": f1["f1"]}except Exception as e:print(e)trainer = Trainer(model=model, args=args, tokenizer=tokenizer,train_dataset=tokenized_datasets["train"], eval_dataset=tokenized_datasets["validation"], data_collator=DataCollatorWithPadding(tokenizer=tokenizer),compute_metrics=compute_metrics)# 自定义Trainer类
class CustomTrainer(Trainer):def compute_loss(self, model, inputs, return_outputs=False):"""Compute the loss for the given model and inputs.:param model: The model to compute the loss for.:param inputs: The inputs dictionary containing the data.:param return_outputs: Whether to return the model outputs along with the loss.:return: A tuple (loss, outputs) if return_outputs is True, else just the loss."""# 构造模型输入model_inputs = {"input_ids": inputs['input_ids'],"attention_mask": inputs['attention_mask'],"token_type_ids": inputs['token_type_ids']}# 将预测值转换为logitsoutputs = model(**model_inputs)
# outputs = model(input_ids=inputs['input_ids'],
# attention_mask=inputs['input_ids'],
# token_type_ids=inputs['token_type_ids'],
# )#print(outputs)logits = outputs.logits#print(logits)# 确保预测值的形状为(batch_size, 2)# 如果模型输出已经是二维的,无需额外处理# 将真实标签转换为一维张量labels = inputs["labels"].flatten().long() # 确保标签是整数类型# 计算交叉熵损失loss = torch.nn.functional.cross_entropy(logits, labels)#print(logits)#print(loss)return (loss, outputs) if return_outputs else loss# 创建CustomTrainer实例
plot_losses_callback = PrintLossCallback()
trainer = CustomTrainer(model=model,args=args,train_dataset=tokenized_datasets["train"],eval_dataset=valid_datasets["train"], # 确保使用验证数据集data_collator=DataCollatorWithPadding(tokenizer=tokenizer),compute_metrics=compute_metrics,callbacks=[plot_losses_callback] # 注册自定义回调
)
#trainer.model = trainer.model.to("cuda")
# 确保使用GPU
if torch.cuda.is_available():trainer.model = trainer.model.to("cuda")
# 训练模型
trainer.train()# # 评估模型
# eval_results = trainer.evaluate()
# print(eval_results)
#trainer.train()#trainer.train()
打印损失方便查看训练过程!
2.3 推理
消极评价
积极评价
这篇关于10.0大模型微调 bitfit的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!