赞
踩
这篇博客主要面向对Bert系列在Pytorch上应用感兴趣的同学,将涵盖的主要内容是:Bert系列有关的论文,Huggingface的实现,以及如何在不同下游任务中使用预训练模型。
看过这篇博客,你将了解:
安装Huggface库(需要预先安装pytorch)
在阅读这篇文章之前,如果你能将以下资料读一遍,或者看一遍的话,在后续的阅读过程中将极大地减少你陷入疑惑的概率。
或者,你更愿意去看论文的话:
一个完整的transformer模型主要包含三部分:
Config,控制模型的名称、最终输出的样式、隐藏层宽度和深度、激活函数的类别等。将Config类导出时文件格式为 json格式,就像下面这样:
- {
- "attention_probs_dropout_prob": 0.1,
- "hidden_act": "gelu",
- "hidden_dropout_prob": 0.1,
- "hidden_size": 768,
- "initializer_range": 0.02,
- "intermediate_size": 3072,
- "max_position_embeddings": 512,
- "num_attention_heads": 12,
- "num_hidden_layers": 12,
- "type_vocab_size": 2,
- "vocab_size": 30522
- }
当然,也可以通过config.json来实例化Config类,这是一个互逆的过程。
Tokenizer,这是一个将纯文本转换为编码的过程。注意,Tokenizer并不涉及将词转化为词向量的过程,仅仅是将纯文本分词,添加[MASK]标记、[SEP]、[CLS]标记,并转换为字典索引。Tokenizer类导出时将分为三个文件,也就是:
vocab.txt
词典文件,每一行为一个词或词的一部分
special_tokens_map.json 特殊标记的定义方式
- {"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]",
- "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer_config.json 配置文件,主要存储特殊的配置。
Model,也就是各种各样的模型。除了初始的Bert、GPT等基本模型,针对下游任务,还定义了诸如BertForQuestionAnswering
等下游任务模型。模型导出时将生成config.json
和pytorch_model.bin
参数文件。前者就是1中的配置文件,这和我们的直觉相同,即config和model应该是紧密联系在一起的两个类。后者其实和torch.save()存储得到的文件是相同的,这是因为Model都直接或者间接继承了Pytorch的Module类。从这里可以看出,HuggingFace在实现时很好地尊重了Pytorch的原生API。
官方文档中初始教程提供的方法为:
- # Load pre-trained model (weights)
- # model = BertModel.from_pretrained('bert-base-uncased')
这个方法需要从官方的s3数据库下载模型配置、参数等信息(代码中已配置好位置)。这个方法虽然简单,但是在国内并不可用。当然你可以先尝试一下,不过会有很大的概率无法下载模型。
在HuggingFace官方模型库上找到需要下载的模型,点击模型链接, 这个例子使用的是bert-base-uncased模型
点击List all files in model,将其中的文件一一下载到同一目录中。例如,对于XLNet:
- # List of model files
- config.json 782.0B
- pytorch_model.bin 445.4MB
- special_tokens_map.json 202.0B
- spiece.model 779.3KB
- tokenizer_config.json 2.0B
但是这种方法有时也会不可用。如果您可以将Transformers预训练模型上传到迅雷等网盘的话,请在评论区告知,我会添加在此博客中,并为您添加博客友链。
通过下载好的路径导入模型:
- import transformers
- MODEL_PATH = r"D:\transformr_files\bert-base-uncased/"
- # a.通过词典导入分词器
- tokenizer = transformers.BertTokenizer.from_pretrained(r"D:\transformr_files\bert-base-uncased\bert-base-uncased-vocab.txt")
- # b. 导入配置文件
- model_config = transformers.BertConfig.from_pretrained(MODEL_PATH)
- # 修改配置
- model_config.output_hidden_states = True
- model_config.output_attentions = True
- # 通过配置和路径导入模型
- model = transformers.BertModel.from_pretrained(MODEL_PATH,config = model_config)
利用分词器进行编码
对于单句:
- # encode仅返回input_ids
- tokenizer.encode("i like you")
- Out : [101, 1045, 2066, 2017, 102]
对于多句:
- # encode_plus返回所有编码信息
- tokenizer.encode_plus("i like you", "but not him")
- Out :
- {'input_ids': [101, 1045, 2066, 2017, 102, 2021, 2025, 2032, 102],
- 'token_type_ids': [0, 0, 0, 0, 0, 1, 1, 1, 1],
- 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}
模型的所有分词器都是在PreTrainedTokenizer中实现的,分词的结果主要有以下内容:
- {
- input_ids: list[int],
- token_type_ids: list[int] if return_token_type_ids is True (default)
- attention_mask: list[int] if return_attention_mask is True (default)
- overflowing_tokens: list[int] if a max_length is specified and return_overflowing_tokens is True
- num_truncated_tokens: int if a max_length is specified and return_overflowing_tokens is True
- special_tokens_mask: list[int] if add_special_tokens if set to True and return_special_tokens_mask is True
- }
编码解释:
- # 添加batch维度并转化为tensor
- input_ids = torch.tensor([input_ids])
- token_type_ids = torch.tensor([token_type_ids])
- # 将模型转化为eval模式
- model.eval()
- # 将模型和数据转移到cuda, 若无cuda,可更换为cpu
- device = 'cuda'
- tokens_tensor = input_ids.to(device)
- segments_tensors = token_type_ids.to(device)
- model.to(device)
-
- # 进行编码
- with torch.no_grad():
- # See the models docstrings for the detail of the inputs
- outputs = model(tokens_tensor, token_type_ids=segments_tensors)
- # Transformers models always output tuples.
- # See the models docstrings for the detail of all the outputs
- # In our case, the first element is the hidden state of the last layer of the Bert model
- encoded_layers = outputs
- # 得到最终的编码结果encoded_layers

Bert最终输出的结果为:
sequence_output, pooled_output, (hidden_states), (attentions)
以输入序列长度为14为例
index | 名称 | 维度 | 描述 |
---|---|---|---|
0 | sequence_output | torch.Size([1, 14, 768]) | 输出序列 |
1 | pooled_output | torch.Size([1, 768]) | 对输出序列进行pool操作的结果 |
2 | (hidden_states) | tuple,13*torch.Size([1, 14, 768]) | 隐藏层状态(包括Embedding层),取决于modelconfig中output_hidden_states |
3 | (attentions) | tuple,12*torch.Size([1, 12, 14, 14]) | 注意力层,取决于参数中output_attentions |
这一节我们以Bert为例对模型整体的流程进行了了解。之后的很多模型都基于Bert,并基于Bert进行了少量的调整。其中的输出和输出参数也有很多重复的地方。
如开头所说,这篇文章重点在于"如何进行模型的调整以及输入输出的设定", 以及"Transformer的实现进行简要的提及", 所以,我们不会去介绍、涉及如何写train循环等话题,而仅仅专注于模型。也就是说,我们将止步于跑通一个模型,而不计批量数据预处理、训练、验证等过程。
同时,这里更看重如何基于Bert等初始模型在实际任务上进行微调,所以我们不会仅仅地导入已经在下游任务上训练好的模型参数,因为在这些模型上使用的方法和上一章的几乎完全相同。
这里的输入和输入以模型的预测过程为例。
模型的构建:
- from transformers import BertTokenizer, BertForQuestionAnswering
- import torch
-
- MODEL_PATH = r"D:\transformr_files\bert-base-uncased/"
- # 实例化tokenizer
- tokenizer = BertTokenizer.from_pretrained(r"D:\transformr_files\bert-base-uncased\bert-base-uncased-vocab.txt")
- # 导入bert的model_config
- model_config = transformers.BertConfig.from_pretrained(MODEL_PATH)
- # 首先新建bert_model
- bert_model = transformers.BertModel.from_pretrained(MODEL_PATH,config = model_config)
- # 最终有两个输出,初始位置和结束位置(下面有解释)
- model_config.num_labels = 2
- # 同样根据bert的model_config新建BertForQuestionAnswering
- model = BertForQuestionAnswering(model_config)
- model.bert = bert_model
一般情况下,一个基本模型对应一个Tokenizer, 所以并不存在对应于具体下游任务的Tokenizer。这里通过bert_model初始化BertForQuestionAnswering。
任务输入:问题句,答案所在的文章 "Who was Jim Henson?", "Jim Henson was a nice puppet"
任务输出:答案 "a nice puppet"
- # 设定模式
- model.eval()
- question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"
- # 获取input_ids编码
- input_ids = tokenizer.encode(question, text)
- # 手动进行token_type_ids编码,可用encode_plus代替
- token_type_ids = [0 if i <= input_ids.index(102) else 1 for i in range(len(input_ids))]
- # 得到评分,
- start_scores, end_scores = model(torch.tensor([input_ids]), token_type_ids=torch.tensor([token_type_ids]))
- # 进行逆编码,得到原始的token
- all_tokens = tokenizer.convert_ids_to_tokens(input_ids)
- #['[CLS]', 'who', 'was', 'jim', 'henson', '?', '[SEP]', 'jim', 'henson', 'was', 'a', 'nice', 'puppet', '[SEP]']
模型输入:inputids, token_type_ids
模型输出:start_scores, end_scores 形状都为torch.Size([1, 14])
,其中14
为序列长度,代表每个位置是开始/结束位置的概率。
将模型输出转化为任务输出:
- # 对输出的答案进行解码的过程
- answer = ' '.join(all_tokens[torch.argmax(start_scores) : torch.argmax(end_scores)+1])
- # assert answer == "a nice puppet"
- # 这里因为没有经过微调,所以效果不是很好,输出结果不佳。
- print(answer)
- # 'was jim henson ? [SEP] jim henson was a nice puppet [SEP]'
模型的构建:
- from transformers import XLNetConfig, XLNetModel, XLNetTokenizer, XLNetForSequenceClassification
- import torch
- # 定义路径,初始化tokenizer
- XLN_PATH = r"D:\transformr_files\XLNetLMHeadModel"
- tokenizer = XLNetTokenizer.from_pretrained(XLN_PATH)
- # 加载配置
- model_config = XLNetConfig.from_pretrained(XLN_PATH)
- # 设定类别数为3
- model_config.num_labels = 3
- # 直接从xlnet的config新建XLNetForSequenceClassification(和上一节方法等效)
- cls_model = XLNetForSequenceClassification.from_pretrained(XLN_PATH, config=model_config)
任务输入:句子 "i like you, what about you"
任务输出:句子所属的类别 class1
- # 设定模式
- model.eval()
- token_codes = tokenizer.encode_plus("i like you, what about you")
模型输入:inputids, token_type_ids
模型输出:logits, hidden states, 其中logits形状为torch.Size([1, 3])
, 其中的3对应的是类别的数量。当训练时,第一项为loss。
其他的模型和之前的两个大致是相同的,你可以自己发挥。我会继续在相关的库上进行实验,如果发现用法不一样的情况,将会添加在这里。
本文章主要对HuggingFace库进行了简要介绍。具体安装等过程请参见官方github仓库。
本文主要参考于官方文档
同时,在模型的理解过程中参考了一些kaggle上的notebooks, 主要是这一篇,作者是Abhishek Thakur
标签: nlp
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。