模型概述
模型特點
模型能力
使用案例
🚀 BERT基礎模型(無大小寫區分)
BERT基礎模型(無大小寫區分)是一個使用掩碼語言建模(MLM)目標在英語語料上進行預訓練的模型。它能夠學習英語語言的內在表示,可用於提取對下游任務有用的特徵,為自然語言處理任務提供強大的支持。
🚀 快速開始
你可以直接使用這個模型進行掩碼語言建模,以下是使用示例:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
>>> unmasker("Hello I'm a [MASK] model.")
[{'sequence': "[CLS] hello i'm a fashion model. [SEP]",
'score': 0.1073106899857521,
'token': 4827,
'token_str': 'fashion'},
{'sequence': "[CLS] hello i'm a role model. [SEP]",
'score': 0.08774490654468536,
'token': 2535,
'token_str': 'role'},
{'sequence': "[CLS] hello i'm a new model. [SEP]",
'score': 0.05338378623127937,
'token': 2047,
'token_str': 'new'},
{'sequence': "[CLS] hello i'm a super model. [SEP]",
'score': 0.04667217284440994,
'token': 3565,
'token_str': 'super'},
{'sequence': "[CLS] hello i'm a fine model. [SEP]",
'score': 0.027095865458250046,
'token': 2986,
'token_str': 'fine'}]
以下是在PyTorch中使用此模型獲取給定文本特徵的方法:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained("bert-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 BertTokenizer, TFBertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = TFBertModel.from_pretrained("bert-base-uncased")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
✨ 主要特性
- 雙向表示學習:通過掩碼語言建模(MLM),模型能夠學習句子的雙向表示,這與傳統的循環神經網絡(RNNs)和自迴歸模型(如GPT)不同。
- 句子關係理解:通過下一句預測(NSP),模型可以學習句子之間的關係,判斷兩個句子是否在原文中相鄰。
- 可用於下游任務:學習到的英語語言內在表示可用於提取對下游任務有用的特徵,如序列分類、標記分類或問答等。
📚 詳細文檔
模型描述
BERT是一個基於Transformer架構的模型,它以自監督的方式在大量英語數據語料上進行預訓練。這意味著它僅在原始文本上進行預訓練,沒有人工對其進行任何標註(因此可以使用大量公開可用的數據),並通過自動過程從這些文本中生成輸入和標籤。更準確地說,它通過兩個目標進行預訓練:
- 掩碼語言建模(MLM):對於一個句子,模型隨機掩碼輸入中15%的單詞,然後將整個掩碼後的句子輸入模型,並預測被掩碼的單詞。這與傳統的循環神經網絡(RNNs)通常逐個查看單詞,或自迴歸模型(如GPT)內部掩碼未來標記的方式不同。它允許模型學習句子的雙向表示。
- 下一句預測(NSP):在預訓練期間,模型將兩個掩碼後的句子作為輸入進行拼接。有時它們對應於原文中相鄰的句子,有時則不是。然後模型需要預測這兩個句子是否相鄰。
預期用途與限制
你可以使用原始模型進行掩碼語言建模或下一句預測,但它主要用於在下游任務上進行微調。請參閱模型中心以查找針對你感興趣的任務進行微調的版本。
請注意,此模型主要旨在針對使用整個句子(可能是掩碼後的)進行決策的任務進行微調,例如序列分類、標記分類或問答。對於文本生成等任務,你應該考慮使用像GPT2這樣的模型。
侷限性和偏差
即使此模型使用的訓練數據可以被描述為相當中立,但該模型仍可能存在有偏差的預測:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
>>> unmasker("The man worked as a [MASK].")
[{'sequence': '[CLS] the man worked as a carpenter. [SEP]',
'score': 0.09747550636529922,
'token': 10533,
'token_str': 'carpenter'},
{'sequence': '[CLS] the man worked as a waiter. [SEP]',
'score': 0.0523831807076931,
'token': 15610,
'token_str': 'waiter'},
{'sequence': '[CLS] the man worked as a barber. [SEP]',
'score': 0.04962705448269844,
'token': 13362,
'token_str': 'barber'},
{'sequence': '[CLS] the man worked as a mechanic. [SEP]',
'score': 0.03788609802722931,
'token': 15893,
'token_str': 'mechanic'},
{'sequence': '[CLS] the man worked as a salesman. [SEP]',
'score': 0.037680890411138535,
'token': 18968,
'token_str': 'salesman'}]
>>> unmasker("The woman worked as a [MASK].")
[{'sequence': '[CLS] the woman worked as a nurse. [SEP]',
'score': 0.21981462836265564,
'token': 6821,
'token_str': 'nurse'},
{'sequence': '[CLS] the woman worked as a waitress. [SEP]',
'score': 0.1597415804862976,
'token': 13877,
'token_str': 'waitress'},
{'sequence': '[CLS] the woman worked as a maid. [SEP]',
'score': 0.1154729500412941,
'token': 10850,
'token_str': 'maid'},
{'sequence': '[CLS] the woman worked as a prostitute. [SEP]',
'score': 0.037968918681144714,
'token': 19215,
'token_str': 'prostitute'},
{'sequence': '[CLS] the woman worked as a cook. [SEP]',
'score': 0.03042375110089779,
'token': 5660,
'token_str': 'cook'}]
這種偏差也會影響此模型的所有微調版本。
訓練數據
BERT模型在BookCorpus和英文維基百科(不包括列表、表格和標題)上進行預訓練。BookCorpus是一個包含11,038本未出版書籍的數據集。
訓練過程
預處理
文本先進行小寫處理,然後使用WordPiece進行分詞,詞彙表大小為30,000。模型的輸入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
句子A和句子B有0.5的概率對應於原始語料庫中連續的兩個句子,在其他情況下,是語料庫中的另一個隨機句子。請注意,這裡所說的“句子”通常是指比單個句子更長的連續文本片段。唯一的限制是兩個“句子”組合後的長度小於512個標記。
每個句子的掩碼過程細節如下:
- 15%的標記被掩碼。
- 在80%的情況下,被掩碼的標記被
[MASK]
替換。 - 在10%的情況下,被掩碼的標記被一個與它們不同的隨機標記替換。
- 在剩下10%的情況下,被掩碼的標記保持不變。
預訓練
模型在4個雲TPU的Pod配置(總共16個TPU芯片)上訓練了100萬步,批次大小為256。在90%的步驟中,序列長度限制為128個標記,在剩下10%的步驟中為512個標記。使用的優化器是Adam,學習率為1e-4,\(\beta_{1} = 0.9\),\(\beta_{2} = 0.999\),權重衰減為0.01,學習率在10,000步內進行預熱,然後線性衰減。
評估結果
在下游任務上進行微調時,此模型取得了以下結果:
Glue測試結果:
任務 | MNLI-(m/mm) | QQP | QNLI | SST-2 | CoLA | STS-B | MRPC | RTE | 平均 |
---|---|---|---|---|---|---|---|---|---|
84.6/83.4 | 71.2 | 90.5 | 93.5 | 52.1 | 85.8 | 88.9 | 66.4 | 79.6 |
🔧 技術細節
輸入格式
[CLS] Sentence A [SEP] Sentence B [SEP]
掩碼規則
- 15%的標記被掩碼。
- 80%被
[MASK]
替換。 - 10%被隨機標記替換。
- 10%保持不變。
訓練參數
- 硬件:4個雲TPU的Pod配置(16個TPU芯片)
- 步數:100萬步
- 批次大小:256
- 序列長度:90%步驟為128,10%步驟為512
- 優化器:Adam
- 學習率:1e - 4
- \(\beta_{1}\):0.9
- \(\beta_{2}\):0.999
- 權重衰減:0.01
- 學習率預熱:10,000步
📄 許可證
本項目採用Apache-2.0許可證。
BibTeX引用和引用信息
@article{DBLP:journals/corr/abs-1810-04805,
author = {Jacob Devlin and
Ming{-}Wei Chang and
Kenton Lee and
Kristina Toutanova},
title = {{BERT:} Pre-training of Deep Bidirectional Transformers for Language
Understanding},
journal = {CoRR},
volume = {abs/1810.04805},
year = {2018},
url = {http://arxiv.org/abs/1810.04805},
archivePrefix = {arXiv},
eprint = {1810.04805},
timestamp = {Tue, 30 Oct 2018 20:39:56 +0100},
biburl = {https://dblp.org/rec/journals/corr/abs-1810-04805.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}




