Transformers 文档

mLUKE

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数据集对模型进行了多语言填空提示任务的评估。我们展示了基于实体的提示比仅使用单词表示更有可能引发正确的事实知识。

该模型由ryo0634贡献。原始代码可以在这里找到。

使用提示

可以直接将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

< >

( vocab_file entity_vocab_file bos_token = '' eos_token = '' sep_token = '' cls_token = '' unk_token = '' pad_token = '' mask_token = '' task = None max_entity_length = 32 max_mention_length = 30 entity_token_1 = '' entity_token_2 = '' entity_unk_token = '[UNK]' entity_pad_token = '[PAD]' entity_mask_token = '[MASK]' entity_mask2_token = '[MASK2]' sp_model_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None **kwargs )

参数

  • 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 the SentencePieceProcessor.__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处理器。

改编自 XLMRobertaTokenizerLukeTokenizer。基于 SentencePiece

此分词器继承自PreTrainedTokenizer,其中包含了大部分主要方法。用户应参考此超类以获取有关这些方法的更多信息。

__call__

< >

( 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中。如果您想自动添加 boseos 标记,这将非常有用。
  • padding (bool, str or PaddingStrategy, optional, defaults to False) — 激活并控制填充。接受以下值:
    • True'longest': 填充到批次中最长的序列(如果只提供一个序列,则不进行填充)。
    • 'max_length': 填充到由参数 max_length 指定的最大长度,或者如果未提供该参数,则填充到模型可接受的最大输入长度。
    • False'do_not_pad' (默认): 不进行填充(即,可以输出具有不同长度序列的批次)。
  • truncation (bool, str or TruncationStrategy, optional, defaults to False) — Activates and controls truncation. Accepts the following values:
    • True or 'longest_first': Truncate to a maximum length specified with the argument max_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 argument max_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 argument max_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 (strTensorType, 可选) — 如果设置,将返回张量而不是Python整数列表。可接受的值有:
    • 'tf': 返回 TensorFlow tf.constant 对象。
    • 'pt': 返回 PyTorch torch.Tensor 对象。
    • 'np': 返回 Numpy np.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 the return_outputs attribute.

    什么是token type IDs?

  • 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 the return_outputs attribute.

    什么是注意力掩码?

  • return_overflowing_tokens (bool, optional, defaults to False) — 是否返回溢出的token序列。如果提供了一对输入id序列(或一批对)并且使用了truncation_strategy = longest_firstTrue,则会引发错误而不是返回溢出的token。
  • return_special_tokens_mask (bool, optional, defaults to False) — 是否返回特殊令牌掩码信息。
  • return_offsets_mapping (bool, optional, defaults to False) — Whether or not to return (char_start, char_end) for each token.

    这仅在继承自PreTrainedTokenizerFast的快速分词器上可用,如果使用Python的分词器,此方法将引发NotImplementedError

  • return_length (bool, optional, defaults to False) — 是否返回编码输入的长度。
  • verbose (bool, optional, defaults to True) — 是否打印更多信息和警告。
  • **kwargs — 传递给 self.tokenize() 方法

返回

BatchEncoding

一个 BatchEncoding 包含以下字段:

  • input_ids — 要输入模型的标记ID列表。

    什么是输入ID?

  • token_type_ids — 要输入模型的标记类型ID列表(当 return_token_type_ids=True 或 如果 “token_type_ids”self.model_input_names 中)。

    什么是标记类型ID?

  • attention_mask — 指定模型应关注哪些标记的索引列表(当 return_attention_mask=True 或如果 “attention_mask”self.model_input_names 中)。

    什么是注意力掩码?

  • entity_ids — 要输入模型的实体ID列表。

    什么是输入ID?

  • entity_position_ids — 要输入模型的输入序列中的实体位置列表。

  • entity_token_type_ids — 要输入模型的实体标记类型ID列表(当 return_token_type_ids=True 或如果 “entity_token_type_ids”self.model_input_names 中)。

    什么是标记类型ID?

  • 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

主要方法,用于根据您想要准备的任务,对一个或多个序列或一个或多个序列对进行标记化并准备模型。

保存词汇表

< >

( 保存目录: str 文件名前缀: typing.Optional[str] = None )

< > Update on GitHub