Transformers 文档

LUKE

LUKE

概述

LUKE模型由Ikuya Yamada、Akari Asai、Hiroyuki Shindo、Hideaki Takeda和Yuji Matsumoto在LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention中提出。它基于RoBERTa,并添加了实体嵌入以及实体感知的自注意力机制,这有助于提高涉及实体推理的各种下游任务的性能,例如命名实体识别、抽取式和填空式问答、实体类型识别和关系分类。

论文的摘要如下:

实体表示在涉及实体的自然语言任务中非常有用。在本文中,我们提出了基于双向变压器的新预训练上下文词和实体表示。所提出的模型将给定文本中的词和实体视为独立的标记,并输出它们的上下文表示。我们的模型使用基于BERT的掩码语言模型的新预训练任务进行训练。该任务涉及预测从维基百科检索的大型实体注释语料库中随机掩码的词和实体。我们还提出了一种实体感知的自注意力机制,该机制是变压器自注意力机制的扩展,并在计算注意力分数时考虑标记的类型(词或实体)。所提出的模型在广泛的实体相关任务中取得了令人印象深刻的实证表现。特别是,它在五个知名数据集上获得了最先进的结果:Open Entity(实体类型识别)、TACRED(关系分类)、CoNLL-2003(命名实体识别)、ReCoRD(填空式问答)和SQuAD 1.1(抽取式问答)。

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

使用提示

  • 此实现与RobertaModel相同,增加了实体嵌入以及实体感知的自注意力机制,这提高了涉及实体推理任务的性能。

  • LUKE将实体视为输入标记;因此,它需要entity_idsentity_attention_maskentity_token_type_idsentity_position_ids作为额外的输入。你可以使用LukeTokenizer来获取这些。

  • LukeTokenizer 接受 entitiesentity_spans(输入文本中基于字符的实体起始和结束位置)作为额外输入。entities 通常由 [MASK] 实体或维基百科实体组成。输入这些实体时的简要描述如下:

    • Inputting [MASK] entities to compute entity representations: The [MASK] entity is used to mask entities to be predicted during pretraining. When LUKE receives the [MASK] entity, it tries to predict the original entity by gathering the information about the entity from the input text. Therefore, the [MASK] entity can be used to address downstream tasks requiring the information of entities in text such as entity typing, relation classification, and named entity recognition.
    • Inputting Wikipedia entities to compute knowledge-enhanced token representations: LUKE learns rich information (or knowledge) about Wikipedia entities during pretraining and stores the information in its entity embedding. By using Wikipedia entities as input tokens, LUKE outputs token representations enriched by the information stored in the embeddings of these entities. This is particularly effective for tasks requiring real-world knowledge, such as question answering.
  • 前一个用例有三种头部模型:

    • LukeForEntityClassification, for tasks to classify a single entity in an input text such as entity typing, e.g. the Open Entity dataset. This model places a linear head on top of the output entity representation.
    • LukeForEntityPairClassification, for tasks to classify the relationship between two entities such as relation classification, e.g. the TACRED dataset. This model places a linear head on top of the concatenated output representation of the pair of given entities.
    • LukeForEntitySpanClassification, for tasks to classify the sequence of entity spans, such as named entity recognition (NER). This model places a linear head on top of the output entity representations. You can address NER using this model by inputting all possible entity spans in the text to the model.

    LukeTokenizer 有一个 task 参数,它允许你通过指定 task="entity_classification"task="entity_pair_classification"task="entity_span_classification" 来轻松创建这些头部模型的输入。请参考每个头部模型的示例代码。

使用示例:

>>> from transformers import LukeTokenizer, LukeModel, LukeForEntityPairClassification

>>> model = LukeModel.from_pretrained("studio-ousia/luke-base")
>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base")
# Example 1: Computing the contextualized entity representation corresponding to the entity mention "Beyoncé"

