博物馆网站建设方案书,seo要点,电子商务网站的建设开发工具,物流公司网站 源码本内容主要介绍 Transformers 库 的基本使用。
1.1 Transformers 库简介 Transformers 库是一个开源库#xff0c;其提供的所有预训练模型都是基于 transformer 模型结构的。
1.1.1 Transformers 库 我们可以使用 Transformers 库提供的 API 轻松下载和训练最先进的预训练模… 本内容主要介绍 Transformers 库 的基本使用。
1.1 Transformers 库简介 Transformers 库是一个开源库其提供的所有预训练模型都是基于 transformer 模型结构的。
1.1.1 Transformers 库 我们可以使用 Transformers 库提供的 API 轻松下载和训练最先进的预训练模型。使用预训练模型可以降低计算成本以及节省从头开始训练模型的时间。这些模型可用于不同模态的任务例如
文本文本分类、信息抽取、问答系统、文本摘要、机器翻译和文本生成。图像图像分类、目标检测和图像分割。音频语音识别和音频分类。多模态表格问答系统、OCR、扫描文档信息抽取、视频分类和视觉问答。 Transformers 库支持三个最流行的深度学习库PyTorch、TensorFlow 和 JAX。 相关资源对应网址如下
网址库的 GitHub 地址https://github.com/huggingface/transformers官方开发文档https://huggingface.co/docs/transformers/index预训练模型下载地址https://huggingface.co/models 1.1.2 Transformers 库支持的模型和框架 以下表格展示了当前 Transformers 库对各模型的支持情况
ModelTokenizer slowTokenizer fastPyTorch supportTensorFlow supportFlax supportALBERT✅✅✅✅✅BART✅✅✅✅✅BEiT❌❌✅❌✅BERT✅✅✅✅✅Bert Generation✅❌✅❌❌BigBird✅✅✅❌✅BigBirdPegasus❌❌✅❌❌Blenderbot✅✅✅✅✅BlenderbotSmall✅✅✅✅✅CamemBERT✅✅✅✅❌Canine✅❌✅❌❌CLIP✅✅✅✅✅ConvBERT✅✅✅✅❌ConvNext❌❌✅✅❌CTRL✅❌✅✅❌Data2VecAudio❌❌✅❌❌Data2VecText❌❌✅❌❌Data2VecVision❌❌✅✅❌DeBERTa✅✅✅✅❌DeBERTa-v2✅✅✅✅❌Decision Transformer❌❌✅❌❌DeiT❌❌✅❌❌DETR❌❌✅❌❌DistilBERT✅✅✅✅✅DPR✅✅✅✅❌DPT❌❌✅❌❌ELECTRA✅✅✅✅✅Encoder decoder❌❌✅✅✅FairSeq Machine-Translation✅❌✅❌❌FlauBERT✅❌✅✅❌Flava❌❌✅❌❌FNet✅✅✅❌❌Funnel Transformer✅✅✅✅❌GLPN❌❌✅❌❌GPT Neo❌❌✅❌✅GPT-J❌❌✅✅✅Hubert❌❌✅✅❌I-BERT❌❌✅❌❌ImageGPT❌❌✅❌❌LayoutLM✅✅✅✅❌LayoutLMv2✅✅✅❌❌LED✅✅✅✅❌Longformer✅✅✅✅❌LUKE✅❌✅❌❌LXMERT✅✅✅✅❌M2M100✅❌✅❌❌Marian✅❌✅✅✅MaskFormer❌❌✅❌❌mBART✅✅✅✅✅MegatronBert❌❌✅❌❌MobileBERT✅✅✅✅❌MPNet✅✅✅✅❌mT5✅✅✅✅✅Nystromformer❌❌✅❌❌OpenAI GPT✅✅✅✅❌OpenAI GPT-2✅✅✅✅✅OPT❌❌✅❌❌Pegasus✅✅✅✅✅Perceiver✅❌✅❌❌PLBart✅❌✅❌❌PoolFormer❌❌✅❌❌ProphetNet✅❌✅❌❌QDQBert❌❌✅❌❌RAG✅❌✅✅❌Realm✅✅✅❌❌Reformer✅✅✅❌❌RegNet❌❌✅❌❌RemBERT✅✅✅✅❌ResNet❌❌✅❌❌RetriBERT✅✅✅❌❌RoBERTa✅✅✅✅✅RoFormer✅✅✅✅✅SegFormer❌❌✅❌❌SEW❌❌✅❌❌SEW-D❌❌✅❌❌Speech Encoder decoder❌❌✅❌✅Speech2Text✅❌✅✅❌Speech2Text2✅❌❌❌❌Splinter✅✅✅❌❌SqueezeBERT✅✅✅❌❌Swin❌❌✅❌❌T5✅✅✅✅✅TAPAS✅❌✅✅❌TAPEX✅✅✅✅✅Transformer-XL✅❌✅✅❌TrOCR❌❌✅❌❌UniSpeech❌❌✅❌❌UniSpeechSat❌❌✅❌❌VAN❌❌✅❌❌ViLT❌❌✅❌❌Vision Encoder decoder❌❌✅✅✅VisionTextDualEncoder❌❌✅❌✅VisualBert❌❌✅❌❌ViT❌❌✅✅✅ViTMAE❌❌✅✅❌Wav2Vec2✅❌✅✅✅WavLM❌❌✅❌❌XGLM✅✅✅❌✅XLM✅❌✅✅❌XLM-RoBERTa✅✅✅✅✅XLM-RoBERTa-XL❌❌✅❌❌XLMProphetNet✅❌✅❌❌XLNet✅✅✅✅❌YOLOS❌❌✅❌❌ 注意Tokenizer slow使用 Python 实现 tokenization 过程。Tokenizer fast基于 Rust 库 Tokenizers 进行实现。 1.2 Pipeline pipeline() 的作用是使用预训练模型进行推断它支持从 这里 下载的所有模型。
1.2.1 Pipeline 支持的任务类型 pipeline() 支持许多常见任务
文本 情感分析Sentiment analysis文本生成Text generation命名实体识别Name entity recognitionNER问答系统Question answering掩码恢复Fill-mask文本摘要Summarization机器翻译Translation特征提取Feature extraction 图像 图像分类Image classification图像分割Image segmentation目标检测Object detection 音频 音频分类Audio classification自动语音识别Automatic speech recognitionASR 注意可以在 Transformers 库的源码查看 Transformers/pipelines/__init__.py 中的 SUPPORTED_TASKS 定义中查看其支持的任务不同版本支持的类型会存在差异。 1.2.2 Pipeline 使用
1简单使用 例如当前我们需要进行一个情感分析的推断任务。我们可以直接使用如下代码
from transformers import pipelineclassifier pipeline(sentiment-analysis)
result classifier(We are very happy to show you the Transformers library.)
print(result)将输出以下结果
[{label: POSITIVE, score: 0.9997795224189758}]上面代码中的 pipeline(sentiment-analysis) 将下载并缓存一个默认的情感分析的预训练模型和加载对应的 tokenizer。针对不同类型的任务对应的参数名称可查看 pipeline 的参数 task 的说明这里不同类型的任务所下载的默认预训练模型可以在 Transformers 库的源码查看 Transformers/pipelines/__init__.py 中的 SUPPORTED_TASKS 定义中查看。 当我们需要一次推理多个句子时可以使用 list 形式作为参数传入
from transformers import pipelineclassifier pipeline(sentiment-analysis)
results classifier([We are very happy to show you the Transformers library.,We hope you dont hate it.])
print(results)将输出以下结果
[{label: POSITIVE, score: 0.9997795224189758},{label: NEGATIVE, score: 0.5308570265769958}]2选择模型 上面部分在进行推理时使用的是对应任务的默认模型。但是有时候我们希望使用指定的模型可以通过指定 pipeline() 的参数 model 来实现。 第一种方法
from transformers import pipelineclassifier pipeline(sentiment-analysis,modelIDEA-CCNL/Erlangshen-Roberta-110M-Sentiment)
result classifier(今天心情很好)
print(result)将输出以下结果
[{label: Positive, score: 0.9374911785125732}]第二种方法和上面的方法加载的是相同的模型。不过这种方法可以使用本地模型进行推理。
from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer
from transformers import pipelinemodel_path r../pretrained_model/IDEA-CCNL(Erlangshen-Roberta-110M-Sentiment)
model AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer AutoTokenizer.from_pretrained(model_path)classifier pipeline(sentiment-analysis, modelmodel, tokenizertokenizer)
result classifier(今天心情很好)
print(result)将输出以下结果
[{label: Positive, score: 0.9374911785125732}]总结上面部分介绍了使用 pipeline() 对文本分类任务的推断的方法。针对文本其他类型任务、图像和音频的任务使用方法基本一致详细可参照 这里。
1.3 加载模型 下面我们将介绍加载模型的一些方法。
1.3.1 随机初始化模型权值 有时候需要随机初始化模型权值比如使用自己的数据进行预训练。首先我们需要初始化一个 config 对象然后将这个 config 对象作为参数传给模型
from transformers import BertConfig
from transformers import BertModelconfig BertConfig()
model BertModel(config)上面的 config 使用的是默认值不过根据需要我们可以修改对应的参数。当然我们也可以使用 AutoConfig.from_pretrained() 加载其他模型的 config
from transformers import AutoConfig
from transformers import AutoModelmodel_path r../pretrained_model/IDEA-CCNL(Erlangshen-Roberta-110M-Sentiment)
config AutoConfig.from_pretrained(model_path)
model AutoModel.from_config(config)1.3.2 使用预训练权值初始化模型权值 有时候需要从预训练模型中加载权值。一般使用 AutoModelForXXX.from_pretrained() 加载对应任务的预训练模型这里之所以使用 XXX是因为不同类型的任务所使用的类是不一样的。例如我们需要加载一个文本序列分类模型需要使用 AutoModelForSequenceClassification。
from transformers import AutoModelForSequenceClassificationmodel AutoModelForSequenceClassification.from_pretrained(IDEA-CCNL/Erlangshen-Roberta-110M-Sentiment)AutoModelForSequenceClassification.from_pretrained() 的第一个参数 pretrained_model_name_or_path 可以是一个字符串也可以是一个文件夹路径。
from transformers import AutoModelForSequenceClassificationmodel_path r../pretrained_model/IDEA-CCNL(Erlangshen-Roberta-110M-Sentiment)
model AutoModelForSequenceClassification.from_pretrained(model_path)我们也可以使用具体的模型类比如下面的 BertForSequenceClassification
from transformers import BertForSequenceClassificationmodel_path r../pretrained_model/IDEA-CCNL(Erlangshen-Roberta-110M-Sentiment)
model BertForSequenceClassification.from_pretrained(model_path)注意上面的模型类型都是针对 PyTorch 模型的。如果我们使用 TensorFlow 模型其类名需要在 PyTorch 模型类名的前面加上 TF。比如 BertForSequenceClassification 对应的 TF 模型类名是 TFBertForSequenceClassification
总结官方推荐使用 AutoModelForXXX 和 TFAutoModelXXX 加载预训练模型。官方认为这样将确保每次都能加载正确的框架。
1.4 预处理 因为模型本身是无法理解原始文本、图像或者音频的。所以需要先将数据转换成模型可以接受的形式然后再传入模型中。
1.4.1 NLPAutoTokenizer 处理文本数据的主要工具为 tokenizer。首先tokenizer 会根据一组规则将文本拆分为 token。然后将这些 token 转换为数值根据词表即 vocab这些数值会被构建成张量并作为模型的输入。模型所需要的其他输入也是由 tokenizer 添加。 当我们使用预训练模型时一定要使用对应的预训练 tokenizer。只有这样才能确保文本以预训练语料库相同的方式进行分割并使用相同的对应 token 索引即 vocab。 1Tokenize 使用 AutoTokenizer.from_pretrained() 加载一个预训练 tokenizer并将文本传入 tokenizer
from transformers import AutoTokenizermodel_path r../pretrained_model/IDEA-CCNL(Erlangshen-Roberta-110M-Sentiment)
tokenizer AutoTokenizer.from_pretrained(model_path)encoded_input tokenizer(今天心情很好)
print(encoded_input)将输出以下结果
{input_ids: [101, 791, 1921, 1921, 3698, 4696, 1962, 102],token_type_ids: [0, 0, 0, 0, 0, 0, 0, 0],attention_mask: [1, 1, 1, 1, 1, 1, 1, 1]}可以看到上面的输出包含三个部分
input_ids对应于句子中每个 token 的索引。token_type_ids当存在多个序列时标识 token 属于那个序列。attention_mask表明对应的 token 是否需要被注意1 表示需要被注意0 表示不需要被注意。涉及到注意力机制。 我们还可以使用 tokenizer 将 input_ids 解码为原始输入
decoded_input tokenizer.decode(encoded_input[input_ids])
print(decoded_input)将输出以下结果
[CLS] 今 天 天 气 真 好 [SEP]我们可以看到上面的输出相比原始文本多了 [CLS] 和 [SEP]它们是在 BERT 等模型中添加一些特殊 token。 如果需要同时处理多个句子可以将多个文本以 list 的形式输入到 tokenizer 中。
2填充Pad 当我们处理一批句子时它们的长度并不总是相同的。但是模型的输入需要具有统一的形状shape。填充是实现此需求的一种策略即为 token 较少的句子添加特殊的填充 token。 给 tokenizer() 传入参数 paddingTrue
batch_sentences [今天天气真好,今天天气真好适合出游]
encoded_inputs tokenizer(batch_sentences, paddingTrue)
print(encoded_inputs)将输出以下结果
{input_ids:[[101, 791, 1921, 1921, 3698, 4696, 1962, 102, 0, 0, 0, 0, 0],[101, 791, 1921, 1921, 3698, 4696, 1962, 8024, 6844, 1394, 1139, 3952, 102]], token_type_ids:[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
attention_mask:[[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}可以看到 tokenizer 使用 0 对第一个句子进行了一些填充。
3截断Truncation 当句子太短时可以采用填充的策略。但有时候句子可能太长模型无法处理。在这种情况下可以将句子进行截断。 给 tokenizer() 传入参数 truncationTrue 即可实现。 如果想了解 tokenizer() 中更多关于参数 padding 和 truncation 的信息可以参照 这里
4构建张量Build tensors 最终如果我们想要 tokenizer 返回传入模型中的实际张量。需要设置参数 return_tensors。如果是传入 PyTorch 模型将其设置为 pt如果是传入 TensorFlow 模型将其设置为 tf。
batch_sentences [今天天气真好,今天天气真好适合出游]
encoded_inputs tokenizer(batch_sentences,paddingTrue, truncationTrue,return_tensorspt)
print(encoded_inputs)将输出以下结果
{input_ids:tensor([[ 101, 791, 1921, 1921, 3698, 4696, 1962, 102, 0, 0, 0, 0,0],[ 101, 791, 1921, 1921, 3698, 4696, 1962, 8024, 6844, 1394, 1139, 3952,102]]),
token_type_ids:tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]),attention_mask:tensor([[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}1.4.2 其他 针对音频数据预处理主要包括重采样Resample、特征提取Feature Extractor、填充pad和截断Truncate详细信息请参照 这里。针对图像数据预处理主要包括特征提取Feature Extractor和数据增强详细信息请参照 这里。针对多模态数据不同类型的数据使用前面介绍的对应预处理方法详细信息请参照 这里。虽然每种数据的预处理方法不完全一样但是最终的目的都是一致的将原始数据转换为模型可以接受的形式。
1.5 微调预训练模型 下面将以一个文本多分类的例子简单介绍如何使用我们自己的数据训练一个分类模型。
1.5.1 准备数据 在微调预训练模型之前我们需要先准备数据。我们可以使用 Datasets 库的 load_dataset 加载数据集
from datasets import load_dataset# 第 1 步准备数据
# 从文件中获取原始数据
datasets load_dataset(f./my_dataset.py)
# 输出训练集中的第一条数据
print(datasets[train][0])在这里需要注意一下因为我们是使用自己的数据进行模型训练所以上面 load_dataset 传入的参数是一个 py 文件的路径。这个 py 文件按照 Datasets 库的规则读取文件并返回训练数据如果想了解更多信息可以参照 这里。 如果我们只是想简单学习 Transformers 库的使用可以使用 Datasets 这个库预置的一些数据集这个时候 load_dataset 传入的参数是一些字符串比如load_dataset(imdb)然后会自动下载对应数据集。
1.5.2 预处理 在将数据喂给模型之前需要将数据进行预处理Tokenize、填充、截断等。
from transformers import AutoTokenizer# 第 2 步预处理数据
# 2.1 加载 tokenizer
tokenizer AutoTokenizer.from_pretrained(configure[model_path])def tokenize_function(examples):return tokenizer(examples[text], paddingmax_length, truncationTrue)# 2.2 得到经过 tokenization 后的数据
tokenized_datasets datasets.map(tokenize_function, batchedTrue)
print(tokenized_datasets[train][0])首先加载 tokenizer然后使用 datasets.map() 生成经过预处理后的数据。因为数据经过 tokenizer() 处理后的不再是 dataset 格式所以需要使用 datasets.map() 进行处理。
1.5.3 加载模型 在前面的部分已经介绍过模型加载的方法可以使用 AutoModelXXX.from_pretrained 加载模型
from transformers import AutoModelForSequenceClassification# 第 3 步加载模型
classification_model AutoModelForSequenceClassification.from_pretrained(configure[model_path], num_labelsget_num_labels())与前面部分不同的地方在于上面的代码中有一个 num_labels 参数需要给这个参数传入我们的数据集中的类别数量。
1.5.4 设定度量指标 在模型训练过程中我们希望能够输出模型的性能指标比如准确率、精确率、召回率、F1 值等以便了解模型的训练情况。我们可以通过 Datasets 库提供的 load_metric() 来实现。下面的代码中实现了准确率计算
import numpy as np
from datasets import load_metric# 第 4 步设定度量指标
metric load_metric(./accuracy.py)def compute_metrics(eval_pred):logits, labels eval_predpredictions np.argmax(logits, axis-1)return metric.compute(predictionspredictions, referenceslabels)如果想了解更多信息可以参照 这里。
1.5.5 设置训练超参数 在进行模型训练时还需要设置一些超参数Transformers 库提供了 TrainingArguments 类。
from transformers import TrainingArguments# 第 5 步设置训练超参数
training_args TrainingArguments(output_dirconfigure[output_dir],evaluation_strategyepoch)在上面的代码中我们设置了两个参数output_dir 指定保存模型的输出路径evaluation_strategy 决定什么时候对模型进行评估设置的参数 epoch 表明每训练完一个 epoch 后进行一次评估评估内容即上一步设定的度量指标。 如果想了解更多参数的设置和具体含义可以参照 这里。
1.5.6 训练和保存模型 经过前面一系列的步骤后我们终于可以开始进行模型训练了。Transformers 库提供了 Trainer 类可以很简单方便地进行模型训练。首先创建一个 Trainer然后调用 train() 函数就开始进行模型训练了。当模型训练完毕后调用 save_model() 保存模型。
# 第 6 步开始训练模型
trainer Trainer(modelclassification_model,argstraining_args,train_datasettokenized_datasets[train],eval_datasettokenized_datasets[validation],tokenizertokenizer,compute_metricscompute_metrics)
trainer.train()# 保存模型
trainer.save_model()有时候我们需要调试模型就需要自己编写模型训练循环详细方法可以参照 这里。
1.5.7 总结 经过前面的介绍现在我们就可以开始动手训练我们自己的文本多分类模型了。 不过前面是以一个文本多分类的例子介绍了如何使用 Transformers 库微调预训练模型。针对其他类型的任务相比文本分类任务会存在一些差异具体指导可以参照以下链接
任务类型参考链接文本分类Text classificationhttps://huggingface.co/docs/transformers/tasks/sequence_classificationToken classification例如 NERhttps://huggingface.co/docs/transformers/tasks/token_classification问答系统Question answeringhttps://huggingface.co/docs/transformers/tasks/question_answering语言模型Language modelinghttps://huggingface.co/docs/transformers/tasks/language_modeling机器翻译Translationhttps://huggingface.co/docs/transformers/tasks/translation文本摘要Sumarizationhttps://huggingface.co/docs/transformers/tasks/summarization多项选择Multiple choicehttps://huggingface.co/docs/transformers/tasks/multiple_choice音频分类Audio classificationhttps://huggingface.co/docs/transformers/tasks/audio_classification自动语音识别ASRhttps://huggingface.co/docs/transformers/tasks/asr图像分类Image classificationhttps://huggingface.co/docs/transformers/tasks/image_classification 参考
[1] Github 地址
[2] 官方开发文档
[3] transformers 教程
[4] https://github.com/nlp-with-transformers/notebooks
[5] https://github.com/datawhalechina/learn-nlp-with-transformers