Transformers 文档

mLUKE

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

该模型于 2021-10-15 发布在 HF papers 上,并于 2021-12-07 贡献给 Hugging Face Transformers。

mLUKE

概述

mLUKE 模型在 mLUKE: The Power of Entity Representations in Multilingual Pretrained Language Models 一文中由 Ryokan Ri, Ikuya Yamada 和 Yoshimasa Tsuruoka 提出。它是 LUKE 模型 的多语言扩展,基于 XLM-RoBERTa 进行训练。

它基于 XLM-RoBERTa 并增加了实体嵌入(entity embeddings),这有助于提高在各种涉及实体推理的下游任务中的表现,例如命名实体识别、抽取式问答、关系分类和填空式知识补全。

论文摘要如下:

最近的研究表明,通过维基百科实体中的跨语言对齐信息,可以有效改进多语言预训练语言模型。然而,现有方法仅在预训练中利用实体信息,并未在下游任务中显式使用实体。在本研究中,我们探索了利用实体表示进行下游跨语言任务的有效性。我们使用包含实体表示的多语言语言模型在 24 种语言上进行训练,并证明该模型在各种跨语言迁移任务中始终优于基于词的预训练模型。我们还分析了该模型,核心洞察在于将实体表示纳入输入使我们能够提取更多与语言无关的特征。我们还使用 mLAMA 数据集通过多语言填空提示任务评估了该模型。我们表明,基于实体的提示比仅使用词表示更容易引出正确的客观事实知识。

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

使用技巧

你可以直接将 mLUKE 的权重接入到 LUKE 模型中,如下所示

from transformers import LukeModel


model = LukeModel.from_pretrained("studio-ousia/mluke-base", device_map="auto")

请注意,mLUKE 有其自带的 tokenizer,即 MLukeTokenizer。你可以按如下方式初始化它

from transformers import MLukeTokenizer


tokenizer = MLukeTokenizer.from_pretrained("studio-ousia/mluke-base")

由于 mLUKE 的架构与 LUKE 等效,因此可以参考 LUKE 的文档页面 以获取所有提示、代码示例和笔记本。

MLukeTokenizer

class transformers.MLukeTokenizer

< >

( bos_token = '<s>' eos_token = '</s>' sep_token = '</s>' cls_token = '<s>' unk_token = '<unk>' pad_token = '<pad>' mask_token = '<mask>' task = None max_entity_length = 32 max_mention_length = 30 entity_token_1 = '<ent>' entity_token_2 = '<ent2>' entity_unk_token = '[UNK]' entity_pad_token = '[PAD]' entity_mask_token = '[MASK]' entity_mask2_token = '[MASK2]' vocab: str | dict | list | None = None entity_vocab: str | dict | list | None = None **kwargs )

参数

  • vocab_file (str) — 词汇表文件路径。
  • entity_vocab_file (str) — 实体词汇表文件路径。
  • bos_token (str, 可选, 默认为 "<s>") — 预训练期间使用的序列开始标记。可用作序列分类标记。

    在构建使用特殊标记的序列时,这不是用于序列开始的标记。实际使用的标记是 cls_token

  • eos_token (str, 可选, 默认为 "</s>") — 序列结束标记。

    在构建使用特殊标记的序列时,这不是用于序列结束的标记。实际使用的标记是 sep_token

  • sep_token (str, 可选, 默认为 "</s>") — 分隔符标记,在从多个序列构建序列时使用,例如用于序列分类的两个序列,或用于问答的文本和问题。它也用作使用特殊标记构建的序列的最后一个标记。
  • cls_token (str, 可选, 默认为 "<s>") — 在进行序列分类(整个序列的分类,而不是每个标记的分类)时使用的分类器标记。它是使用特殊标记构建序列时的第一个标记。
  • unk_token (str, 可选, 默认为 "<unk>") — 未知标记。词汇表中不存在的标记无法转换为 ID,而是设置为此标记。
  • pad_token (str, 可选, 默认为 "<pad>") — 用于填充的标记,例如在对不同长度的序列进行批处理时。
  • mask_token (str, 可选, 默认为 "<mask>") — 用于遮蔽值的标记。这是在用遮蔽语言模型(masked language modeling)训练此模型时使用的标记。这是模型将尝试预测的标记。
  • task (str, 可选) — 你想要为其准备序列的任务。取值为 "entity_classification", "entity_pair_classification""entity_span_classification" 之一。如果你指定了此参数,实体序列将根据给定的实体跨度(entity span)自动创建。
  • max_entity_length (int, 可选, 默认为 32) — entity_ids 的最大长度。
  • max_mention_length (int, 可选, 默认为 30) — 实体跨度内的最大标记数量。
  • entity_token_1 (str, 可选, 默认为 <ent>) — 用于在词标记序列中表示实体跨度的特殊标记。此标记仅在 task 设置为 "entity_classification""entity_pair_classification" 时使用。
  • entity_token_2 (str, 可选, 默认为 <ent2>) — 用于在词标记序列中表示实体跨度的特殊标记。此标记仅在 task 设置为 "entity_pair_classification" 时使用。
  • additional_special_tokens (list[str], 可选, 默认为 ["<s>NOTUSED", "</s>NOTUSED"]) — 分词器使用的其他特殊标记。
  • sp_model_kwargs (dict, 可选) — 将传递给 SentencePieceProcessor.__init__() 方法。可以利用 SentencePiece 的 Python 封装 来设置以下内容:

    • enable_sampling: 启用子词正则化(subword regularization)。

    • nbest_size: Unigram 的采样参数。对 BPE-Dropout 无效。

      • nbest_size = {0,1}: 不执行采样。
      • nbest_size > 1: 从 nbest_size 结果中采样。
      • nbest_size < 0: 假设 nbest_size 无限大,并使用前向过滤和后向采样算法从所有假设(格)中采样。
    • alpha: Unigram 采样的平滑参数,以及 BPE-dropout 的合并操作丢失概率。

  • sp_model (SentencePieceProcessor) — 用于每次转换(字符串、标记和 ID)的 SentencePiece 处理器。