>>> text = "Beyoncé lives in Los Angeles."
>>> entity_spans = [(0, 7)]  # character-based entity span corresponding to "Beyoncé"
>>> inputs = tokenizer(text, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
>>> outputs = model(**inputs)
>>> word_last_hidden_state = outputs.last_hidden_state
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
# Example 2: Inputting Wikipedia entities to obtain enriched contextualized representations

>>> entities = [
...     "Beyoncé",
...     "Los Angeles",
... ]  # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
>>> entity_spans = [(0, 7), (17, 28)]  # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
>>> inputs = tokenizer(text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
>>> outputs = model(**inputs)
>>> word_last_hidden_state = outputs.last_hidden_state
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
# Example 3: Classifying the relationship between two entities using LukeForEntityPairClassification head model

>>> model = LukeForEntityPairClassification.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
>>> entity_spans = [(0, 7), (17, 28)]  # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> predicted_class_idx = int(logits[0].argmax())
>>> print("Predicted class:", model.config.id2label[predicted_class_idx])

资源

LukeConfig

transformers.LukeConfig

< >

( 词汇表大小 = 50267 实体词汇表大小 = 500000 隐藏层大小 = 768 实体嵌入大小 = 256 隐藏层数量 = 12 注意力头数量 = 12 中间层大小 = 3072 隐藏层激活函数 = 'gelu' 隐藏层丢弃概率 = 0.1 注意力概率丢弃概率 = 0.1 最大位置嵌入 = 512 类型词汇表大小 = 2 初始化范围 = 0.02 层归一化epsilon = 1e-12 使用实体感知注意力 = True 分类器丢弃 = None 填充标记ID = 1 开始标记ID = 0 结束标记ID = 2 **kwargs )

参数

  • vocab_size (int, 可选, 默认为 50267) — LUKE 模型的词汇表大小。定义了可以通过调用 LukeModel 时传递的 inputs_ids 表示的不同标记的数量。
  • entity_vocab_size (int, 可选, 默认为 500000) — LUKE 模型的实体词汇表大小。定义了可以通过调用 LukeModel 时传递的 entity_ids 表示的不同实体的数量。
  • hidden_size (int, optional, 默认为 768) — 编码器层和池化层的维度。
  • entity_emb_size (int, optional, defaults to 256) — 实体嵌入的维度数。
  • num_hidden_layers (int, 可选, 默认为 12) — Transformer 编码器中的隐藏层数量。
  • num_attention_heads (int, optional, defaults to 12) — Transformer编码器中每个注意力层的注意力头数量。
  • intermediate_size (int, 可选, 默认为 3072) — Transformer编码器中“中间”(通常称为前馈)层的维度。
  • hidden_act (strCallable, 可选, 默认为 "gelu") — 编码器和池化器中的非线性激活函数(函数或字符串)。如果是字符串,支持 "gelu""relu""silu""gelu_new"
  • hidden_dropout_prob (float, optional, 默认为 0.1) — 嵌入层、编码器和池化器中所有全连接层的 dropout 概率。
  • attention_probs_dropout_prob (float, optional, defaults to 0.1) — 注意力概率的丢弃比例。
  • max_position_embeddings (int, optional, 默认为 512) — 此模型可能使用的最大序列长度。通常将其设置为较大的值以防万一(例如,512 或 1024 或 2048)。
  • type_vocab_size (int, 可选, 默认为 2) — 调用 LukeModel 时传递的 token_type_ids 的词汇大小.
  • initializer_range (float, 可选, 默认为 0.02) — 用于初始化所有权重矩阵的截断正态初始化器的标准差。
  • layer_norm_eps (float, optional, defaults to 1e-12) — 层归一化层使用的epsilon值。
  • use_entity_aware_attention (bool, 可选, 默认为 True) — 模型是否应使用在LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention (Yamada et al.)中提出的实体感知自注意力机制。
  • classifier_dropout (float, optional) — 分类头的丢弃比率。
  • pad_token_id (int, 可选, 默认为 1) — 填充标记的ID.
  • bos_token_id (int, optional, 默认为 0) — 流的开始标记 id.
  • eos_token_id (int, optional, defaults to 2) — 流结束标记的ID。

这是用于存储LukeModel配置的配置类。它用于根据指定的参数实例化一个LUKE模型,定义模型架构。使用默认值实例化配置将产生类似于LUKE studio-ousia/luke-base架构的配置。

配置对象继承自PretrainedConfig,可用于控制模型输出。阅读PretrainedConfig的文档以获取更多信息。

示例:

>>> from transformers import LukeConfig, LukeModel

>>> # Initializing a LUKE configuration
>>> configuration = LukeConfig()

>>> # Initializing a model from the configuration
>>> model = LukeModel(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config

LukeTokenizer

transformers.LukeTokenizer

< >

( vocab_file merges_file entity_vocab_file 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]' errors = 'replace' bos_token = '' eos_token = '' sep_token = '' cls_token = '' unk_token = '' pad_token = '' mask_token = '' add_prefix_space = False **kwargs )

参数

  • vocab_file (str) — 词汇表文件的路径。
  • merges_file (str) — 合并文件的路径。
  • entity_vocab_file (str) — 实体词汇表文件的路径。
  • 任务 (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" 时使用。
  • errors (str, 可选, 默认为 "replace") — 解码字节为UTF-8时遵循的范式。更多信息请参见 bytes.decode.
  • 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, 可选, 默认为 "") — 用于屏蔽值的标记。这是在训练此模型时用于屏蔽语言建模的标记。这是模型将尝试预测的标记。
  • add_prefix_space (bool, 可选, 默认为 False) — 是否在输入前添加一个初始空格。这允许将前导词视为任何其他词。(LUKE 分词器通过前面的空格检测单词的开头)。

构建一个LUKE分词器,基于GPT-2分词器,使用字节级字节对编码。

这个分词器已经被训练成将空格视为标记的一部分(有点像sentencepiece),因此一个单词将会

无论它是否在句子的开头(没有空格),编码方式都会有所不同:

>>> from transformers import LukeTokenizer

>>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-base")
>>> tokenizer("Hello world")["input_ids"]
[0, 31414, 232, 2]

>>> tokenizer(" Hello world")["input_ids"]
[0, 20920, 232, 2]

你可以通过在实例化此分词器或在某些文本上调用它时传递add_prefix_space=True来绕过这种行为,但由于模型不是以这种方式预训练的,这可能会导致性能下降。

当与is_split_into_words=True一起使用时,此分词器将在每个单词(即使是第一个单词)前添加一个空格。

这个分词器继承自PreTrainedTokenizer,其中包含了大部分主要方法。用户应参考这个超类以获取有关这些方法的更多信息。它还创建了实体序列,即entity_idsentity_attention_maskentity_token_type_idsentity_position_ids,供LUKE模型使用。

__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]]], optional) — 要编码的实体跨度的序列或序列批次。每个序列由元组组成,每个元组包含两个整数,表示基于字符的实体起始和结束位置。如果在构造函数中指定"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, optional, 默认为 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, optional) — 模型应应用填充的一侧。应在['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, 可选, 默认为 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 )

