🚀 ALBERT XXLarge v2
ALBERT XXLarge v2 是一個基於英文語料庫,採用掩碼語言模型(MLM)目標進行預訓練的模型。它能夠學習英文語言的內部表示,可用於提取對下游任務有用的特徵。
🚀 快速開始
你可以直接使用此模型進行掩碼語言建模或下一句預測,但它主要用於在下游任務上進行微調。可訪問 模型中心 查找針對你感興趣任務的微調版本。
✨ 主要特性
- 雙向表示學習:通過掩碼語言建模(MLM),模型能夠學習句子的雙向表示。
- 句子順序預測:使用基於預測兩個連續文本片段順序的預訓練損失。
- 參數共享:在 Transformer 中共享層,減少內存佔用。
- 版本優化:版本 2 由於不同的丟棄率、額外的訓練數據和更長的訓練時間,在幾乎所有下游任務中都有更好的表現。
📦 安裝指南
文檔未提及安裝步驟,故跳過此章節。
💻 使用示例
基礎用法
你可以使用管道直接進行掩碼語言建模:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='albert-xxlarge-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-xxlarge-v2')
model = AlbertModel.from_pretrained("albert-xxlarge-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-xxlarge-v2')
model = TFAlbertModel.from_pretrained("albert-xxlarge-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% 的單詞,然後讓模型預測這些被掩蓋的單詞,從而學習句子的雙向表示。
- 句子順序預測(SOP):基於預測兩個連續文本片段的順序進行預訓練。
ALBERT 的特殊之處在於它在 Transformer 中共享層,所有層具有相同的權重,這使得內存佔用較小,但計算成本與具有相同隱藏層數的 BERT 架構相似。
此模型具有以下配置:
屬性 |
詳情 |
層數 |
12 個重複層 |
嵌入維度 |
128 |
隱藏維度 |
4096 |
注意力頭數 |
64 |
參數數量 |
2.23 億 |
預期用途和侷限性
該模型主要用於需要使用整個句子(可能被掩碼)進行決策的任務,如序列分類、標記分類或問答。對於文本生成等任務,建議使用 GPT2 等模型。
侷限性和偏差
即使該模型使用的訓練數據相對中立,但它仍可能存在有偏差的預測。例如:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='albert-xxlarge-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] 句子 A [SEP] 句子 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 |
🔧 技術細節
文檔未提及技術實現細節,故跳過此章節。
📄 許可證
本項目採用 Apache - 2.0 許可證。