模型概述
模型特點
模型能力
使用案例
🚀 CAMeLBERT:適用於阿拉伯語自然語言處理任務的預訓練模型集合
CAMeLBERT是一組針對阿拉伯語NLP任務預訓練的模型集合,涵蓋不同規模和變體,適用於多種阿拉伯語變體,可用於掩碼語言建模、下一句預測等任務,也可在各類NLP任務中微調。
🚀 快速開始
你可以直接使用此模型進行掩碼語言建模:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='CAMeL-Lab/bert-base-arabic-camelbert-mix')
>>> unmasker("الهدف من الحياة هو [MASK] .")
[{'sequence': '[CLS] الهدف من الحياة هو النجاح. [SEP]',
'score': 0.10861027985811234,
'token': 6232,
'token_str': 'النجاح'},
{'sequence': '[CLS] الهدف من الحياة هو.. [SEP]',
'score': 0.07626965641975403,
'token': 18,
'token_str': '.'},
{'sequence': '[CLS] الهدف من الحياة هو الحياة. [SEP]',
'score': 0.05131986364722252,
'token': 3696,
'token_str': 'الحياة'},
{'sequence': '[CLS] الهدف من الحياة هو الموت. [SEP]',
'score': 0.03734956309199333,
'token': 4295,
'token_str': 'الموت'},
{'sequence': '[CLS] الهدف من الحياة هو العمل. [SEP]',
'score': 0.027189988642930984,
'token': 2854,
'token_str': 'العمل'}]
注意:要下載我們的模型,你需要 transformers>=3.5.0
。否則,你可以手動下載模型。
以下是如何在 PyTorch 中使用此模型獲取給定文本的特徵:
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
model = AutoModel.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
text = "مرحبا يا عالم."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
在 TensorFlow 中使用:
from transformers import AutoTokenizer, TFAutoModel
tokenizer = AutoTokenizer.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
model = TFAutoModel.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
text = "مرحبا يا عالم."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
✨ 主要特性
- 多變體支持:提供針對現代標準阿拉伯語(MSA)、方言阿拉伯語(DA)、古典阿拉伯語(CA)以及這三種變體混合的預訓練模型。
- 規模多樣:除了標準規模的模型,還提供了按比例縮小的 MSA 變體模型(如一半、四分之一、八分之一和十六分之一規模)。
- 任務廣泛:可用於掩碼語言建模、下一句預測,也可在多種 NLP 任務(如命名實體識別、詞性標註、情感分析、方言識別和詩歌分類)中進行微調。
📦 安裝指南
要下載模型,你需要 transformers>=3.5.0
。若不滿足此條件,可手動下載模型。
💻 使用示例
基礎用法
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='CAMeL-Lab/bert-base-arabic-camelbert-mix')
>>> unmasker("الهدف من الحياة هو [MASK] .")
[{'sequence': '[CLS] الهدف من الحياة هو النجاح. [SEP]',
'score': 0.10861027985811234,
'token': 6232,
'token_str': 'النجاح'},
{'sequence': '[CLS] الهدف من الحياة هو.. [SEP]',
'score': 0.07626965641975403,
'token': 18,
'token_str': '.'},
{'sequence': '[CLS] الهدف من الحياة هو الحياة. [SEP]',
'score': 0.05131986364722252,
'token': 3696,
'token_str': 'الحياة'},
{'sequence': '[CLS] الهدف من الحياة هو الموت. [SEP]',
'score': 0.03734956309199333,
'token': 4295,
'token_str': 'الموت'},
{'sequence': '[CLS] الهدف من الحياة هو العمل. [SEP]',
'score': 0.027189988642930984,
'token': 2854,
'token_str': 'العمل'}]
高級用法
在 PyTorch 中獲取給定文本的特徵:
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
model = AutoModel.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
text = "مرحبا يا عالم."
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
在 TensorFlow 中使用:
from transformers import AutoTokenizer, TFAutoModel
tokenizer = AutoTokenizer.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
model = TFAutoModel.from_pretrained('CAMeL-Lab/bert-base-arabic-camelbert-mix')
text = "مرحبا يا عالم."
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
📚 詳細文檔
模型描述
CAMeLBERT 是一組在不同規模和變體的阿拉伯語文本上預訓練的 BERT 模型集合。我們發佈了針對現代標準阿拉伯語(MSA)、方言阿拉伯語(DA)和古典阿拉伯語(CA)的預訓練語言模型,以及一個在這三種變體混合數據上預訓練的模型。此外,我們還提供了在縮小規模的 MSA 變體數據集上預訓練的額外模型(一半、四分之一、八分之一和十六分之一規模)。詳細內容可參考論文 "The Interplay of Variant, Size, and Task Type in Arabic Pre-trained Language Models"。
本模型卡片描述的是 CAMeLBERT - Mix (bert-base-arabic-camelbert-mix
),這是一個在 MSA、DA 和 CA 混合變體數據上預訓練的模型。
模型 | 變體 | 規模 | 詞數 | |
---|---|---|---|---|
✔ | bert-base-arabic-camelbert-mix |
CA,DA,MSA | 167GB | 17.3B |
bert-base-arabic-camelbert-ca |
CA | 6GB | 847M | |
bert-base-arabic-camelbert-da |
DA | 54GB | 5.8B | |
bert-base-arabic-camelbert-msa |
MSA | 107GB | 12.6B | |
bert-base-arabic-camelbert-msa-half |
MSA | 53GB | 6.3B | |
bert-base-arabic-camelbert-msa-quarter |
MSA | 27GB | 3.1B | |
bert-base-arabic-camelbert-msa-eighth |
MSA | 14GB | 1.6B | |
bert-base-arabic-camelbert-msa-sixteenth |
MSA | 6GB | 746M |
預期用途
你可以使用發佈的模型進行掩碼語言建模或下一句預測。不過,它主要用於在 NLP 任務(如命名實體識別、詞性標註、情感分析、方言識別和詩歌分類)中進行微調。我們在 這裡 發佈了微調代碼。
訓練數據
- 現代標準阿拉伯語(MSA)
- 方言阿拉伯語(DA):我們論文 our paper 中描述的方言阿拉伯語數據集合。
- 古典阿拉伯語(CA):[OpenITI (Version 2020.1.2)](https://zenodo.org/record/3891466#.YEX4 - F0zbzc)
評估結果
- 我們在五個 NLP 任務(命名實體識別、詞性標註、情感分析、方言識別和詩歌分類)上評估了預訓練語言模型。
- 使用 12 個數據集對模型進行微調並評估。
- 使用 Hugging Face 的 transformers 庫對 CAMeLBERT 模型進行微調。
- 使用 transformers
v3.1.0
和 PyTorchv1.5.1
。 - 通過在最後隱藏層添加全連接線性層進行微調。
- 使用 \(F_{1}\) 分數作為所有任務的評估指標。
- 微調代碼可在 這裡 獲取。
結果
任務 | 數據集 | 變體 | 混合 | CA | DA | MSA | MSA - 1/2 | MSA - 1/4 | MSA - 1/8 | MSA - 1/16 |
---|---|---|---|---|---|---|---|---|---|---|
命名實體識別 | ANERcorp | MSA | 80.8% | 67.9% | 74.1% | 82.4% | 82.0% | 82.1% | 82.6% | 80.8% |
詞性標註 | PATB (MSA) | MSA | 98.1% | 97.8% | 97.7% | 98.3% | 98.2% | 98.3% | 98.2% | 98.2% |
ARZTB (EGY) | DA | 93.6% | 92.3% | 92.7% | 93.6% | 93.6% | 93.7% | 93.6% | 93.6% | |
Gumar (GLF) | DA | 97.3% | 97.7% | 97.9% | 97.9% | 97.9% | 97.9% | 97.9% | 97.9% | |
情感分析 | ASTD | MSA | 76.3% | 69.4% | 74.6% | 76.9% | 76.0% | 76.8% | 76.7% | 75.3% |
ArSAS | MSA | 92.7% | 89.4% | 91.8% | 93.0% | 92.6% | 92.5% | 92.5% | 92.3% | |
SemEval | MSA | 69.0% | 58.5% | 68.4% | 72.1% | 70.7% | 72.8% | 71.6% | 71.2% | |
方言識別 | MADAR - 26 | DA | 62.9% | 61.9% | 61.8% | 62.6% | 62.0% | 62.8% | 62.0% | 62.2% |
MADAR - 6 | DA | 92.5% | 91.5% | 92.2% | 91.9% | 91.8% | 92.2% | 92.1% | 92.0% | |
MADAR - Twitter - 5 | MSA | 75.7% | 71.4% | 74.2% | 77.6% | 78.5% | 77.3% | 77.7% | 76.2% | |
NADI | DA | 24.7% | 17.3% | 20.1% | 24.9% | 24.6% | 24.6% | 24.9% | 23.8% | |
詩歌分類 | APCD | CA | 79.8% | 80.9% | 79.6% | 79.7% | 79.9% | 80.0% | 79.7% | 79.8% |
結果(平均值)
變體 | 混合 | CA | DA | MSA | MSA - 1/2 | MSA - 1/4 | MSA - 1/8 | MSA - 1/16 | |
---|---|---|---|---|---|---|---|---|---|
變體平均[[1]](#footnote - 1) | MSA | 82.1% | 75.7% | 80.1% | 83.4% | 83.0% | 83.3% | 83.2% | 82.3% |
DA | 74.4% | 72.1% | 72.9% | 74.2% | 74.0% | 74.3% | 74.1% | 73.9% | |
CA | 79.8% | 80.9% | 79.6% | 79.7% | 79.9% | 80.0% | 79.7% | 79.8% | |
宏平均 | ALL | 78.7% | 74.7% | 77.1% | 79.2% | 79.0% | 79.2% | 79.1% | 78.6% |
[1]:變體平均是指在同一語言變體的一組任務上的平均值。
🔧 技術細節
訓練過程
我們使用 Google 發佈的 [原始實現](https://github.com/google - research/bert) 進行預訓練。除非另有說明,我們遵循原始英文 BERT 模型的超參數進行預訓練。
預處理
- 從每個語料庫中提取原始文本後,進行以下預處理:
- 首先,使用 [原始 BERT 實現](https://github.com/google - research/bert/blob/eedf5716ce1268e56f0a50264a88cafad334ac61/tokenization.py#L286 - L297) 提供的工具移除無效字符並規範化空格。
- 移除沒有任何阿拉伯字符的行。
- 使用 [CAMeL Tools](https://github.com/CAMeL - Lab/camel_tools) 移除變音符號和 kashida。
- 最後,使用基於啟發式的句子分割器將每行分割成句子。
- 使用 HuggingFace 的 tokenizers 在整個數據集(167 GB 文本)上訓練一個詞彙量為 30,000 的 WordPiece 分詞器。
- 不將字母小寫,也不去除重音。
預訓練
- 模型在單個雲 TPU (
v3 - 8
) 上總共訓練了一百萬步。 - 前 90,000 步使用批量大小為 1,024 進行訓練,其餘步驟使用批量大小為 256 進行訓練。
- 90% 的步驟中序列長度限制為 128 個標記,其餘 10% 的步驟中序列長度限制為 512 個標記。
- 使用全詞掩碼和重複因子為 10。
- 對於最大序列長度為 128 個標記的數據集,設置每個序列的最大預測數為 20;對於最大序列長度為 512 個標記的數據集,設置為 80。
- 使用隨機種子 12345,掩碼語言模型概率為 0.15,短序列概率為 0.1。
- 使用的優化器是 Adam,學習率為 1e - 4,\(\beta_{1} = 0.9\),\(\beta_{2} = 0.999\),權重衰減為 0.01,學習率在 10,000 步內熱身,之後線性衰減。
📄 許可證
本項目採用 Apache - 2.0 許可證。
致謝
本研究得到了 Google 的 TensorFlow 研究雲(TFRC)的 Cloud TPU 支持。
引用
@inproceedings{inoue-etal-2021-interplay,
title = "The Interplay of Variant, Size, and Task Type in {A}rabic Pre-trained Language Models",
author = "Inoue, Go and
Alhafni, Bashar and
Baimukan, Nurpeiis and
Bouamor, Houda and
Habash, Nizar",
booktitle = "Proceedings of the Sixth Arabic Natural Language Processing Workshop",
month = apr,
year = "2021",
address = "Kyiv, Ukraine (Online)",
publisher = "Association for Computational Linguistics",
abstract = "In this paper, we explore the effects of language variants, data sizes, and fine-tuning task types in Arabic pre-trained language models. To do so, we build three pre-trained language models across three variants of Arabic: Modern Standard Arabic (MSA), dialectal Arabic, and classical Arabic, in addition to a fourth language model which is pre-trained on a mix of the three. We also examine the importance of pre-training data size by building additional models that are pre-trained on a scaled-down set of the MSA variant. We compare our different models to each other, as well as to eight publicly available models by fine-tuning them on five NLP tasks spanning 12 datasets. Our results suggest that the variant proximity of pre-training data to fine-tuning data is more important than the pre-training data size. We exploit this insight in defining an optimized system selection model for the studied tasks.",
}