LukeModel

transformers.LukeModel

< >

( config: LukeConfig add_pooling_layer: bool = True )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,仅加载配置。查看 from_pretrained() 方法以加载模型权重。

裸的LUKE模型转换器,输出词标记和实体的原始隐藏状态,没有任何特定的头部。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.BaseLukeModelOutputWithPoolingtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor 形状为 (batch_size, entity_length), 可选) — 用于避免对填充实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 实体令牌,
    • 1 对应于 部分 B 实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor 形状为 (batch_size, sequence_length, hidden_size), 可选) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。如果您希望对如何将 input_ids 索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, optional) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个 ModelOutput 而不是一个普通的元组。

返回

transformers.models.luke.modeling_luke.BaseLukeModelOutputWithPoolingtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.BaseLukeModelOutputWithPooling 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • last_hidden_state (torch.FloatTensor 形状为 (batch_size, sequence_length, hidden_size)) — 模型最后一层输出的隐藏状态序列。
  • entity_last_hidden_state (torch.FloatTensor 形状为 (batch_size, entity_length, hidden_size)) — 模型最后一层输出的实体隐藏状态序列。
  • pooler_output (torch.FloatTensor 形状为 (batch_size, hidden_size)) — 序列的第一个标记(分类标记)的最后一层隐藏状态,经过线性层和Tanh激活函数进一步处理。
  • hidden_states (tuple(torch.FloatTensor), 可选, 当传递了 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,一个用于每一层的输出),形状为 (batch_size, sequence_length, hidden_size)。模型在每一层输出的隐藏状态加上初始嵌入输出。
  • entity_hidden_states (tuple(torch.FloatTensor), 可选, 当传递了 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,一个用于每一层的输出),形状为 (batch_size, entity_length, hidden_size)。模型在每一层输出的实体隐藏状态加上初始实体嵌入输出。
  • attentions (tuple(torch.FloatTensor), 可选, 当传递了 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每一层一个),形状为 (batch_size, num_heads, sequence_length + entity_length, sequence_length + entity_length)。注意力权重在注意力softmax之后,用于计算自注意力头中的加权平均值。

LukeModel 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeModel

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-base")
>>> model = LukeModel.from_pretrained("studio-ousia/luke-base")
# Compute the contextualized entity representation corresponding to the entity mention "Beyoncé"

>>> text = "Beyoncé lives in Los Angeles."
>>> entity_spans = [(0, 7)]  # character-based entity span corresponding to "Beyoncé"

