distilbert/distilbert-base-uncased

Hugging Face Models Trending 模型

摘要

DistilBERT是BERT基础模型的蒸馏版本,更小更快,使用蒸馏损失、掩码语言建模和余弦嵌入损失进行训练。它旨在用于序列分类、标记分类和问答等任务的微调。

任务: fill-mask 标签: transformers, pytorch, tf, jax, rust, safetensors, distilbert, fill-mask, exbert, en, dataset:bookcorpus, dataset:wikipedia, arxiv:1910.01108, license:apache-2.0, endpoints_compatible, region:us, deploy:sagemaker, deploy:azure
查看原文
查看缓存全文

缓存时间: 2026/09/03 06:12

distilbert/distilbert-base-uncased · Hugging Face

来源:https://huggingface.co/distilbert/distilbert-base-uncased
此模型是BERT基础模型 (https://huggingface.co/bert-base-uncased) 的蒸馏版本。它在本文 (https://arxiv.org/abs/1910.01108) 中提出。蒸馏过程的代码可在此处 (https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation) 找到。该模型不区分大小写:它不会区分“english”和“English”。

模型描述

DistilBERT 是一个transformers模型,比BERT更小更快,它以自监督方式在与BERT相同的数据集上进行预训练,并使用BERT基础模型作为教师模型。这意味着它仅在原始文本上进行预训练,没有任何人工标注(因此它可以利用大量公开可用数据),并通过一个自动过程使用BERT基础模型从这些文本生成输入和标签。更准确地说,它通过三个目标进行预训练:

  • 蒸馏损失:训练模型使其输出与BERT基础模型相同的概率分布。
  • 掩码语言建模:这是BERT基础模型原始训练损失的一部分。模型在输入句子中随机掩码15%的词,然后将整个掩码句子输入模型以预测被掩码的词。这与通常逐词处理的传统循环神经网络,或像GPT这样内部掩码未来标记的自回归模型不同。它允许模型学习句子的双向表示。
  • 余弦嵌入损失:模型还被训练以生成尽可能接近BERT基础模型的隐藏状态。

通过这种方式,模型学习到了与其教师模型相同的英语内部表示,同时在推理或下游任务中速度更快。

预期用途与限制

您可以使用原始模型进行掩码语言建模或下一句预测,但其主要目的是在下游任务上进行微调。请查看模型中心 (https://huggingface.co/models?filter=distilbert) 以查找您感兴趣任务上的微调版本。

请注意,此模型主要旨在用于需要对整个句子(可能经过掩码处理)进行决策的任务,例如序列分类、标记分类或问答。对于文本生成等任务,您应该考虑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)

局限性与偏见

即使用于此模型的训练数据可以被认为是相当中性的,但该模型的预测仍可能存在偏见。它也继承了其教师模型 (https://huggingface.co/bert-base-uncased#limitations-and-bias) 的一些偏见。

>>> 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 (https://yknzhu.wixsite.com/mbweb)(一个包含11,038本未出版书籍的数据集)和英文维基百科 (https://en.wikipedia.org/wiki/English_Wikipedia)(不包括列表、表格和标题)。

训练过程

数据预处理

文本被转换为小写,并使用WordPiece分词,词汇表大小为30,000。模型的输入形式如下:

[CLS] 句子A [SEP] 句子B [SEP]

以0.5的概率,句子A和句子B对应于原始语料库中两个连续的句子,在其他情况下,它是语料库中的另一个随机句子。请注意,这里被视为“句子”的是一个连续的文本段,通常比单个句子长。唯一的约束是两个“句子”的组合长度小于512个标记。

每个句子的掩码过程细节如下:

  • 15%的标记被掩码。
  • 在80%的情况下,被掩码的标记被替换为[MASK]
  • 在10%的情况下,被掩码的标记被替换为一个随机的不同于原始标记的标记。
  • 在剩下的10%情况下,被掩码的标记保持不变。

预训练

该模型在8块16GB V100 GPU上训练了90小时。所有超参数细节请参见训练代码 (https://github.com/huggingface/transformers/tree/main/examples/research_projects/distillation)。

评估结果

在下游任务上进行微调后,该模型取得了以下结果:

GLUE测试结果:

任务MNLIQQPQNLISST-2CoLASTS-BMRPCRTE
得分82.288.589.291.351.385.887.559.9

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}
}

相似文章

google-bert/bert-base-uncased

Hugging Face Models Trending

对BERT base uncased模型的描述,这是一个在英文文本上使用掩码语言模型和下一句预测进行预训练的语言模型,可在Hugging Face上获取。

掩码蒸馏:将思维链内化到语言模型中

arXiv cs.AI

掩码蒸馏是一种知识蒸馏框架,它训练学生大语言模型仅预测解决方案令牌,同时推理教师提供反馈,旨在将思维链计算内化到模型参数中。该方法显示任务相关的成功,在GSM8K上有效,但对于像Countdown这样更困难的任务需要小型脚手架。

揭秘同策略蒸馏:其益处、危害及原因

Hugging Face Daily Papers

本文介绍了一种无需训练的框架,用于分析推理模型在逐token级别上的蒸馏信号。研究揭示,蒸馏引导在错误推理路径上更为有效,且其效果取决于学生模型的能力及任务上下文。

跨分词器LLM蒸馏:基于字节级接口的方法

Hugging Face Daily Papers

本文提出字节级蒸馏(BLD),一种简单的跨分词器知识迁移方法,通过在共享的字节级接口上操作,在1B-8B参数模型上实现了与更复杂现有方法相当或更优的性能。