改编自 XLMRobertaTokenizerLukeTokenizer。基于 SentencePiece

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

__call__

< >

( text: str | list[str] text_pair: str | list[str] | None = None entity_spans: list[tuple[int, int]] | list[list[tuple[int, int]]] | None = None entity_spans_pair: list[tuple[int, int]] | list[list[tuple[int, int]]] | None = None entities: list[str] | list[list[str]] | None = None entities_pair: list[str] | list[list[str]] | None = None add_special_tokens: bool = True padding: bool | str | transformers.utils.generic.PaddingStrategy = False truncation: bool | str | transformers.tokenization_utils_base.TruncationStrategy = None max_length: int | None = None max_entity_length: int | None = None stride: int = 0 is_split_into_words: bool | None = False pad_to_multiple_of: int | None = None padding_side: str | None = None return_tensors: str | transformers.utils.generic.TensorType | None = None return_token_type_ids: bool | None = None return_attention_mask: bool | None = 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

返回

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_lengthreturn_overflowing_tokens=True 时)。

  • num_truncated_tokens — 截断标记的数量(当指定 max_lengthreturn_overflowing_tokens=True 时)。

  • special_tokens_mask — 0 和 1 的列表,其中 1 表示添加的特殊标记,0 表示常规序列标记(当 add_special_tokens=Truereturn_special_tokens_mask=True 时)。

  • length — 输入的长度(当 return_length=True 时)

add_special_tokens (bool, 可选, 默认为 True): 在编码序列时是否添加特殊标记。这将使用底层的 PretrainedTokenizerBase.build_inputs_with_special_tokens 函数,该函数定义了哪些标记会被自动添加到 input id 中。如果你想自动添加 boseos 标记,这非常有用。padding (bool, strPaddingStrategy, 可选, 默认为 False): 启用并控制填充。接受以下值

  • True or 'longest': Pad to the longest sequence in the batch (or no padding if only a single sequence is provided).

  • 'max_length': Pad 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.

  • False'do_not_pad' (默认): 不填充(即可以输出包含不同长度序列的批次)。truncation (bool, strTruncationStrategy, 可选, 默认为 False): 启用并控制截断。接受以下值

  • 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 整数列表。可接受的值为

  • 'pt': Return PyTorch torch.Tensor objects.
  • 'np': Return Numpy np.ndarray objects.

return_token_type_ids (bool, 可选): 是否返回标记类型 ID。如果保留默认值,将根据特定分词器的默认设置返回标记类型 ID,该默认设置由 return_outputs 属性定义。

什么是标记类型 ID? return_attention_mask (bool, 可选): 是否返回注意力掩码。如果保留默认值,将根据特定分词器的默认设置返回注意力掩码,该默认设置由 return_outputs 属性定义。

什么是注意力掩码? return_overflowing_tokens (bool, 可选, 默认为 False): 是否返回溢出的标记序列。如果使用 truncation_strategy = longest_firstTrue 提供了输入 ID 对(或对的批次),则会引发错误,而不是返回溢出的标记。 return_special_tokens_mask (bool, 可选, 默认为 False): 是否返回特殊标记掩码信息。 return_offsets_mapping (bool, 可选, 默认为 False): 是否为每个标记返回 (char_start, char_end)

这仅适用于继承自 PreTrainedTokenizerFast 的快速分词器,如果使用 Python 的分词器,此方法将引发 NotImplementedError。 return_length (bool, 可选, 默认为 False): 是否返回编码输入的长度。 verbose (bool, 可选, 默认为 True): 是否打印更多信息和警告。 **kwargs: 传递给 self.tokenize() 方法

save_vocabulary

< >

( save_directory: str filename_prefix: str | None = None )

仅保存实体词汇表文件。tokenizer.json 由父级 TokenizersBackend 保存。

在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.