>>> encoding = tokenizer(text, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt")
>>> outputs = model(**encoding)
>>> word_last_hidden_state = outputs.last_hidden_state
>>> entity_last_hidden_state = outputs.entity_last_hidden_state
# Input Wikipedia entities to obtain enriched contextualized representations of word tokens

>>> text = "Beyoncé lives in Los Angeles."
>>> entities = [
...     "Beyoncé",
...     "Los Angeles",
... ]  # Wikipedia entity titles corresponding to the entity mentions "Beyoncé" and "Los Angeles"
>>> entity_spans = [
...     (0, 7),
...     (17, 28),
... ]  # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"

>>> encoding = tokenizer(
...     text, entities=entities, entity_spans=entity_spans, add_prefix_space=True, return_tensors="pt"
... )
>>> outputs = model(**encoding)
>>> word_last_hidden_state = outputs.last_hidden_state
>>> entity_last_hidden_state = outputs.entity_last_hidden_state

LukeForMaskedLM

transformers.LukeForMaskedLM

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型在顶部具有语言建模头和实体预测头,用于掩码语言建模和掩码实体预测。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.LongTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None labels: typing.Optional[torch.LongTensor] = None entity_labels: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.LukeMaskedLMOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充的实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示未掩码的实体标记,
    • 0 表示掩码的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 的实体令牌,
    • 1 对应于 部分 B 的实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids。如果您希望对如何将input_ids索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个 ModelOutput 而不是一个普通的元组。
  • labels (torch.LongTensor of shape (batch_size, sequence_length), optional) — 用于计算掩码语言建模损失的标签。索引应在 [-100, 0, ..., config.vocab_size] 范围内(参见 input_ids 文档字符串)。索引设置为 -100 的标记将被忽略(掩码), 损失仅针对标签在 [0, ..., config.vocab_size] 范围内的标记进行计算
  • entity_labels (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于计算掩码语言建模损失的标签。索引应在 [-100, 0, ..., config.vocab_size] 范围内(参见 input_ids 文档字符串)。索引设置为 -100 的标记将被忽略(掩码), 损失仅针对标签在 [0, ..., config.vocab_size] 范围内的标记进行计算

返回

transformers.models.luke.modeling_luke.LukeMaskedLMOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.LukeMaskedLMOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含根据配置(LukeConfig)和输入而定的各种元素。

  • loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 掩码语言建模(MLM)损失和实体预测损失的总和。

  • mlm_loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 掩码语言建模(MLM)损失。

  • mep_loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 掩码实体预测(MEP)损失。

  • logits (torch.FloatTensor 形状为 (batch_size, sequence_length, config.vocab_size)) — 语言建模头的预测分数(SoftMax 之前每个词汇标记的分数)。

  • entity_logits (torch.FloatTensor 形状为 (batch_size, sequence_length, config.vocab_size)) — 实体预测头的预测分数(SoftMax 之前每个实体词汇标记的分数)。

  • hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每层的输出),形状为 (batch_size, sequence_length, hidden_size)

    模型在每层输出处的隐藏状态加上初始嵌入输出。

  • entity_hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每层的输出),形状为 (batch_size, entity_length, hidden_size)。模型在每层输出处的实体隐藏状态加上初始实体嵌入输出。

  • attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

LukeForMaskedLM 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

LukeForEntityClassification

transformers.LukeForEntityClassification

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,仅加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型在顶部带有分类头(在第一个实体标记的隐藏状态之上的线性层),用于实体分类任务,例如Open Entity。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.EntityClassificationOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充的实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示未掩码的实体标记,
    • 0 表示掩码的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 的实体令牌,
    • 1 对应于 部分 B 的实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids。如果您希望对如何将input_ids索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • labels (torch.LongTensor 形状为 (batch_size,)(batch_size, num_labels), 可选) — 用于计算分类损失的标签。如果形状为 (batch_size,),则使用交叉熵损失进行单标签分类。在这种情况下,标签应包含应在 [0, ..., config.num_labels - 1] 范围内的索引。如果形状为 (batch_size, num_labels),则使用二元交叉熵损失进行多标签分类。在这种情况下,标签应仅包含 [0, 1],其中 0 和 1 分别表示假和真。

返回

transformers.models.luke.modeling_luke.EntityClassificationOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.EntityClassificationOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 分类损失。
  • logits (torch.FloatTensor 形状为 (batch_size, config.num_labels)) — 分类分数(在 SoftMax 之前)。
  • hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每一层的输出),形状为 (batch_size, sequence_length, hidden_size)。模型在每一层输出处的隐藏状态 加上初始嵌入输出。
  • entity_hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每一层的输出),形状为 (batch_size, entity_length, hidden_size)。模型在每一层输出处的实体隐藏状态 加上初始实体嵌入输出。
  • attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每一层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)。注意力权重在注意力 softmax 之后,用于计算 自注意力头中的加权平均值。

LukeForEntityClassification 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeForEntityClassification

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-open-entity")
>>> model = LukeForEntityClassification.from_pretrained("studio-ousia/luke-large-finetuned-open-entity")

>>> text = "Beyoncé lives in Los Angeles."
>>> entity_spans = [(0, 7)]  # character-based entity span corresponding to "Beyoncé"
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> predicted_class_idx = logits.argmax(-1).item()
>>> print("Predicted class:", model.config.id2label[predicted_class_idx])
Predicted class: person

LukeForEntityPairClassification

