mLUKE
概述
mLUKE模型由Ryokan Ri、Ikuya Yamada和Yoshimasa Tsuruoka在mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models中提出。它是基于XLM-RoBERTa训练的LUKE模型的多语言扩展。
它基于XLM-RoBERTa并添加了实体嵌入,这有助于提高涉及实体推理的各种下游任务的性能,例如命名实体识别、抽取式问答、关系分类、填空式知识补全。
论文的摘要如下:
最近的研究表明,利用维基百科实体的跨语言对齐信息可以有效改进多语言预训练语言模型。然而,现有的方法仅在预训练中利用实体信息,并未在下游任务中明确使用实体。在本研究中,我们探讨了在下游跨语言任务中利用实体表示的有效性。我们训练了一个包含24种语言的实体表示的多语言语言模型,并展示了该模型在各种跨语言迁移任务中始终优于基于单词的预训练模型。我们还分析了该模型,关键见解是将实体表示纳入输入中,使我们能够提取更多与语言无关的特征。我们还使用mLAMA数据集对模型进行了多语言填空提示任务的评估。我们展示了基于实体的提示比仅使用单词表示更有可能引发正确的事实知识。
使用提示
可以直接将mLUKE的权重插入到LUKE模型中,如下所示:
from transformers import LukeModel
model = LukeModel.from_pretrained("studio-ousia/mluke-base")
请注意,mLUKE 有自己的分词器,MLukeTokenizer。您可以按以下方式初始化它:
from transformers import MLukeTokenizer
tokenizer = MLukeTokenizer.from_pretrained("studio-ousia/mluke-base")
由于mLUKE的架构与LUKE相同,可以参考LUKE的文档页面获取所有提示、代码示例和笔记本。
MLukeTokenizer
类 transformers.MLukeTokenizer
< source >( vocab_file entity_vocab_file bos_token = '' eos_token = '' sep_token = '' cls_token = '' unk_token = '
参数
- vocab_file (
str
) — 词汇表文件的路径。 - entity_vocab_file (
str
) — 实体词汇表文件的路径。 - bos_token (
str
, optional, defaults to"<s>"
) — The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.在使用特殊标记构建序列时,这不是用于序列开头的标记。使用的标记是
cls_token
。 - eos_token (
str
, optional, defaults to"</s>"
) — The end of sequence token.在使用特殊标记构建序列时,这不是用于序列结束的标记。 使用的标记是
sep_token
。 - sep_token (
str
, 可选, 默认为""
) — 分隔符标记,用于从多个序列构建序列时,例如用于序列分类的两个序列或用于问答的文本和问题。它也用作使用特殊标记构建的序列的最后一个标记。 - cls_token (
str
, 可选, 默认为"
) — 用于序列分类的分类器标记(对整个序列进行分类,而不是对每个标记进行分类)。当使用特殊标记构建时,它是序列的第一个标记。" - unk_token (
str
, optional, defaults to"
) — 未知标记。不在词汇表中的标记无法转换为ID,而是设置为该标记。" - pad_token (
str
, optional, defaults to"
) — 用于填充的标记,例如在对不同长度的序列进行批处理时使用。" - mask_token (
str
, optional, defaults to"
) — 用于屏蔽值的标记。这是在训练此模型时用于屏蔽语言建模的标记。这是模型将尝试预测的标记。" - 任务 (
str
, 可选) — 您想要准备序列的任务。可以是"entity_classification"
,"entity_pair_classification"
, 或"entity_span_classification"
。如果您指定此参数,实体 序列将根据给定的实体范围自动创建。 - max_entity_length (
int
, optional, 默认为 32) —entity_ids
的最大长度. - max_mention_length (
int
, optional, defaults to 30) — 实体跨度内的最大令牌数。 - entity_token_1 (
str
, 可选, 默认为
) — 用于表示单词标记序列中的实体跨度的特殊标记。此标记仅在task
设置为"entity_classification"
或"entity_pair_classification"
时使用。 - entity_token_2 (
str
, 可选, 默认为
) — 用于表示单词标记序列中的实体跨度的特殊标记。此标记仅在task
设置为"entity_pair_classification"
时使用。 - additional_special_tokens (
List[str]
, optional, defaults to["
) — 分词器使用的额外特殊标记。NOTUSED", "NOTUSED"] - sp_model_kwargs (
dict
, optional) — Will be passed to theSentencePieceProcessor.__init__()
method. The Python wrapper for SentencePiece can be used, among other things, to set:-
enable_sampling
: 启用子词正则化。 -
nbest_size
: 用于unigram的采样参数。对于BPE-Dropout无效。nbest_size = {0,1}
: No sampling is performed.nbest_size > 1
: samples from the nbest_size results.nbest_size < 0
: assuming that nbest_size is infinite and samples from the all hypothesis (lattice) using forward-filtering-and-backward-sampling algorithm.
-
alpha
: 用于单字采样的平滑参数,以及BPE-dropout的合并操作丢弃概率。
-
- sp_model (
SentencePieceProcessor
) — 用于每次转换(字符串、标记和ID)的SentencePiece处理器。
改编自 XLMRobertaTokenizer 和 LukeTokenizer。基于 SentencePiece。
此分词器继承自PreTrainedTokenizer,其中包含了大部分主要方法。用户应参考此超类以获取有关这些方法的更多信息。
__call__
< source >( text: typing.Union[str, typing.List[str]] text_pair: typing.Union[str, typing.List[str], NoneType] = None entity_spans: typing.Union[typing.List[typing.Tuple[int, int]], typing.List[typing.List[typing.Tuple[int, int]]], NoneType] = None entity_spans_pair: typing.Union[typing.List[typing.Tuple[int, int]], typing.List[typing.List[typing.Tuple[int, int]]], NoneType] = None entities: typing.Union[typing.List[str], typing.List[typing.List[str]], NoneType] = None entities_pair: typing.Union[typing.List[str], typing.List[typing.List[str]], NoneType] = None add_special_tokens: bool = True padding: typing.Union[bool, str, transformers.utils.generic.PaddingStrategy] = False truncation: typing.Union[bool, str, transformers.tokenization_utils_base.TruncationStrategy] = None max_length: typing.Optional[int] = None max_entity_length: typing.Optional[int] = None stride: int = 0 is_split_into_words: typing.Optional[bool] = False pad_to_multiple_of: typing.Optional[int] = None padding_side: typing.Optional[bool] = None return_tensors: typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None return_token_type_ids: typing.Optional[bool] = None return_attention_mask: typing.Optional[bool] = None return_overflowing_tokens: bool = False return_special_tokens_mask: bool = False return_offsets_mapping: bool = False return_length: bool = False verbose: bool = True **kwargs ) → BatchEncoding
参数
- text (
str
,List[str]
,List[List[str]]
) — 要编码的序列或序列批次。每个序列必须是一个字符串。请注意,此 分词器不支持基于预分词字符串的分词。 - text_pair (
str
,List[str]
,List[List[str]]
) — 要编码的序列或序列批次。每个序列必须是一个字符串。请注意,此 分词器不支持基于预分词字符串的分词。 - entity_spans (
List[Tuple[int, int]]
,List[List[Tuple[int, int]]]
, 可选) — 要编码的实体跨度的序列或序列批次。每个序列由元组组成,每个元组包含两个整数,表示基于字符的实体起始和结束位置。如果在构造函数中指定"entity_classification"
或"entity_pair_classification"
作为task
参数,则每个序列的长度必须分别为1或2。如果指定了entities
,则每个序列的长度必须等于entities
的每个序列的长度。 - entity_spans_pair (
List[Tuple[int, int]]
,List[List[Tuple[int, int]]]
, optional) — 要编码的实体跨度的序列或序列批次。每个序列由元组组成,每个元组包含两个整数,表示基于字符的实体起始和结束位置。如果在构造函数中指定了task
参数,则忽略此参数。如果指定了entities_pair
,则每个序列的长度必须等于entities_pair
的每个序列的长度。 - entities (
List[str]
,List[List[str]]
, optional) — 要编码的实体序列或实体序列批次。每个序列由表示实体的字符串组成,即特殊实体(例如,[MASK])或维基百科的实体标题(例如,洛杉矶)。如果在构造函数中指定了task
参数,则忽略此参数。每个序列的长度必须等于entity_spans
中每个序列的长度。如果指定了entity_spans
但未指定此参数,则实体序列或实体序列批次将自动通过填充[MASK]实体来构建。 - entities_pair (
List[str]
,List[List[str]]
, optional) — 要编码的实体序列或实体序列批次。每个序列由表示实体的字符串组成,即特殊实体(例如,[MASK])或维基百科的实体标题(例如,洛杉矶)。如果在构造函数中指定了task
参数,则忽略此参数。每个序列的长度必须等于entity_spans_pair
中每个序列的长度。如果指定了entity_spans_pair
但未指定此参数,则通过填充[MASK]实体自动构建实体序列或实体序列批次。 - max_entity_length (
int
, optional) —entity_ids
的最大长度. - add_special_tokens (
bool
, 可选, 默认为True
) — 是否在编码序列时添加特殊标记。这将使用底层的PretrainedTokenizerBase.build_inputs_with_special_tokens
函数,该函数定义了哪些标记会自动添加到输入ID中。如果您想自动添加bos
或eos
标记,这将非常有用。 - padding (
bool
,str
or PaddingStrategy, optional, defaults toFalse
) — 激活并控制填充。接受以下值:True
或'longest'
: 填充到批次中最长的序列(如果只提供一个序列,则不进行填充)。'max_length'
: 填充到由参数max_length
指定的最大长度,或者如果未提供该参数,则填充到模型可接受的最大输入长度。False
或'do_not_pad'
(默认): 不进行填充(即,可以输出具有不同长度序列的批次)。
- truncation (
bool
,str
or TruncationStrategy, optional, defaults toFalse
) — Activates and controls truncation. Accepts the following values:True
or'longest_first'
: Truncate to a maximum length specified with the argumentmax_length
or to the maximum acceptable input length for the model if that argument is not provided. This will truncate token by token, removing a token from the longest sequence in the pair if a pair of sequences (or a batch of pairs) is provided.'only_first'
: Truncate to a maximum length specified with the argumentmax_length
or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the first sequence of a pair if a pair of sequences (or a batch of pairs) is provided.'only_second'
: Truncate to a maximum length specified with the argumentmax_length
or to the maximum acceptable input length for the model if that argument is not provided. This will only truncate the second sequence of a pair if a pair of sequences (or a batch of pairs) is provided.False
or'do_not_truncate'
(default): No truncation (i.e., can output batch with sequence lengths greater than the model maximum admissible input size).
- max_length (
int
, optional) — Controls the maximum length to use by one of the truncation/padding parameters.如果未设置或设置为
None
,则在需要截断/填充参数时,将使用预定义的模型最大长度。如果模型没有特定的最大输入长度(如XLNet),则截断/填充到最大长度的功能将被停用。 - stride (
int
, 可选, 默认为 0) — 如果与max_length
一起设置为一个数字,当return_overflowing_tokens=True
时返回的溢出标记将包含来自截断序列末尾的一些标记,以提供截断序列和溢出序列之间的一些重叠。此参数的值定义了重叠标记的数量。 - is_split_into_words (
bool
, 可选, 默认为False
) — 输入是否已经预分词(例如,分割成单词)。如果设置为True
,分词器会假设输入已经分割成单词(例如,通过空格分割),然后进行分词。这对于NER或分词分类非常有用。 - pad_to_multiple_of (
int
, 可选) — 如果设置,将序列填充到提供的值的倍数。需要激活padding
。 这对于在计算能力>= 7.5
(Volta)的NVIDIA硬件上启用Tensor Cores特别有用。 - padding_side (
str
, 可选) — 模型应在哪一侧应用填充。应在['right', 'left']之间选择。 默认值从同名的类属性中选取。 - return_tensors (
str
或 TensorType, 可选) — 如果设置,将返回张量而不是Python整数列表。可接受的值有:'tf'
: 返回 TensorFlowtf.constant
对象。'pt'
: 返回 PyTorchtorch.Tensor
对象。'np'
: 返回 Numpynp.ndarray
对象。
- return_token_type_ids (
bool
, optional) — Whether to return token type IDs. If left to the default, will return the token type IDs according to the specific tokenizer’s default, defined by thereturn_outputs
attribute. - return_attention_mask (
bool
, optional) — Whether to return the attention mask. If left to the default, will return the attention mask according to the specific tokenizer’s default, defined by thereturn_outputs
attribute. - return_overflowing_tokens (
bool
, optional, defaults toFalse
) — 是否返回溢出的token序列。如果提供了一对输入id序列(或一批对)并且使用了truncation_strategy = longest_first
或True
,则会引发错误而不是返回溢出的token。 - return_special_tokens_mask (
bool
, optional, defaults toFalse
) — 是否返回特殊令牌掩码信息。 - return_offsets_mapping (
bool
, optional, defaults toFalse
) — Whether or not to return(char_start, char_end)
for each token.这仅在继承自PreTrainedTokenizerFast的快速分词器上可用,如果使用Python的分词器,此方法将引发
NotImplementedError
。 - return_length (
bool
, optional, defaults toFalse
) — 是否返回编码输入的长度。 - verbose (
bool
, optional, defaults toTrue
) — 是否打印更多信息和警告。 - **kwargs — 传递给
self.tokenize()
方法
一个 BatchEncoding 包含以下字段:
-
input_ids — 要输入模型的标记ID列表。
-
token_type_ids — 要输入模型的标记类型ID列表(当
return_token_type_ids=True
或 如果 “token_type_ids” 在self.model_input_names
中)。 -
attention_mask — 指定模型应关注哪些标记的索引列表(当
return_attention_mask=True
或如果 “attention_mask” 在self.model_input_names
中)。 -
entity_ids — 要输入模型的实体ID列表。
-
entity_position_ids — 要输入模型的输入序列中的实体位置列表。
-
entity_token_type_ids — 要输入模型的实体标记类型ID列表(当
return_token_type_ids=True
或如果 “entity_token_type_ids” 在self.model_input_names
中)。 -
entity_attention_mask — 指定模型应关注哪些实体的索引列表 (当
return_attention_mask=True
或如果 “entity_attention_mask” 在self.model_input_names
中)。 -
entity_start_positions — 单词标记序列中实体的起始位置列表(当
task="entity_span_classification"
)。 -
entity_end_positions — 单词标记序列中实体的结束位置列表(当
task="entity_span_classification"
)。 -
overflowing_tokens — 溢出标记序列列表(当指定了
max_length
并且return_overflowing_tokens=True
)。 -
num_truncated_tokens — 被截断的标记数量(当指定了
max_length
并且return_overflowing_tokens=True
)。 -
special_tokens_mask — 0和1的列表,1表示添加的特殊标记,0表示 常规序列标记(当
add_special_tokens=True
并且return_special_tokens_mask=True
)。 -
length — 输入的长度(当
return_length=True
)
主要方法,用于根据您想要准备的任务,对一个或多个序列或一个或多个序列对进行标记化并准备模型。