模型简介
模型特点
模型能力
使用案例
🚀 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.",
}