transformers.LukeForEntityPairClassification

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,仅加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型在顶部带有分类头(在两个实体标记的隐藏状态之上的线性层),用于实体对分类任务,例如TACRED。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.LongTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.EntityPairClassificationOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor 形状为 (batch_size, entity_length), 可选) — 用于避免对填充实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 的实体令牌,
    • 1 对应于 部分 B 的实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor 形状为 (batch_size, sequence_length, hidden_size), 可选) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。如果您希望对如何将 input_ids 索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • labels (torch.LongTensor of shape (batch_size,) or (batch_size, num_labels), optional) — 用于计算分类损失的标签。如果形状是(batch_size,),则使用交叉熵损失进行单标签分类。在这种情况下,标签应包含应在[0, ..., config.num_labels - 1]范围内的索引。如果形状是(batch_size, num_labels),则使用二元交叉熵损失进行多标签分类。在这种情况下,标签应仅包含[0, 1],其中0和1分别表示假和真。

返回

transformers.models.luke.modeling_luke.EntityPairClassificationOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.EntityPairClassificationOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 分类损失。
  • logits (torch.FloatTensor 形状为 (batch_size, config.num_labels)) — 分类得分(在 SoftMax 之前)。
  • hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每层的输出),形状为 (batch_size, sequence_length, hidden_size)。模型在每层输出处的隐藏状态 加上初始嵌入输出。
  • entity_hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每层的输出),形状为 (batch_size, entity_length, hidden_size)。模型在每层输出处的实体隐藏状态 加上初始实体嵌入输出。
  • attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)。注意力权重在注意力 softmax 之后,用于计算 自注意力头中的加权平均值。

LukeForEntityPairClassification 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeForEntityPairClassification

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-tacred")
>>> model = LukeForEntityPairClassification.from_pretrained("studio-ousia/luke-large-finetuned-tacred")

>>> text = "Beyoncé lives in Los Angeles."
>>> entity_spans = [
...     (0, 7),
...     (17, 28),
... ]  # character-based entity spans corresponding to "Beyoncé" and "Los Angeles"
>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> predicted_class_idx = logits.argmax(-1).item()
>>> print("Predicted class:", model.config.id2label[predicted_class_idx])
Predicted class: per:cities_of_residence

LukeForEntitySpanClassification

transformers.LukeForEntitySpanClassification

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型在顶部带有跨度分类头(在隐藏状态输出之上的线性层),用于诸如命名实体识别等任务。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.LongTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None entity_start_positions: typing.Optional[torch.LongTensor] = None entity_end_positions: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.LongTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.EntitySpanClassificationOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 实体令牌,
    • 1 对应于 部分 B 实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids。如果您希望对如何将input_ids索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量中的attentions
  • output_hidden_states (bool, optional) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • entity_start_positions (torch.LongTensor) — 实体在单词标记序列中的起始位置。
  • entity_end_positions (torch.LongTensor) — 实体在单词标记序列中的结束位置。
  • labels (torch.LongTensor 形状为 (batch_size, entity_length)(batch_size, entity_length, num_labels), 可选) — 用于计算分类损失的标签。如果形状为 (batch_size, entity_length),则使用交叉熵损失进行单标签分类。在这种情况下,标签应包含应在 [0, ..., config.num_labels - 1] 范围内的索引。如果形状为 (batch_size, entity_length, num_labels),则使用二元交叉熵损失进行多标签分类。在这种情况下,标签应仅包含 [0, 1],其中 0 和 1 分别表示假和真。

返回

transformers.models.luke.modeling_luke.EntitySpanClassificationOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.EntitySpanClassificationOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,)可选,当提供 labels 时返回) — 分类损失。
  • logits (torch.FloatTensor 形状为 (batch_size, entity_length, config.num_labels)) — 分类分数(在 SoftMax 之前)。
  • hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每一层的输出),形状为 (batch_size, sequence_length, hidden_size)。模型在每一层输出处的隐藏状态 加上初始嵌入输出。
  • entity_hidden_states (tuple(torch.FloatTensor)可选,当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入的输出 + 一个用于每一层的输出),形状为 (batch_size, entity_length, hidden_size)。模型在每一层输出处的实体隐藏状态 加上初始实体嵌入输出。
  • attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每一层一个),形状为 (batch_size, num_heads, sequence_length, sequence_length)。注意力权重在注意力 softmax 之后,用于计算 自注意力头中的加权平均值。

LukeForEntitySpanClassification 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeForEntitySpanClassification

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-conll-2003")
>>> model = LukeForEntitySpanClassification.from_pretrained("studio-ousia/luke-large-finetuned-conll-2003")

>>> text = "Beyoncé lives in Los Angeles"
# List all possible entity spans in the text

>>> word_start_positions = [0, 8, 14, 17, 21]  # character-based start positions of word tokens
>>> word_end_positions = [7, 13, 16, 20, 28]  # character-based end positions of word tokens
>>> entity_spans = []
>>> for i, start_pos in enumerate(word_start_positions):
...     for end_pos in word_end_positions[i:]:
...         entity_spans.append((start_pos, end_pos))

