当前位置: 首页 > article >正文

【Finetune】(一)、transformers之BitFit微调

文章目录

  • 0、参数微调简介
  • 1、常见的微调方法
  • 2、代码实战
    • 2.1、导包
    • 2.2、加载数据集
    • 2.3、数据集处理
    • 2.4、创建模型
    • 2.5、BitFit微调*
    • 2.6、配置模型参数
    • 2.7、创建训练器
    • 2.8、模型训练
    • 2.9、模型推理

0、参数微调简介

 参数微调方法是仅对模型的一小部分的参数(这一小部分可能是模型自身的,也可能是外部引入的)进行训练,便可以为模型带来显著的性能变化,在一些场景下甚至不输于全量微调。
 由于训练一小部分参数,极大程度降低了训练大模型的算力需求,不需要多机多卡,单卡就可以完成对一些大模型的训练。不仅如此,少量的训练参数,对存储的要求同样降低很多,大多数的参数微调方法只需要保存训练部分的参数,与动辄几十GB的原始大模型相比,几乎可以忽略。

1、常见的微调方法

 常见的微调方法如图所示:
在这里插入图片描述

Lialin, Vladislav, Vijeta Deshpande, and Anna Rumshisky. “Scaling down to scale up: A guide to parameter-efficient fine-tuning.” arXiv preprint arXiv:2303.15647 (2023).

2、代码实战

  • 模型——bloom-389m-zh
  • 数据集——alpaca_data_zh

2.1、导包

from datasets import load_dataset, Dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForSeq2Seq, TrainingArguments, Trainer

2.2、加载数据集

ds = Dataset.load_from_disk("./alpaca_data_zh/")

2.3、数据集处理

tokenizer = AutoTokenizer.from_pretrained("../Model/bloom-389m-zh")
tokenizer
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

2.4、创建模型

model = AutoModelForCausalLM.from_pretrained("../Model/bloom-389m-zh",low_cpu_mem_usage=True)

2.5、BitFit微调*

#选择模型参数里面的所有bias部分
#非bias部分冻结
num_param = 0
for name,param in model.named_parameters():
    if 'bias' not in name:
        param.requires_grad = False
    else:
        num_param+=param.numel()
num_param

2.6、配置模型参数

args = TrainingArguments(
    output_dir="./chatbot",
    per_device_train_batch_size=1,
    gradient_accumulation_steps=4,
    logging_steps=10,
    num_train_epochs=1
)

2.7、创建训练器

trainer = Trainer(
    args=args,
    model=model,
    train_dataset=tokenized_ds,
    data_collator=DataCollatorForSeq2Seq(tokenizer, padding=True, )
)

2.8、模型训练

trainer.train()

2.9、模型推理

from transformers import pipeline

pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
ipt = "Human: {}\n{}".format("考试有哪些技巧?", "").strip() + "\n\nAssistant: "
pipe(ipt, max_length=256, do_sample=True, temperature=0.5)

http://www.kler.cn/news/310377.html

相关文章:

  • ZLMediaKit Windows编译以及使用
  • 浅谈Spring Cloud:认识微服务
  • Flutter问题记录 - 适配Xcode 16和iOS 18
  • 【系统架构设计师-2011年真题】案例分析-答案及详解
  • 优思学院|如何从零开始自己学习六西格玛?
  • 井盖状态检测数据集
  • TCP socket
  • Android 进程间通信
  • 使用llama.cpp 在推理MiniCPM-1.2B模型
  • 24年蓝桥杯及攻防世界赛题-MISC-3
  • 【Redis】Redis 典型应用 - 分布式锁原理与实现
  • 计算机毕业设计 基于SpringBoot框架的网上蛋糕销售系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • Python编程 - 协程
  • [PICO VR眼镜]眼动追踪串流Unity开发与使用方法,眼动追踪打包报错问题解决(Eye Tracking/手势跟踪)
  • FFmpeg源码:skip_bits、skip_bits1、show_bits函数分析
  • centos远程桌面连接windows
  • iPhone 16系列:熟悉的味道,全新的体验
  • 浅谈Tair缓存的三种存储引擎MDB、LDB、RDB
  • 使用Addressables+SpriteAtlas打包产生冗余
  • Python知识点:详细讲解Python字节码与反编译
  • Elasticsearch 开放推理 API 新增阿里云 AI 搜索支持
  • react 高阶组件
  • 优化数据的抓取规则:减少无效请求
  • 【数学建模】典型相关分析
  • 【RabbitMQ 项目】服务端:数据管理模块之消息管理
  • 大语言模型超参数调优:开启 AI 潜能的钥匙
  • Linux下rpm方式部署mysql(国产化生产环境无联网服务器部署实操)
  • Android开发高频面试题之——Android篇
  • 为什么 ECB 模式不安全
  • ETL架构类型有哪些?怎么选择?