language: zh
license: apache-2.0
datasets:
DistilBERT基础模型(区分大小写)模型卡
该模型是BERT基础模型的蒸馏版本,由这篇论文提出。蒸馏过程的代码可在此处找到。此模型区分大小写:例如"english"和"English"会被视为不同词汇。
关于预训练的所有细节、用途、限制及潜在偏见(如下所述)均与DistilBERT-base-uncased相同。如需了解更多信息,强烈建议查阅该模型。
模型描述
DistilBERT是一种基于Transformer的模型,比BERT更小更快,通过自监督方式在同一语料库上进行预训练,并以BERT基础模型作为教师模型。这意味着它仅对原始文本进行预训练,无需人工标注(因此可利用大量公开数据),通过BERT基础模型自动生成输入和标签。具体而言,其预训练包含三个目标:
- 蒸馏损失:模型被训练以输出与BERT基础模型相同的概率分布。
- 掩码语言建模(MLM):这是BERT基础模型原始训练损失的一部分。模型随机掩码输入中15%的词汇,然后处理整个掩码句子并预测被掩码的词汇。这与传统的循环神经网络(RNN)逐词处理或GPT等自回归模型内部掩码未来标记不同,使模型能学习句子的双向表示。
- 余弦嵌入损失:模型还被训练以生成与BERT基础模型尽可能接近的隐藏状态。
通过这种方式,模型在学习英语内部表示的同时,推理或下游任务速度更快。
预期用途与限制
原始模型可用于掩码语言建模或下一句预测,但主要目的是在下游任务上进行微调。请参阅模型中心寻找您感兴趣任务的微调版本。
注意,此模型主要针对使用整个句子(可能被掩码)进行决策的任务(如序列分类、标记分类或问答)进行微调。对于文本生成等任务,应使用GPT2等模型。
使用方法
可直接使用管道进行掩码语言建模:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='distilbert-base-uncased')
>>> unmasker("Hello I'm a [MASK] model.")
[{'sequence': "[CLS] hello i'm a role model. [SEP]",
'score': 0.05292855575680733,
'token': 2535,
'token_str': 'role'},
{'sequence': "[CLS] hello i'm a fashion model. [SEP]",
'score': 0.03968575969338417,
'token': 4827,
'token_str': 'fashion'},
{'sequence': "[CLS] hello i'm a business model. [SEP]",
'score': 0.034743521362543106,
'token': 2449,
'token_str': 'business'},
{'sequence': "[CLS] hello i'm a model model. [SEP]",
'score': 0.03462274372577667,
'token': 2944,
'token_str': 'model'},
{'sequence': "[CLS] hello i'm a modeling model. [SEP]",
'score': 0.018145186826586723,
'token': 11643,
'token_str': 'modeling'}]
在PyTorch中获取文本特征:
from transformers import DistilBertTokenizer, DistilBertModel
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = DistilBertModel.from_pretrained("distilbert-base-uncased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
在TensorFlow中:
from transformers import DistilBertTokenizer, TFDistilBertModel
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
model = TFDistilBertModel.from_pretrained("distilbert-base-uncased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
限制与偏见
尽管训练数据相对中立,此模型可能存在预测偏见,并继承了教师模型的部分偏见。
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='distilbert-base-uncased')
>>> unmasker("The White man worked as a [MASK].")
[{'sequence': '[CLS] the white man worked as a blacksmith. [SEP]',
'score': 0.1235365942120552,
'token': 20987,
'token_str': 'blacksmith'},
{'sequence': '[CLS] the white man worked as a carpenter. [SEP]',
'score': 0.10142576694488525,
'token': 10533,
'token_str': 'carpenter'},
{'sequence': '[CLS] the white man worked as a farmer. [SEP]',
'score': 0.04985016956925392,
'token': 7500,
'token_str': 'farmer'},
{'sequence': '[CLS] the white man worked as a miner. [SEP]',
'score': 0.03932540491223335,
'token': 18594,
'token_str': 'miner'},
{'sequence': '[CLS] the white man worked as a butcher. [SEP]',
'score': 0.03351764753460884,
'token': 14998,
'token_str': 'butcher'}]
>>> unmasker("The Black woman worked as a [MASK].")
[{'sequence': '[CLS] the black woman worked as a waitress. [SEP]',
'score': 0.13283951580524445,
'token': 13877,
'token_str': 'waitress'},
{'sequence': '[CLS] the black woman worked as a nurse. [SEP]',
'score': 0.12586183845996857,
'token': 6821,
'token_str': 'nurse'},
{'sequence': '[CLS] the black woman worked as a maid. [SEP]',
'score': 0.11708822101354599,
'token': 10850,
'token_str': 'maid'},
{'sequence': '[CLS] the black woman worked as a prostitute. [SEP]',
'score': 0.11499975621700287,
'token': 19215,
'token_str': 'prostitute'},
{'sequence': '[CLS] the black woman worked as a housekeeper. [SEP]',
'score': 0.04722772538661957,
'token': 22583,
'token_str': 'housekeeper'}]
这种偏见也会影响该模型的所有微调版本。
训练数据
DistilBERT的预训练数据与BERT相同,包括BookCorpus(11,038本未出版书籍)和英文维基百科(排除列表、表格和标题)。
训练流程
预处理
文本经过小写转换,并使用30,000词表的WordPiece进行分词。模型输入格式如下:
[CLS] 句子A [SEP] 句子B [SEP]
50%的情况下,句子A和B是原始语料中的连续句子;其余情况下,B为语料中的随机句子。这里的“句子”指通常比单句更长的连续文本,唯一限制是合并长度不超过512个标记。
掩码过程细节:
- 15%的标记被掩码。
- 80%的情况下,掩码标记替换为
[MASK]
。
- 10%的情况下,替换为随机标记。
- 剩余10%保持原样。
预训练
模型在8块16GB V100上训练90小时。所有超参数详见训练代码。
评估结果
下游任务微调后的结果:
Glue测试结果:
任务 |
MNLI |
QQP |
QNLI |
SST-2 |
CoLA |
STS-B |
MRPC |
RTE |
|
81.5 |
87.8 |
88.2 |
90.4 |
47.2 |
85.5 |
85.6 |
60.6 |
BibTeX条目与引用信息
@article{Sanh2019DistilBERTAD,
title={DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter},
author={Victor Sanh and Lysandre Debut and Julien Chaumond and Thomas Wolf},
journal={ArXiv},
year={2019},
volume={abs/1910.01108}
}