>>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
>>> outputs = model(**inputs)
>>> logits = outputs.logits
>>> predicted_class_indices = logits.argmax(-1).squeeze().tolist()
>>> for span, predicted_class_idx in zip(entity_spans, predicted_class_indices):
...     if predicted_class_idx != 0:
...         print(text[span[0] : span[1]], model.config.id2label[predicted_class_idx])
Beyoncé PER
Los Angeles LOC

LukeForSequenceClassification

transformers.LukeForSequenceClassification

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型转换器,顶部带有序列分类/回归头(在池化输出之上的线性层),例如用于GLUE任务。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.LukeSequenceClassifierOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 的实体令牌,
    • 1 对应于 部分 B 的实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids。如果您希望对如何将input_ids索引转换为相关向量有更多控制权,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量中的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • labels (torch.LongTensor of shape (batch_size,), optional) — 用于计算序列分类/回归损失的标签。索引应在 [0, ..., config.num_labels - 1] 范围内。如果 config.num_labels == 1,则计算回归损失(均方损失),如果 config.num_labels > 1,则计算分类损失(交叉熵)。

返回

transformers.models.luke.modeling_luke.LukeSequenceClassifierOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.LukeSequenceClassifierOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,), 可选, 当提供 labels 时返回) — 分类(或回归,如果 config.num_labels==1)损失。

  • logits (torch.FloatTensor 形状为 (batch_size, config.num_labels)) — 分类(或回归,如果 config.num_labels==1)得分(在 SoftMax 之前)。

  • hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,如果模型有嵌入层,+ 一个用于每一层的输出)形状为 (batch_size, sequence_length, hidden_size)

    模型在每一层输出处的隐藏状态加上可选的初始嵌入输出。

  • entity_hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出 + 一个用于每一层的输出)形状为 (batch_size, entity_length, hidden_size)。模型在每一层输出处的实体隐藏状态加上初始实体嵌入输出。

  • attentions (tuple(torch.FloatTensor), 可选, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每一层一个)形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

LukeForSequenceClassification 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

单标签分类示例:

>>> import torch
>>> from transformers import AutoTokenizer, LukeForSequenceClassification

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-base")
>>> model = LukeForSequenceClassification.from_pretrained("studio-ousia/luke-base")

>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")

>>> with torch.no_grad():
...     logits = model(**inputs).logits

>>> predicted_class_id = logits.argmax().item()

>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
>>> num_labels = len(model.config.id2label)
>>> model = LukeForSequenceClassification.from_pretrained("studio-ousia/luke-base", num_labels=num_labels)

>>> labels = torch.tensor([1])
>>> loss = model(**inputs, labels=labels).loss

多标签分类示例:

>>> import torch
>>> from transformers import AutoTokenizer, LukeForSequenceClassification

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-base")
>>> model = LukeForSequenceClassification.from_pretrained("studio-ousia/luke-base", problem_type="multi_label_classification")

>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")

>>> with torch.no_grad():
...     logits = model(**inputs).logits

>>> predicted_class_ids = torch.arange(0, logits.shape[-1])[torch.sigmoid(logits).squeeze(dim=0) > 0.5]

>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
>>> num_labels = len(model.config.id2label)
>>> model = LukeForSequenceClassification.from_pretrained(
...     "studio-ousia/luke-base", num_labels=num_labels, problem_type="multi_label_classification"
... )

>>> labels = torch.sum(
...     torch.nn.functional.one_hot(predicted_class_ids[None, :].clone(), num_classes=num_labels), dim=1
... ).to(torch.float)
>>> loss = model(**inputs, labels=labels).loss

LukeForMultipleChoice

transformers.LukeForMultipleChoice

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型顶部带有多项选择分类头(在池化输出顶部有一个线性层和一个softmax),例如用于RocStories/SWAG任务。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.LukeMultipleChoiceModelOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, num_choices, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, num_choices, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, num_choices, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, num_choices, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充的实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 实体令牌,
    • 1 对应于 部分 B 实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor of shape (batch_size, num_choices, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids。如果您希望对如何将input_ids索引转换为相关向量有更多控制权,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个 ModelOutput 而不是一个普通的元组。
  • labels (torch.LongTensor of shape (batch_size,), optional) — 用于计算多项选择分类损失的标签。索引应在 [0, ..., num_choices-1] 范围内,其中 num_choices 是输入张量第二维的大小。(参见上面的 input_ids

返回

transformers.models.luke.modeling_luke.LukeMultipleChoiceModelOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.LukeMultipleChoiceModelOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,), 可选, 当提供 labels 时返回) — 分类损失。

  • logits (torch.FloatTensor 形状为 (batch_size, num_choices)) — num_choices 是输入张量的第二维度。(见上面的 input_ids)。

    分类分数(在 SoftMax 之前)。

  • hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,如果模型有嵌入层,+ 一个用于每一层的输出)形状为 (batch_size, sequence_length, hidden_size)

    模型在每一层输出处的隐藏状态加上可选的初始嵌入输出。

  • entity_hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出 + 一个用于每一层的输出)形状为 (batch_size, entity_length, hidden_size)。模型在每一层输出处的实体隐藏状态加上初始实体嵌入输出。

  • attentions (tuple(torch.FloatTensor), 可选, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每一层一个)形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力权重在注意力 softmax 之后,用于计算自注意力头中的加权平均值。

