英文

CamemBERT: 一种美味的法语语言模型

目录

  • 模型详情
  • 用途
  • 风险、限制和偏见
  • 训练
  • 评估
  • 引用信息
  • 如何开始使用该模型

模型详情

  • 模型描述:CamemBERT是基于RoBERTa模型的法语最先进的语言模型。它现在在Hugging Face上以6个不同版本提供,参数数量、预训练数据量和预训练数据源域也有所不同。
  • 开发者:Louis Martin*,Benjamin Muller*,Pedro Javier Ortiz Suárez*,Yoann Dupont,Laurent Romary,Éric Villemonte de la Clergerie,Djamé Seddah和Benoît Sagot。
  • 模型类型:填充掩码
  • 语言:法语
  • 许可证:MIT
  • 父模型:有关RoBERTa基本模型的更多信息,请参阅 RoBERTa base model
  • 更多信息资源:

用途

直接用途

该模型可用于填充掩码任务。

风险、限制和偏见

内容警告:读者应该知道,本部分包含令人不安、冒犯性和能够传播历史和当前刻板印象的内容。

已进行了大量研究,探讨了语言模型的偏见和公平性问题(参见,例如, Sheng et al. (2021) Bender et al. (2021) )。

该模型在OSCAR多语言语料库的一个子语料库上进行了预训练。与OSCAR数据集相关的一些限制和风险在 OSCAR dataset card 中详细说明,包括:

某些OSCAR子语料库的质量可能低于预期,特别是对于资源最低的语言。

构建自Common Crawl,可能存在个人和敏感信息。

训练

训练数据

OSCAR或Open Super-large Crawled Aggregated coRpus是通过使用Ungoliant架构对Common Crawl语料库进行语言分类和过滤而获得的多语种语料库。

训练过程
Model #params Arch. Training data
camembert-base 110M Base OSCAR (138 GB of text)
camembert/camembert-large 335M Large CCNet (135 GB of text)
camembert/camembert-base-ccnet 110M Base CCNet (135 GB of text)
camembert/camembert-base-wikipedia-4gb 110M Base Wikipedia (4 GB of text)
camembert/camembert-base-oscar-4gb 110M Base Subsample of OSCAR (4 GB of text)
camembert/camembert-base-ccnet-4gb 110M Base Subsample of CCNet (4 GB of text)

评估

模型开发者使用四种不同的法语下游任务对CamemBERT进行了评估:词性标注(POS)、依存句法分析、命名实体识别(NER)和自然语言推理(NLI)。

引用信息

@inproceedings{martin2020camembert,
  title={CamemBERT: a Tasty French Language Model},
  author={Martin, Louis and Muller, Benjamin and Su{\'a}rez, Pedro Javier Ortiz and Dupont, Yoann and Romary, Laurent and de la Clergerie, {\'E}ric Villemonte and Seddah, Djam{\'e} and Sagot, Beno{\^\i}t},
  booktitle={Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics},
  year={2020}
}

如何开始使用该模型

加载CamemBERT及其子词标记器:
from transformers import CamembertModel, CamembertTokenizer

# You can replace "camembert-base" with any other model from the table, e.g. "camembert/camembert-large".
tokenizer = CamembertTokenizer.from_pretrained("camembert-base")
camembert = CamembertModel.from_pretrained("camembert-base")

camembert.eval()  # disable dropout (or leave in train mode to finetune)
使用管道方法填充掩码:
from transformers import pipeline 

camembert_fill_mask  = pipeline("fill-mask", model="camembert-base", tokenizer="camembert-base")
results = camembert_fill_mask("Le camembert est <mask> :)")
# results
#[{'sequence': '<s> Le camembert est délicieux :)</s>', 'score': 0.4909103214740753, 'token': 7200},
# {'sequence': '<s> Le camembert est excellent :)</s>', 'score': 0.10556930303573608, 'token': 2183}, 
# {'sequence': '<s> Le camembert est succulent :)</s>', 'score': 0.03453315049409866, 'token': 26202}, 
# {'sequence': '<s> Le camembert est meilleur :)</s>', 'score': 0.03303130343556404, 'token': 528}, 
# {'sequence': '<s> Le camembert est parfait :)</s>', 'score': 0.030076518654823303, 'token': 1654}]
从CamemBERT输出中提取上下文嵌入特征:
import torch
# Tokenize in sub-words with SentencePiece
tokenized_sentence = tokenizer.tokenize("J'aime le camembert !")
# ['▁J', "'", 'aime', '▁le', '▁ca', 'member', 't', '▁!'] 

# 1-hot encode and add special starting and end tokens 
encoded_sentence = tokenizer.encode(tokenized_sentence)
# [5, 121, 11, 660, 16, 730, 25543, 110, 83, 6] 
# NB: Can be done in one step : tokenize.encode("J'aime le camembert !")

# Feed tokens to Camembert as a torch tensor (batch dim 1)
encoded_sentence = torch.tensor(encoded_sentence).unsqueeze(0)
embeddings, _ = camembert(encoded_sentence)
# embeddings.detach()
# embeddings.size torch.Size([1, 10, 768])
# tensor([[[-0.0254,  0.0235,  0.1027,  ..., -0.1459, -0.0205, -0.0116],
#         [ 0.0606, -0.1811, -0.0418,  ..., -0.1815,  0.0880, -0.0766],
#         [-0.1561, -0.1127,  0.2687,  ..., -0.0648,  0.0249,  0.0446],
#         ...,
从所有CamemBERT层中提取上下文嵌入特征:
from transformers import CamembertConfig
# (Need to reload the model with new config)
config = CamembertConfig.from_pretrained("camembert-base", output_hidden_states=True)
camembert = CamembertModel.from_pretrained("camembert-base", config=config)

embeddings, _, all_layer_embeddings = camembert(encoded_sentence)
#  all_layer_embeddings list of len(all_layer_embeddings) == 13 (input embedding layer + 12 self attention layers)
all_layer_embeddings[5]
# layer 5 contextual embedding : size torch.Size([1, 10, 768])
#tensor([[[-0.0032,  0.0075,  0.0040,  ..., -0.0025, -0.0178, -0.0210],
#         [-0.0996, -0.1474,  0.1057,  ..., -0.0278,  0.1690, -0.2982],
#         [ 0.0557, -0.0588,  0.0547,  ..., -0.0726, -0.0867,  0.0699],
#         ...,