模型概述
模型特點
模型能力
使用案例
🚀 DistilBERT基礎模型(無大小寫區分)
DistilBERT基礎模型(無大小寫區分)是BERT基礎模型的蒸餾版本,它在相同語料庫上以自監督方式進行預訓練,速度更快且體積更小,可用於掩碼語言建模、下一句預測等任務,也適合在下游任務中進行微調。
🚀 快速開始
你可以直接使用此模型進行掩碼語言建模,也可以將其用於下游任務的微調。以下是使用該模型進行掩碼語言建模的示例代碼:
>>> 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'}]
✨ 主要特性
- 體積小、速度快:DistilBERT是一個比BERT更小、更快的Transformer模型。
- 自監督預訓練:在相同語料庫上以自監督方式進行預訓練,使用BERT基礎模型作為教師模型。
- 學習雙向表示:通過掩碼語言建模(MLM)任務,模型可以學習句子的雙向表示。
- 多任務適用性:可用於掩碼語言建模、下一句預測等任務,也適合在下游任務中進行微調。
📦 安裝指南
你可以使用transformers
庫來使用這個模型,安裝命令如下:
pip install transformers
💻 使用示例
基礎用法
以下是使用該模型進行掩碼語言建模的示例代碼:
>>> 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和TensorFlow中的使用方法:
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)
📚 詳細文檔
預期用途和限制
你可以使用原始模型進行掩碼語言建模或下一句預測,但它主要用於在下游任務中進行微調。你可以在模型中心查找針對你感興趣的任務進行微調的版本。
需要注意的是,該模型主要旨在針對使用整個句子(可能經過掩碼處理)進行決策的任務進行微調,例如序列分類、標記分類或問答任務。對於文本生成等任務,你應該考慮使用像GPT2這樣的模型。
侷限性和偏差
即使該模型使用的訓練數據可以被描述為相當中立,但該模型的預測仍可能存在偏差。它還繼承了其教師模型的一些偏差。這種偏差也會影響該模型的所有微調版本。
訓練數據
DistilBERT在與BERT相同的數據上進行預訓練,這些數據包括BookCorpus(一個由11,038本未出版書籍組成的數據集)和英文維基百科(不包括列表、表格和標題)。
訓練過程
預處理
文本先轉換為小寫,然後使用WordPiece進行分詞,詞彙表大小為30,000。模型的輸入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
有50%的概率,句子A和句子B對應原始語料庫中的兩個連續句子,在其他情況下,句子B是語料庫中的另一個隨機句子。這裡的“句子”通常是指比單個句子更長的連續文本片段。唯一的限制是兩個“句子”組合後的長度小於512個標記。
每個句子的掩碼處理細節如下:
- 15%的標記被掩碼。
- 在80%的情況下,被掩碼的標記被替換為
[MASK]
。 - 在10%的情況下,被掩碼的標記被替換為一個與它們不同的隨機標記。
- 在剩下的10%的情況下,被掩碼的標記保持不變。
預訓練
該模型在8個16GB的V100 GPU上訓練了90小時。有關所有超參數的詳細信息,請參閱訓練代碼。
評估結果
在下游任務上進行微調時,該模型取得了以下結果:
Glue測試結果:
任務 | MNLI | QQP | QNLI | SST - 2 | CoLA | STS - B | MRPC | RTE |
---|---|---|---|---|---|---|---|---|
82.2 | 88.5 | 89.2 | 91.3 | 51.3 | 85.8 | 87.5 | 59.9 |
🔧 技術細節
DistilBERT是一個Transformer模型,它比BERT更小、更快,並且在相同的語料庫上以自監督方式進行預訓練,使用BERT基礎模型作為教師模型。具體來說,它在預訓練時具有三個目標:
- 蒸餾損失:訓練模型以返回與BERT基礎模型相同的概率。
- 掩碼語言建模(MLM):這是BERT基礎模型原始訓練損失的一部分。在處理句子時,模型會隨機掩碼輸入中15%的單詞,然後將整個掩碼後的句子輸入模型,並預測被掩碼的單詞。這與傳統的循環神經網絡(RNN)不同,RNN通常逐個處理單詞,也與像GPT這樣的自迴歸模型不同,GPT會在內部掩碼未來的標記。這種方式允許模型學習句子的雙向表示。
- 餘弦嵌入損失:訓練模型生成與BERT基礎模型儘可能接近的隱藏狀態。
通過這種方式,模型學習到與教師模型相同的英語語言內部表示,同時在推理或下游任務中速度更快。
📄 許可證
本項目採用Apache 2.0許可證。
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}
}