LukeForMultipleChoice 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeForMultipleChoice
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-base")
>>> model = LukeForMultipleChoice.from_pretrained("studio-ousia/luke-base")

>>> prompt = "In Italy, pizza served in formal settings, such as at a restaurant, is presented unsliced."
>>> choice0 = "It is eaten with a fork and a knife."
>>> choice1 = "It is eaten while held in the hand."
>>> labels = torch.tensor(0).unsqueeze(0)  # choice0 is correct (according to Wikipedia ;)), batch size 1

>>> encoding = tokenizer([prompt, prompt], [choice0, choice1], return_tensors="pt", padding=True)
>>> outputs = model(**{k: v.unsqueeze(0) for k, v in encoding.items()}, labels=labels)  # batch size is 1

>>> # the linear classifier still needs to be trained
>>> loss = outputs.loss
>>> logits = outputs.logits

LukeForTokenClassification

transformers.LukeForTokenClassification

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型在顶部有一个标记分类头(在隐藏状态输出之上的线性层)。要使用LUKE解决命名实体识别(NER)任务,LukeForEntitySpanClassification比这个类更合适。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.LongTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.FloatTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.LukeTokenClassifierOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 实体令牌,
    • 1 对应于 部分 B 实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。如果您希望对如何将 input_ids 索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, optional) — 是否返回一个ModelOutput而不是一个普通的元组。
  • labels (torch.LongTensor of shape (batch_size,), optional) — 用于计算多项选择分类损失的标签。索引应在 [0, ..., num_choices-1] 范围内,其中 num_choices 是输入张量第二维的大小。(参见上面的 input_ids

返回

transformers.models.luke.modeling_luke.LukeTokenClassifierOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.LukeTokenClassifierOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,), 可选, 当提供 labels 时返回) — 分类损失。

  • logits (torch.FloatTensor 形状为 (batch_size, sequence_length, config.num_labels)) — 分类分数(在 SoftMax 之前)。

  • hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,如果模型有嵌入层,+ 一个用于每一层的输出)形状为 (batch_size, sequence_length, hidden_size)

    模型在每一层输出处的隐藏状态加上可选的初始嵌入输出。

  • entity_hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出 + 一个用于每一层的输出)形状为 (batch_size, entity_length, hidden_size)。模型在每一层输出处的实体隐藏状态加上初始实体嵌入输出。

  • attentions (tuple(torch.FloatTensor), 可选, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每一层一个)形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

LukeForTokenClassification 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeForTokenClassification
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-base")
>>> model = LukeForTokenClassification.from_pretrained("studio-ousia/luke-base")

>>> inputs = tokenizer(
...     "HuggingFace is a company based in Paris and New York", add_special_tokens=False, return_tensors="pt"
... )

>>> with torch.no_grad():
...     logits = model(**inputs).logits

>>> predicted_token_class_ids = logits.argmax(-1)

>>> # Note that tokens are classified rather then input words which means that
>>> # there might be more predicted token classes than words.
>>> # Multiple token classes might account for the same word
>>> predicted_tokens_classes = [model.config.id2label[t.item()] for t in predicted_token_class_ids[0]]

>>> labels = predicted_token_class_ids
>>> loss = model(**inputs, labels=labels).loss

LukeForQuestionAnswering

transformers.LukeForQuestionAnswering

< >

( config )

参数

  • config (LukeConfig) — 包含模型所有参数的模型配置类。使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。

LUKE模型在顶部带有跨度分类头,用于抽取式问答任务,如SQuAD(在隐藏状态输出之上的线性层用于计算span start logitsspan end logits)。

该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。

前进

< >

( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None token_type_ids: typing.Optional[torch.LongTensor] = None position_ids: typing.Optional[torch.FloatTensor] = None entity_ids: typing.Optional[torch.LongTensor] = None entity_attention_mask: typing.Optional[torch.FloatTensor] = None entity_token_type_ids: typing.Optional[torch.LongTensor] = None entity_position_ids: typing.Optional[torch.LongTensor] = None head_mask: typing.Optional[torch.FloatTensor] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None start_positions: typing.Optional[torch.LongTensor] = None end_positions: typing.Optional[torch.LongTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.luke.modeling_luke.LukeQuestionAnsweringModelOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

    什么是输入ID?

  • attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — Mask to avoid performing attention on padding token indices. Mask values selected in [0, 1]:
    • 1 for tokens that are not masked,
    • 0 for tokens that are masked.

    什么是注意力掩码?

  • token_type_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Segment token indices to indicate first and second portions of the inputs. Indices are selected in [0, 1]:
    • 0 corresponds to a sentence A token,
    • 1 corresponds to a sentence B token.

    什么是token type IDs?

  • position_ids (torch.LongTensor of shape (batch_size, sequence_length), optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, config.max_position_embeddings - 1].

    什么是位置ID?

  • entity_ids (torch.LongTensor of shape (batch_size, entity_length)) — Indices of entity tokens in the entity vocabulary.

    可以使用AutoTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()PreTrainedTokenizer.call()

  • entity_attention_mask (torch.FloatTensor of shape (batch_size, entity_length), optional) — 用于避免对填充实体标记索引执行注意力的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示 未掩码 的实体标记,
    • 0 表示 掩码 的实体标记。
  • entity_token_type_ids (torch.LongTensor of shape (batch_size, entity_length), optional) — 用于指示实体令牌输入的第一部分和第二部分的段令牌索引。索引在 [0, 1] 中选择:
    • 0 对应于 部分 A 实体令牌,
    • 1 对应于 部分 B 实体令牌。
  • entity_position_ids (torch.LongTensor of shape (batch_size, entity_length, max_mention_length), optional) — 每个输入实体在位置嵌入中的位置索引。选择范围在 [0, config.max_position_embeddings - 1] 之间。
  • inputs_embeds (torch.FloatTensor 形状为 (batch_size, sequence_length, hidden_size), 可选) — 可选地,您可以选择直接传递嵌入表示,而不是传递 input_ids。如果您希望对如何将 input_ids 索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。
  • head_mask (torch.FloatTensor 形状为 (num_heads,)(num_layers, num_heads), 可选) — 用于屏蔽自注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, optional) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • start_positions (torch.LongTensor of shape (batch_size,), optional) — 用于计算标记分类损失的标记跨度起始位置(索引)的标签。 位置被限制在序列长度内(sequence_length)。序列之外的位置不会被考虑用于计算损失。
  • end_positions (torch.LongTensor of shape (batch_size,), optional) — 用于计算标记分类损失的标记跨度结束位置(索引)的标签。 位置被限制在序列长度内(sequence_length)。序列之外的位置不会用于计算损失。

返回

transformers.models.luke.modeling_luke.LukeQuestionAnsweringModelOutputtuple(torch.FloatTensor)

一个 transformers.models.luke.modeling_luke.LukeQuestionAnsweringModelOutput 或一个由 torch.FloatTensor 组成的元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种 元素,具体取决于配置(LukeConfig)和输入。

  • loss (torch.FloatTensor 形状为 (1,), 可选, 当提供 labels 时返回) — 总跨度提取损失是起始和结束位置的交叉熵之和。

  • start_logits (torch.FloatTensor 形状为 (batch_size, sequence_length)) — 跨度起始分数(在 SoftMax 之前)。

  • end_logits (torch.FloatTensor 形状为 (batch_size, sequence_length)) — 跨度结束分数(在 SoftMax 之前)。

  • hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,如果模型有嵌入层,+ 一个用于每层的输出)形状为 (batch_size, sequence_length, hidden_size)

    模型在每层输出处的隐藏状态加上可选的初始嵌入输出。

  • entity_hidden_states (tuple(torch.FloatTensor), 可选, 当传递 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出 + 一个用于每层的输出)形状为 (batch_size, entity_length, hidden_size)。模型在每层输出处的实体隐藏状态加上初始实体嵌入输出。

  • attentions (tuple(torch.FloatTensor), 可选, 当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每层一个)形状为 (batch_size, num_heads, sequence_length, sequence_length)

    注意力 softmax 后的注意力权重,用于计算自注意力头中的加权平均值。

LukeForQuestionAnswering 的前向方法,重写了 __call__ 特殊方法。

尽管前向传递的配方需要在此函数内定义,但之后应该调用Module实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。

示例:

>>> from transformers import AutoTokenizer, LukeForQuestionAnswering
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("studio-ousia/luke-base")
>>> model = LukeForQuestionAnswering.from_pretrained("studio-ousia/luke-base")

>>> question, text = "Who was Jim Henson?", "Jim Henson was a nice puppet"

>>> inputs = tokenizer(question, text, return_tensors="pt")
>>> with torch.no_grad():
...     outputs = model(**inputs)

>>> answer_start_index = outputs.start_logits.argmax()
>>> answer_end_index = outputs.end_logits.argmax()

>>> predict_answer_tokens = inputs.input_ids[0, answer_start_index : answer_end_index + 1]

>>> # target is "nice puppet"
>>> target_start_index = torch.tensor([14])
>>> target_end_index = torch.tensor([15])

>>> outputs = model(**inputs, start_positions=target_start_index, end_positions=target_end_index)
>>> loss = outputs.loss
< > Update on GitHub