机器学习初学者 2024年12月04日
【NLP】Kaggle知识点:文本分类与LoRA
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍参数高效微调(PEFT)及LoRA,包括其优势、应用案例等。PEFT可降低计算和存储成本,LoRA能减少可训练参数数量。文中还展示了LoRA在文本分类中的应用。

参数高效微调(PEFT)可针对特定应用微调选定参数,降低成本和基础设施要求。

LoRA是一种低秩分解方法,减少可训练参数数量,降低内存消耗。

文中通过LoRA文本分类案例,展示了其在实际中的应用,包括安装依赖、加载数据集等操作。

PEFT和LoRA具有计算存储成本降低、性能保持一致等优势。

Coggle 2024-12-04 12:02 浙江

在这篇博客中,我们逐步进行参数高效微调(Parameter Efficient Fine Tuning,简称PEFT),使用大语言模型(LLM)低秩适配(Low Rank Adaptation,LoRA)。我们将了解如何使用参数高效微调来针对特定应用微调选定的可训练参数,以最低的成本和最少的基础设施实现这一目标。

unsetunset为什么需要参数高效微调?unsetunset

大语言模型(LLM)已经针对某些任务进行了预训练;我们可以在应用程序中使用LLM来执行它们已经训练过的任何任务。然而,这些LLM在我们的环境中运行需要非常昂贵的资源,因此需要参数高效微调。

假设我们能够以经济有效的方式在我们的系统上使用大语言模型。这可以通过使用PEFT库来实现,因为它允许我们单独使用LLM的一些参数。

PEFT(参数高效微调)

参数高效微调(Parameter Efficient Fine Tuning,简称PEFT)是一个库,它允许我们在不对完整模型进行微调的情况下使用大语言模型(LLM)来执行任务,而是对一些(额外的)参数进行微调。完整模型的微调通常需要昂贵的计算成本,而PEFT通过微调额外参数显著减少了计算和存储成本。

参数高效微调的优势

unsetunsetLoRA(低秩适配)unsetunset

LoRA(Low Rank Adaptation)是一种低秩分解方法,旨在减少可训练参数的数量,从而在微调大语言模型(LLM)时降低内存消耗。通过使用LoRA,可以更加轻松地进行LLM的微调,同时显著减少所需的计算和存储资源。

在PEFT(参数高效微调)中,LoRA配置通过get_peft_model()函数封装,以创建一个可训练的PeftModel。通过调整LoraConfig中的init_lora_weights参数,可以增加或减少模型权重,从而优化模型的性能和资源消耗。

from peft import LoraConfig, get_peft_model
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
# 加载预训练的模型和分词器
model_name = "t5-small"
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 配置LoRA参数
lora_config = LoraConfig(
    r=8,  # 低秩矩阵的秩
    lora_alpha=32,  # LoRA的alpha参数
    lora_dropout=0.1,  # Dropout率
    init_lora_weights=0.02  # 初始化LoRA权重
)
# 获取PEFT模型
peft_model = get_peft_model(model, lora_config)
# 对输入进行编码
input_text = "Translate English to French: The weather is nice today."
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
# 使用PEFT模型进行推理
outputs = peft_model.generate(input_ids)
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("Translated text:", output_text)

unsetunsetLoRA文本分类案例unsetunset

!pip install transformers datasets evaluate accelerate peft
import torch
from transformers import RobertaModel, RobertaTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer, DataCollatorWithPadding
from peft import LoraConfig, get_peft_model
from datasets import load_dataset
peft_model_name = 'roberta-base-peft'
modified_base = 'roberta-base-modified'
base_model = 'roberta-base'
dataset = load_dataset('ag_news')
tokenizer = RobertaTokenizer.from_pretrained(base_model)
def preprocess(examples):
    tokenized = tokenizer(examples['text'], truncation=True, padding=True)
    return tokenized
tokenized_dataset = dataset.map(preprocess, batched=True,  remove_columns=["text"])
train_dataset=tokenized_dataset['train']
eval_dataset=tokenized_dataset['test'].shard(num_shards=2, index=0)
test_dataset=tokenized_dataset['test'].shard(num_shards=2, index=1)
# Extract the number of classess and their names
num_labels = dataset['train'].features['label'].num_classes
class_names = dataset["train"].features["label"].names
print(f"number of labels: {num_labels}")
print(f"the labels: {class_names}")
# Create an id2label mapping
# We will need this for our classifier.
id2label = {i: label for i, label in enumerate(class_names)}
data_collator = DataCollatorWithPadding(tokenizer=tokenizer, return_tensors="pt")
# use the same Training args for all models
training_args = TrainingArguments(
    output_dir='./results',
    evaluation_strategy='steps',
    learning_rate=5e-5,
    num_train_epochs=1,
    per_device_train_batch_size=16,
)
def get_trainer(model):
      return  Trainer(
          model=model,
          args=training_args,
          train_dataset=train_dataset,
          eval_dataset=eval_dataset,
          data_collator=data_collator,
      )
full_finetuning_trainer = get_trainer(
    AutoModelForSequenceClassification.from_pretrained(base_model, id2label=id2label),
)
full_finetuning_trainer.train()
model = AutoModelForSequenceClassification.from_pretrained(base_model, id2label=id2label)
peft_config = LoraConfig(task_type="SEQ_CLS", inference_mode=False, r=8, lora_alpha=16, lora_dropout=0.1)
peft_model = get_peft_model(model, peft_config)
peft_model.print_trainable_parameters()
peft_lora_finetuning_trainer = get_trainer(peft_model)
peft_lora_finetuning_trainer.train()

往期精彩回顾





请备注:”昵称-学校/公司-研究方向“,例如:”张小明-浙大-CV“加群。

也可以加入机器学习交流qq群772479961


跳转微信打开

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

参数高效微调 LoRA 文本分类 模型效率
相关文章