5. PaddleNLP中的预训练模型
5.1 PaddleNLP 预训练模型介绍
与此同时,为方便用户使用,PaddleNLP提供了常用的预训练模型及其相应权重,包括从 huggingface.co 直接转换的模型权重和百度自研模型权重。 目前共包含了40多个主流预训练模型,500多个模型权重。下标列出了PaddleNLP内置的模型以及这些模型支持的常见NLP任务类型,其中各项任务解释如下:
- Sequence Classification: 针对整个文本序列进行分类,最典型的任务是文本分类
- Token Classification:针对序列中每个token进行分类,最典型的任务是命名实体识别
- Question Answering:针对Query从给定文档中进行抽取答案,最典型的任务是阅读理解
- Text Generation:文本生成任务,最典型的任务是机器翻译
Model | Sequence Classification | Token Classification | Question Answering | Text Generation |
ALBERT | ✅ | ✅ | ✅ | ❌ |
BART | ✅ | ✅ | ✅ | ✅ |
BERT | ✅ | ✅ | ✅ | ❌ |
BigBird | ✅ | ✅ | ✅ | ❌ |
Blenderbot | ❌ | ❌ | ❌ | ✅ |
Blenderbot-Small | ❌ | ❌ | ❌ | ✅ |
ChineseBert | ✅ | ✅ | ✅ | ❌ |
ConvBert | ✅ | ✅ | ✅ | ❌ |
CTRL | ✅ | ❌ | ❌ | ❌ |
DistilBert | ✅ | ✅ | ✅ | ❌ |
ELECTRA | ✅ | ✅ | ✅ | ❌ |
ERNIE | ✅ | ✅ | ✅ | ❌ |
ERNIE-CTM | ❌ | ✅ | ❌ | ❌ |
ERNIE-DOC | ✅ | ✅ | ✅ | ❌ |
ERNIE-GEN | ❌ | ❌ | ❌ | ✅ |
ERNIE-GRAM | ✅ | ✅ | ✅ | ❌ |
ERNIE-M | ✅ | ✅ | ✅ | ❌ |
FNet | ✅ | ✅ | ✅ | ❌ |
Funnel | ✅ | ✅ | ✅ | ❌ |
GPT | ✅ | ✅ | ❌ | ✅ |
LayoutLM | ✅ | ✅ | ❌ | ❌ |
LayoutLMV2 | ❌ | ✅ | ❌ | ❌ |
LayoutXLM | ❌ | ✅ | ❌ | ❌ |
Luke | ❌ | ✅ | ✅ | ❌ |
MBart | ✅ | ❌ | ✅ | ❌ |
MegatronBert | ✅ | ✅ | ✅ | ❌ |
MobileBert | ✅ | ❌ | ✅ | ❌ |
MPNet | ✅ | ✅ | ✅ | ❌ |
NeZha | ✅ | ✅ | ✅ | ❌ |
PPMiniLM | ✅ | ❌ | ❌ | ❌ |
ProphetNet | ❌ | ❌ | ❌ | ✅ |
Reformer | ✅ | ❌ | ✅ | ❌ |
RemBert | ✅ | ✅ | ✅ | ❌ |
RoBERTa | ✅ | ✅ | ✅ | ❌ |
RoFormer | ✅ | ✅ | ✅ | ❌ |
SKEP | ✅ | ✅ | ❌ | ❌ |
SqueezeBert | ✅ | ✅ | ✅ | ❌ |
T5 | ❌ | ❌ | ❌ | ✅ |
TinyBert | ✅ | ❌ | ❌ | ❌ |
UnifiedTransformer | ❌ | ❌ | ❌ | ✅ |
XLNet | ✅ | ✅ | ✅ | ❌ |
下标列出了一些比较常用的模型权重,其中每个权重由不同的语料或模型参数设置进行训练得到,能够使用于合适的应用场景,并且每个权重均有一个对应的名字,其表明了对应的模型训练设置,例如bert-base-uncased表示用不区分大小写的英文语料训练出来的base版的BERT模型,bert-base-chinese表示用中文语料训练出的base版的BERT等等。
Model | Weight name | language | Details of the model |
BERT | bert-base-uncased | English | 12-layer, 768-hidden, 12-heads, 110M parameters. |
BERT | bert-base-chinese | Chinese | 12-layer, 768-hidden, 12-heads, 108M parameters. |
Ernie | ernie-1.0-base-zh | Chinese | 12-layer, 768-hidden, 12-heads, 108M parameters. |
Ernie | ernie-3.0-base-zh | Chinese | 12-layer, 768-hidden, 12-heads, 118M parameters. |
RoBERTa | hfl/roberta-wwm-ext | Chinese | 12-layer, 768-hidden, 12-heads, 102M parameters. |
ELECTRA | chinese-electra-base | Chinese | 12-layer, 768-hidden, 12-heads, 102M parameters. |
Reformer | reformer-enwik8 | English | 12-layer, 1024-hidden, 8-heads, 148M parameters. |
GPT | gpt-cpm-large-cn | Chinese | 32-layer, 2560-hidden, 32-heads, 2.6B parameters. Trained on Chinese text. |
TinyBERT | tinybert-4l-312d-zh | Chinese | 4-layer, 312-hidden, 12-heads, 14.5M parameters. The TinyBert model distilled from the BERT model bert-base-uncased |
5.2 使用PaddleNLP加载预训练模型
使用PaddleNLP加载与训练模型非常简单,只需要以下两步便可以轻松加载预训练模型:
- 确定要加载的模型,将模型导入至当前环境
- 确定要加载的模型权重,将权重名称传入至模型中
初始情况下,PaddleNLP会下载对应的权重,并对模型进行初始化,如果模型已经下载到本地,默认会直接加载本地的模型权重。 例如,
In [7]
from paddlenlp.transformers import BertModelmodel_name = "bert-base-chinese"
model = AutoModel.from_pretrained(model_name)
[2022-08-09 21:39:46,519] [ INFO] - We are using <class 'paddlenlp.transformers.bert.modeling.BertModel'> to load 'bert-base-chinese'.
[2022-08-09 21:39:46,524] [ INFO] - Already cached /home/aistudio/.paddlenlp/models/bert-base-chinese/bert-base-chinese.pdparams
[2022-08-09 21:39:56,802] [ INFO] - Weights from pretrained model not used in BertModel: ['cls.predictions.decoder_weight', 'cls.predictions.decoder_bias', 'cls.predictions.transform.weight', 'cls.predictions.transform.bias', 'cls.predictions.layer_norm.weight', 'cls.predictions.layer_norm.bias', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias']
一般来讲,每个预训练模型的权重均有对应的tokenizer,tokenizer用于对输入文本进行分词,将文本转为对应的token序列,对该token序列进行编码,形成适合输入模型的数据形式。 tokenizer的加载同模型加载方式,同样非常方便。
下面,我们加载BERT的tokenizer,并对输入文本进行形式转换,同时将转换后的id转回原始的token,我们来比较一下其中的差异。
In [1]
from paddlenlp.transformers import BertTokenizer
from pprint import pprintmodel_name = "bert-base-chinese"
tokenizer = BertTokenizer.from_pretrained(model_name)text = "我爱深度学习"
encoded_inputs = tokenizer(text=text, return_position_ids=True)
pprint(encoded_inputs)tokens = tokenizer.convert_ids_to_tokens(encoded_inputs["input_ids"])
pprint(tokens)
[2022-08-24 16:04:56,572] [ INFO] - Downloading https://paddle-hapi.bj.bcebos.com/models/bert/bert-base-chinese-vocab.txt and saved to /home/aistudio/.paddlenlp/models/bert-base-chinese
[2022-08-24 16:04:56,576] [ INFO] - Downloading bert-base-chinese-vocab.txt from https://paddle-hapi.bj.bcebos.com/models/bert/bert-base-chinese-vocab.txt
100%|██████████| 107/107 [00:00<00:00, 3378.23it/s]
{'input_ids': [101, 2769, 4263, 3918, 2428, 2110, 739, 102],
'position_ids': [0, 1, 2, 3, 4, 5, 6, 7],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0]}
['[CLS]', '我', '爱', '深', '度', '学', '习', '[SEP]']
可以看到,tokenizer返回了input_ids、token_type_ids和position_ids,同时在序列前后增加了[CLS]和[SEP] token,以输入到BERT模型中。
另外,PaddleNLP在提供丰富预训练模型的同时,也降低了用户的使用门槛。 使用Auto模块,可以加载不同网络结构的预训练模型以及对应的tokenizer,只需要将模型的权重名称传入Auto便可以很方便地加载。
首先下载最新版本的PaddleNLP库,安装成功后,请重启AiStudio运行内核,然后便可以加载BERT模型。
In [3]
!pip install -U paddlenlp
Looking in indexes: Simple Index
。。。
Collecting pyarrow>=6.0.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/08/9b5fe7c9e2774bca77dae29d22a446ead804fb8e050f2899ae1f60d73ad1/pyarrow-9.0.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (35.3 MB)
l ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/35.3 MB ? eta -:--:--━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.1/35.3 MB 4.1 MB/s eta 0:00:09━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.3/35.3 MB 4.5 MB/s eta
...
0:00:01━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸ 35.3/35.3 MB 3.9 MB/s eta 0:00:01━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸ 35.3/35.3 MB 3.9 MB/s eta 0:00:01━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 35.3/35.3 MB 3.4 MB/s eta 0:00:00
。。。
[notice] A new release of pip available: 22.1.2 -> 22.2.2
[notice] To update, run: pip install --upgrade pip
In [2]
from paddlenlp.transformers import AutoTokenizer, AutoModel# 使用Auto模块加载tokenizer
model_name = "bert-base-chinese"
tokenizer = AutoTokenizer.from_pretrained(model_name)text = "我爱深度学习"
encoded_inputs = tokenizer(text=text, return_position_ids=True)
pprint(encoded_inputs)tokens = tokenizer.convert_ids_to_tokens(encoded_inputs["input_ids"])
pprint(tokens)# 使用Auto模块加载模型
model = AutoModel.from_pretrained(model_name)
[2022-08-10 10:39:44,088] [ INFO] - We are using <class 'paddlenlp.transformers.bert.tokenizer.BertTokenizer'> to load 'bert-base-chinese'.
[2022-08-10 10:39:44,092] [ INFO] - Already cached /home/aistudio/.paddlenlp/models/bert-base-chinese/bert-base-chinese-vocab.txt
[2022-08-10 10:39:44,114] [ INFO] - tokenizer config file saved in /home/aistudio/.paddlenlp/models/bert-base-chinese/tokenizer_config.json
[2022-08-10 10:39:44,116] [ INFO] - Special tokens file saved in /home/aistudio/.paddlenlp/models/bert-base-chinese/special_tokens_map.json
{'input_ids': [101, 2769, 4263, 3918, 2428, 2110, 739, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0], 'position_ids': [0, 1, 2, 3, 4, 5, 6, 7]}
['[CLS]', '我', '爱', '深', '度', '学', '习', '[SEP]']
[2022-08-10 10:39:44,122] [ INFO] - We are using <class 'paddlenlp.transformers.bert.modeling.BertModel'> to load 'bert-base-chinese'.
[2022-08-10 10:39:44,125] [ INFO] - Downloading http://bj.bcebos.com/paddlenlp/models/transformers/bert/bert-base-chinese.pdparams and saved to /home/aistudio/.paddlenlp/models/bert-base-chinese
[2022-08-10 10:39:44,127] [ INFO] - Downloading bert-base-chinese.pdparams from http://bj.bcebos.com/paddlenlp/models/transformers/bert/bert-base-chinese.pdparams
100%|██████████| 680M/680M [00:09<00:00, 75.2MB/s]
W0810 10:39:53.714033 1102 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0810 10:39:53.718433 1102 gpu_resources.cc:91] device: 0, cuDNN Version: 7.6.
[2022-08-10 10:40:00,105] [ INFO] - Weights from pretrained model not used in BertModel: ['cls.predictions.decoder_weight', 'cls.predictions.decoder_bias', 'cls.predictions.transform.weight', 'cls.predictions.transform.bias', 'cls.predictions.layer_norm.weight', 'cls.predictions.layer_norm.bias', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias']
可以看到,使用PaddleNLP可以非常方便地加载相应的模型,并基于这些模型进行NLP任务的实现。下一节,我们将基于PaddleNLP从数据准备、到模型构建、再到模型训练与测试的完整流程出发,实现文本匹配任务,深入讲解如何使用PaddleNLP进行训练NLP任务。