模型概述
模型特點
模型能力
使用案例
🚀 ALBERT Base v2
ALBERT Base v2是一個基於英文語料庫,通過掩碼語言模型(MLM)目標進行預訓練的模型。它能學習到英文語言的內在表示,為下游任務提取有用特徵,在多個自然語言處理任務中表現出色。
🚀 快速開始
你可以直接使用該模型進行掩碼語言建模任務。以下是使用示例:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='albert-base-v2')
>>> unmasker("Hello I'm a [MASK] model.")
[
{
"sequence":"[CLS] hello i'm a modeling model.[SEP]",
"score":0.05816134437918663,
"token":12807,
"token_str":"▁modeling"
},
{
"sequence":"[CLS] hello i'm a modelling model.[SEP]",
"score":0.03748830780386925,
"token":23089,
"token_str":"▁modelling"
},
{
"sequence":"[CLS] hello i'm a model model.[SEP]",
"score":0.033725276589393616,
"token":1061,
"token_str":"▁model"
},
{
"sequence":"[CLS] hello i'm a runway model.[SEP]",
"score":0.017313428223133087,
"token":8014,
"token_str":"▁runway"
},
{
"sequence":"[CLS] hello i'm a lingerie model.[SEP]",
"score":0.014405295252799988,
"token":29104,
"token_str":"▁lingerie"
}
]
✨ 主要特性
- 自監督預訓練:在大量英文數據語料庫上以自監督方式進行預訓練,通過自動處理從原始文本生成輸入和標籤,學習到英文語言的內在表示。
- 雙目標預訓練:採用掩碼語言建模(MLM)和句子順序預測(SOP)兩個目標進行預訓練,能夠學習句子的雙向表示。
- 參數共享:在Transformer中共享層,所有層具有相同的權重,內存佔用小,但計算成本與具有相同隱藏層數的BERT架構相似。
- 版本優化:版本2相較於版本1,由於不同的丟棄率、額外的訓練數據和更長的訓練時間,在幾乎所有下游任務中都有更好的表現。
📦 安裝指南
文檔未提及安裝步驟,跳過該章節。
💻 使用示例
基礎用法
使用該模型進行掩碼語言建模:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='albert-base-v2')
>>> unmasker("Hello I'm a [MASK] model.")
[
{
"sequence":"[CLS] hello i'm a modeling model.[SEP]",
"score":0.05816134437918663,
"token":12807,
"token_str":"▁modeling"
},
{
"sequence":"[CLS] hello i'm a modelling model.[SEP]",
"score":0.03748830780386925,
"token":23089,
"token_str":"▁modelling"
},
{
"sequence":"[CLS] hello i'm a model model.[SEP]",
"score":0.033725276589393616,
"token":1061,
"token_str":"▁model"
},
{
"sequence":"[CLS] hello i'm a runway model.[SEP]",
"score":0.017313428223133087,
"token":8014,
"token_str":"▁runway"
},
{
"sequence":"[CLS] hello i'm a lingerie model.[SEP]",
"score":0.014405295252799988,
"token":29104,
"token_str":"▁lingerie"
}
]
高級用法
在PyTorch中獲取給定文本的特徵:
from transformers import AlbertTokenizer, AlbertModel
tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
model = AlbertModel.from_pretrained("albert-base-v2")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
在TensorFlow中獲取給定文本的特徵:
from transformers import AlbertTokenizer, TFAlbertModel
tokenizer = AlbertTokenizer.from_pretrained('albert-base-v2')
model = TFAlbertModel.from_pretrained("albert-base-v2")
text = "Replace me by any text you'd like."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📚 詳細文檔
模型描述
ALBERT是一個基於Transformer架構的模型,在大量英文數據語料庫上以自監督方式進行預訓練。它通過自動處理從原始文本生成輸入和標籤,無需人工標註。具體來說,它採用了兩個預訓練目標:
- 掩碼語言建模(MLM):隨機掩蓋輸入句子中15%的單詞,然後讓模型預測這些被掩蓋的單詞。與傳統的循環神經網絡(RNN)和自迴歸模型(如GPT)不同,MLM允許模型學習句子的雙向表示。
- 句子順序預測(SOP):基於預測兩個連續文本片段的順序的預訓練損失。
通過這兩個目標,模型學習到英文語言的內在表示,可用於下游任務的特徵提取。
ALBERT的特殊之處在於它在Transformer中共享層,所有層具有相同的權重。使用重複層可以減少內存佔用,但計算成本與具有相同隱藏層數的BERT架構相似。
這是基礎模型的第二個版本。版本2與版本1不同,由於不同的丟棄率、額外的訓練數據和更長的訓練時間,在幾乎所有下游任務中都有更好的表現。
該模型具有以下配置:
屬性 | 詳情 |
---|---|
模型類型 | ALBERT Base v2 |
訓練數據 | BookCorpus(包含11,038本未出版書籍)和英文維基百科(不包括列表、表格和標題) |
層數 | 12個重複層 |
嵌入維度 | 128 |
隱藏維度 | 768 |
注意力頭數 | 12 |
參數數量 | 11M |
預期用途和限制
你可以使用原始模型進行掩碼語言建模或下一句預測,但它主要用於在下游任務上進行微調。請參閱模型中心,查找你感興趣的任務的微調版本。
請注意,該模型主要用於需要使用整個句子(可能被掩碼)來做出決策的任務,如序列分類、標記分類或問答。對於文本生成等任務,你應該考慮使用GPT2等模型。
侷限性和偏差
即使該模型使用的訓練數據可以被認為是相當中立的,但它仍然可能產生有偏差的預測:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='albert-base-v2')
>>> unmasker("The man worked as a [MASK].")
[
{
"sequence":"[CLS] the man worked as a chauffeur.[SEP]",
"score":0.029577180743217468,
"token":28744,
"token_str":"▁chauffeur"
},
{
"sequence":"[CLS] the man worked as a janitor.[SEP]",
"score":0.028865724802017212,
"token":29477,
"token_str":"▁janitor"
},
{
"sequence":"[CLS] the man worked as a shoemaker.[SEP]",
"score":0.02581118606030941,
"token":29024,
"token_str":"▁shoemaker"
},
{
"sequence":"[CLS] the man worked as a blacksmith.[SEP]",
"score":0.01849772222340107,
"token":21238,
"token_str":"▁blacksmith"
},
{
"sequence":"[CLS] the man worked as a lawyer.[SEP]",
"score":0.01820771023631096,
"token":3672,
"token_str":"▁lawyer"
}
]
>>> unmasker("The woman worked as a [MASK].")
[
{
"sequence":"[CLS] the woman worked as a receptionist.[SEP]",
"score":0.04604868218302727,
"token":25331,
"token_str":"▁receptionist"
},
{
"sequence":"[CLS] the woman worked as a janitor.[SEP]",
"score":0.028220869600772858,
"token":29477,
"token_str":"▁janitor"
},
{
"sequence":"[CLS] the woman worked as a paramedic.[SEP]",
"score":0.0261906236410141,
"token":23386,
"token_str":"▁paramedic"
},
{
"sequence":"[CLS] the woman worked as a chauffeur.[SEP]",
"score":0.024797942489385605,
"token":28744,
"token_str":"▁chauffeur"
},
{
"sequence":"[CLS] the woman worked as a waitress.[SEP]",
"score":0.024124596267938614,
"token":13678,
"token_str":"▁waitress"
}
]
這種偏差也會影響該模型的所有微調版本。
訓練數據
ALBERT模型在BookCorpus和英文維基百科(不包括列表、表格和標題)上進行預訓練。
訓練過程
預處理
文本首先進行小寫處理,然後使用SentencePiece進行分詞,詞彙表大小為30,000。模型的輸入形式如下:
[CLS] Sentence A [SEP] Sentence B [SEP]
訓練
ALBERT的訓練過程遵循BERT的設置。每個句子的掩碼過程細節如下:
- 15%的標記被掩蓋。
- 在80%的情況下,被掩蓋的標記被替換為
[MASK]
。 - 在10%的情況下,被掩蓋的標記被替換為一個隨機標記。
- 在剩下的10%的情況下,被掩蓋的標記保持不變。
評估結果
在下游任務上進行微調時,ALBERT模型取得了以下結果:
平均 | SQuAD1.1 | SQuAD2.0 | MNLI | SST-2 | RACE | |
---|---|---|---|---|---|---|
V2 | ||||||
ALBERT-base | 82.3 | 90.2/83.2 | 82.1/79.3 | 84.6 | 92.9 | 66.8 |
ALBERT-large | 85.7 | 91.8/85.2 | 84.9/81.8 | 86.5 | 94.9 | 75.2 |
ALBERT-xlarge | 87.9 | 92.9/86.4 | 87.9/84.1 | 87.9 | 95.4 | 80.7 |
ALBERT-xxlarge | 90.9 | 94.6/89.1 | 89.8/86.9 | 90.6 | 96.8 | 86.8 |
V1 | ||||||
ALBERT-base | 80.1 | 89.3/82.3 | 80.0/77.1 | 81.6 | 90.3 | 64.0 |
ALBERT-large | 82.4 | 90.6/83.9 | 82.3/79.4 | 83.5 | 91.7 | 68.5 |
ALBERT-xlarge | 85.5 | 92.5/86.1 | 86.1/83.1 | 86.4 | 92.4 | 74.8 |
ALBERT-xxlarge | 91.0 | 94.8/89.3 | 90.2/87.4 | 90.8 | 96.9 | 86.5 |
BibTeX引用
@article{DBLP:journals/corr/abs-1909-11942,
author = {Zhenzhong Lan and
Mingda Chen and
Sebastian Goodman and
Kevin Gimpel and
Piyush Sharma and
Radu Soricut},
title = {{ALBERT:} {A} Lite {BERT} for Self-supervised Learning of Language
Representations},
journal = {CoRR},
volume = {abs/1909.11942},
year = {2019},
url = {http://arxiv.org/abs/1909.11942},
archivePrefix = {arXiv},
eprint = {1909.11942},
timestamp = {Fri, 27 Sep 2019 13:04:21 +0200},
biburl = {https://dblp.org/rec/journals/corr/abs-1909-11942.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}
🔧 技術細節
文檔未提供具體的技術實現細節,跳過該章節。
📄 許可證
該模型採用Apache 2.0許可證。



