Transformers 文档

先知网络

ProphetNet

Models Spaces

概述

ProphetNet模型由Yu Yan、Weizhen Qi、Yeyun Gong、Dayiheng Liu、Nan Duan、Jiusheng Chen、Ruofei Zhang和Ming Zhou于2020年1月13日在ProphetNet: Predicting Future N-gram for Sequence-to-Sequence Pre-training中提出。

ProphetNet 是一种编码器-解码器模型,可以预测“ngram”语言建模的n个未来标记,而不仅仅是下一个标记。

论文的摘要如下:

在本文中,我们提出了一种新的序列到序列预训练模型,称为ProphetNet,该模型引入了一种新颖的自监督目标,称为未来n-gram预测,并提出了n流自注意力机制。与传统的序列到序列模型中的一步预测优化不同,ProphetNet通过n步预测进行优化,该预测在每个时间步基于先前的上下文标记同时预测接下来的n个标记。未来n-gram预测明确鼓励模型为未来的标记进行规划,并防止在强局部相关性上过拟合。我们分别使用基础规模数据集(16GB)和大规模数据集(160GB)对ProphetNet进行预训练。然后,我们在CNN/DailyMail、Gigaword和SQuAD 1.1基准上进行了抽象摘要和问题生成任务的实验。实验结果表明,与使用相同规模预训练语料库的模型相比,ProphetNet在所有这些数据集上都达到了新的最先进结果。

作者的代码可以在这里找到。

使用提示

  • ProphetNet 是一个带有绝对位置嵌入的模型,因此通常建议在右侧而不是左侧填充输入。
  • 模型架构基于原始的Transformer,但在解码器中用主自注意力机制和自及n流(预测)自注意力机制替换了“标准”自注意力机制。

资源

ProphetNetConfig

transformers.ProphetNetConfig

< >

( activation_dropout: typing.Optional[float] = 0.1 activation_function: typing.Union[str, typing.Callable, NoneType] = 'gelu' vocab_size: typing.Optional[int] = 30522 hidden_size: typing.Optional[int] = 1024 encoder_ffn_dim: typing.Optional[int] = 4096 num_encoder_layers: typing.Optional[int] = 12 num_encoder_attention_heads: typing.Optional[int] = 16 decoder_ffn_dim: typing.Optional[int] = 4096 num_decoder_layers: typing.Optional[int] = 12 num_decoder_attention_heads: typing.Optional[int] = 16 attention_dropout: typing.Optional[float] = 0.1 dropout: typing.Optional[float] = 0.1 max_position_embeddings: typing.Optional[int] = 512 init_std: typing.Optional[float] = 0.02 is_encoder_decoder: typing.Optional[bool] = True add_cross_attention: typing.Optional[bool] = True decoder_start_token_id: typing.Optional[int] = 0 ngram: typing.Optional[int] = 2 num_buckets: typing.Optional[int] = 32 relative_max_distance: typing.Optional[int] = 128 disable_ngram_loss: typing.Optional[bool] = False eps: typing.Optional[float] = 0.0 use_cache: typing.Optional[bool] = True pad_token_id: typing.Optional[int] = 0 bos_token_id: typing.Optional[int] = 1 eos_token_id: typing.Optional[int] = 2 **kwargs )

参数

  • activation_dropout (float, optional, 默认为 0.1) — 全连接层内部激活的丢弃比例。
  • activation_function (strfunction, 可选, 默认为 "gelu") — 编码器和池化器中的非线性激活函数(函数或字符串)。如果是字符串,支持 "gelu""relu""silu""gelu_new"
  • vocab_size (int, 可选, 默认为 30522) — ProphetNET 模型的词汇表大小。定义了调用 ProphetNetModel 时传递的 inputs_ids 可以表示的不同标记的数量。
  • hidden_size (int, optional, 默认为 1024) — 层的维度和池化层的维度。
  • encoder_ffn_dim (int, optional, defaults to 4096) — 解码器中“中间”(通常称为前馈)层的维度。
  • num_encoder_layers (int, optional, 默认为 12) — 编码器层数.
  • num_encoder_attention_heads (int, 可选, 默认为 16) — Transformer编码器中每个注意力层的注意力头数。
  • decoder_ffn_dim (int, optional, defaults to 4096) — 解码器中intermediate(通常称为前馈)层的维度。
  • num_decoder_layers (int, optional, defaults to 12) — 解码器层数.
  • num_decoder_attention_heads (int, optional, 默认为 16) — Transformer解码器中每个注意力层的注意力头数。
  • attention_dropout (float, optional, 默认为 0.1) — 注意力概率的丢弃比例。
  • dropout (float, optional, defaults to 0.1) — 嵌入层、编码器和池化器中所有全连接层的dropout概率。
  • max_position_embeddings (int, optional, 默认为 512) — 此模型可能使用的最大序列长度。通常将其设置为较大的值以防万一(例如,512、1024 或 2048)。
  • init_std (float, optional, 默认为 0.02) — 用于初始化所有权重矩阵的 truncated_normal_initializer 的标准差。
  • add_cross_attention (bool, optional, defaults to True) — 是否应该向模型添加交叉注意力层。
  • is_encoder_decoder (bool, optional, defaults to True) — 这是否是一个编码器/解码器模型。
  • pad_token_id (int, optional, 默认为 1) — 填充标记的ID.
  • bos_token_id (int, optional, 默认为 0) — 流的开始标记 id.
  • eos_token_id (int, optional, defaults to 2) — 流结束标记的ID。
  • ngram (int, 可选, 默认为 2) — 要预测的未来标记的数量。设置为1以与传统语言模型相同,预测下一个第一个标记。
  • num_buckets (int, 可选, 默认为 32) — 用于每个注意力层的桶数。这是用于相对位置计算的。更多详情请参见 [T5 论文](see https://arxiv.org/abs/1910.10683)。
  • relative_max_distance (int, 可选, 默认为 128) — 相对距离大于此数值的将被放入同一个最后的桶中。这是用于相对位置计算的。更多详情请参见 [T5 论文](see https://arxiv.org/abs/1910.10683)。
  • disable_ngram_loss (bool, optional, defaults to False) — 是否仅训练预测下一个第一个标记。
  • eps (float, 可选, 默认为 0.0) — 控制损失计算中标签平滑的epsilon参数值。如果设置为0,则不执行标签平滑。
  • use_cache (bool, 可选, 默认为 True) — 模型是否应返回最后的键/值注意力(并非所有模型都使用)。

这是用于存储ProphetNetModel配置的配置类。它用于根据指定的参数实例化一个ProphetNet模型,定义模型架构。使用默认值实例化配置将产生与ProphetNet microsoft/prophetnet-large-uncased架构类似的配置。

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

ProphetNetTokenizer

transformers.ProphetNetTokenizer

< >

( vocab_file: str do_lower_case: typing.Optional[bool] = True do_basic_tokenize: typing.Optional[bool] = True never_split: typing.Optional[typing.Iterable] = None unk_token: typing.Optional[str] = '[UNK]' sep_token: typing.Optional[str] = '[SEP]' x_sep_token: typing.Optional[str] = '[X_SEP]' pad_token: typing.Optional[str] = '[PAD]' mask_token: typing.Optional[str] = '[MASK]' tokenize_chinese_chars: typing.Optional[bool] = True strip_accents: typing.Optional[bool] = None clean_up_tokenization_spaces: bool = True **kwargs )

参数

  • vocab_file (str) — 包含词汇表的文件。
  • do_lower_case (bool, optional, defaults to True) — 是否在分词时将输入转换为小写。
  • do_basic_tokenize (bool, optional, defaults to True) — 是否在WordPiece之前进行基本的分词。
  • never_split (Iterable, 可选) — 在分词过程中永远不会被分割的标记集合。仅在 do_basic_tokenize=True
  • unk_token (str, optional, defaults to "[UNK]") — 未知标记。不在词汇表中的标记无法转换为ID,而是设置为这个标记。
  • sep_token (str, optional, defaults to "[SEP]") — 分隔符标记,用于从多个序列构建序列时,例如用于序列分类的两个序列或用于问答的文本和问题。它也用作使用特殊标记构建的序列的最后一个标记。
  • x_sep_token (str, 可选, 默认为 "[X_SEP]") — 特殊的第二分隔符标记,可以由 ProphetNetForConditionalGeneration 生成。它用于在摘要中分隔类似项目符号的句子,例如.
  • pad_token (str, optional, defaults to "[PAD]") — 用于填充的标记,例如在批处理不同长度的序列时使用。
  • mask_token (str, 可选, 默认为 "[MASK]") — 用于屏蔽值的标记。这是在训练此模型时用于屏蔽语言建模的标记。这是模型将尝试预测的标记。
  • tokenize_chinese_chars (bool, optional, defaults to True) — Whether or not to tokenize Chinese characters.

    这可能应该为日语停用(参见此 issue)。

  • strip_accents (bool, 可选) — 是否去除所有重音符号。如果未指定此选项,则将由lowercase的值决定(如原始BERT中所示)。
  • clean_up_tokenization_spaces (bool, optional, defaults to True) — 是否在解码后清理空格,清理包括移除潜在的额外空格等可能的痕迹。

构建一个ProphetNetTokenizer。基于WordPiece。

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

build_inputs_with_special_tokens

< >

( token_ids_0: typing.List[int] token_ids_1: typing.Optional[typing.List[int]] = None ) List[int]

参数

  • token_ids_0 (List[int]) — 特殊令牌将被添加到的ID列表。
  • token_ids_1 (List[int], optional) — 可选的第二个序列对的ID列表。

返回

List[int]

带有适当特殊标记的输入ID列表。

通过连接和添加特殊标记,从序列或序列对构建序列分类任务的模型输入。BERT序列的格式如下:

  • 单一序列: [CLS] X [SEP]
  • 序列对:[CLS] A [SEP] B [SEP]

convert_tokens_to_string

< >

( tokens: str )

将一系列标记(字符串)转换为单个字符串。

create_token_type_ids_from_sequences

< >

( token_ids_0: typing.List[int] token_ids_1: typing.Optional[typing.List[int]] = None ) List[int]

参数

  • token_ids_0 (List[int]) — ID列表.
  • token_ids_1 (List[int], optional) — 可选的第二个序列对的ID列表。

返回

List[int]

根据给定序列的token type IDs列表。

从传递给序列对分类任务的两个序列中创建一个掩码。一个ProphetNet

序列对掩码具有以下格式:

0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence    | second sequence |

如果 token_ids_1None,此方法仅返回掩码的第一部分(0s)。

get_special_tokens_mask

< >

( token_ids_0: typing.List[int] token_ids_1: typing.Optional[typing.List[int]] = None already_has_special_tokens: typing.Optional[bool] = False ) List[int]

参数

  • token_ids_0 (List[int]) — ID列表.
  • token_ids_1 (List[int], optional) — 可选的第二个序列对的ID列表。
  • already_has_special_tokens (bool, optional, defaults to False) — 令牌列表是否已经用模型的特殊令牌格式化。

返回

List[int]

一个整数列表,范围在[0, 1]:1表示特殊标记,0表示序列标记。

从没有添加特殊标记的标记列表中检索序列ID。当使用标记器的prepare_for_model方法添加特殊标记时,会调用此方法。

ProphetNet 特定输出

transformers.models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqLMOutput

< >

( loss: typing.Optional[torch.FloatTensor] = None logits: FloatTensor = None logits_ngram: typing.Optional[torch.FloatTensor] = None past_key_values: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_ngram_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_ngram_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None cross_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_last_hidden_state: typing.Optional[torch.FloatTensor] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None )

参数

  • loss (torch.FloatTensor of shape (1,), optional, 当提供 labels 时返回) — 语言建模损失.
  • logits (torch.FloatTensor of shape (batch_size, decoder_sequence_length, config.vocab_size)) — 主流语言建模头的预测分数(在SoftMax之前每个词汇标记的分数)。
  • logits_ngram (torch.FloatTensor of shape (batch_size, ngram * decoder_sequence_length, config.vocab_size)) — 预测流语言建模头的预测分数(在SoftMax之前每个词汇标记的分数)。
  • past_key_values (List[torch.FloatTensor], optional, returned when use_cache=True is passed or when config.use_cache=True) — List of torch.FloatTensor of length config.n_layers, with each tensor of shape (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)).

    包含解码器的预计算隐藏状态(注意力块中的键和值),这些状态可以用于(参见past_key_values输入)以加速顺序解码。

  • decoder_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, decoder_sequence_length, hidden_size).

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

  • decoder_ngram_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, ngram * decoder_sequence_length, hidden_size).

    解码器预测流的隐藏状态在每层输出加上初始嵌入输出。

  • decoder_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

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

  • decoder_ngram_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

    解码器预测流的注意力权重,在注意力softmax之后,用于计算自注意力头中的加权平均值。

  • cross_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, encoder_sequence_length, decoder_sequence_length).

    解码器交叉注意力层的注意力权重,在注意力softmax之后,用于计算加权平均值。

  • encoder_last_hidden_state (torch.FloatTensor of shape (batch_size, encoder_sequence_length, hidden_size), optional) — 模型编码器最后一层输出的隐藏状态序列。
  • encoder_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, encoder_sequence_length, hidden_size).

    编码器在每一层输出处的隐藏状态加上初始嵌入输出。

  • encoder_attentions (tuple(torch.FloatTensor), 可选, 当传递 output_attentions=Trueconfig.output_attentions=True 时返回) — torch.FloatTensor 的元组(每层一个),形状为 (batch_size, num_attn_heads, encoder_sequence_length, encoder_sequence_length)。编码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。

序列到序列语言模型输出的基类。

transformers.models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqModelOutput

< >

( last_hidden_state: FloatTensor last_hidden_state_ngram: typing.Optional[torch.FloatTensor] = None past_key_values: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_ngram_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None decoder_ngram_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None cross_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_last_hidden_state: typing.Optional[torch.FloatTensor] = None encoder_hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None encoder_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None )

参数

  • last_hidden_state (torch.FloatTensor of shape (batch_size, decoder_sequence_length, hidden_size)) — Sequence of main stream hidden-states at the output of the last layer of the decoder of the model.

    如果使用了past_key_values,则只输出形状为(batch_size, 1, hidden_size)的序列的最后一个隐藏状态。

  • last_hidden_state_ngram (torch.FloatTensor of shape (batch_size,ngram * decoder_sequence_length, config.vocab_size), optional) — 模型解码器最后一层输出的预测流隐藏状态序列。
  • past_key_values (List[torch.FloatTensor], optional, returned when use_cache=True is passed or when config.use_cache=True) — List of torch.FloatTensor of length config.n_layers, with each tensor of shape (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)).

    包含解码器的预计算隐藏状态(注意力块中的键和值),这些状态可以用于(参见past_key_values输入)以加速顺序解码。

  • decoder_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, decoder_sequence_length, hidden_size).

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

  • decoder_ngram_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, ngram * decoder_sequence_length, hidden_size).

    解码器预测流的隐藏状态在每层输出加上初始嵌入输出。

  • decoder_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

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

  • decoder_ngram_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

    解码器预测流的注意力权重,在注意力softmax之后,用于计算加权平均值。

  • cross_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, encoder_sequence_length, decoder_sequence_length).

    解码器交叉注意力层的注意力权重,在注意力softmax之后,用于计算加权平均值。

  • encoder_last_hidden_state (torch.FloatTensor of shape (batch_size, encoder_sequence_length, hidden_size), optional) — 模型编码器最后一层输出的隐藏状态序列。
  • encoder_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, encoder_sequence_length, hidden_size).

    编码器在每一层输出处的隐藏状态加上初始嵌入输出。

  • encoder_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, encoder_sequence_length, encoder_sequence_length).

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

模型编码器输出的基类,还包含:可以加速顺序解码的预计算隐藏状态。

transformers.models.prophetnet.modeling_prophetnet.ProphetNetDecoderModelOutput

< >

( last_hidden_state: FloatTensor last_hidden_state_ngram: typing.Optional[torch.FloatTensor] = None past_key_values: typing.Optional[typing.Tuple[torch.FloatTensor]] = None hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None hidden_states_ngram: typing.Optional[typing.Tuple[torch.FloatTensor]] = None attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None ngram_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None cross_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None )

参数

  • last_hidden_state (torch.FloatTensor of shape (batch_size, decoder_sequence_length, hidden_size)) — Sequence of main stream hidden-states at the output of the last layer of the decoder of the model.

    如果使用了past_key_values,则只输出形状为(batch_size, 1, hidden_size)的序列的最后一个隐藏状态。

  • last_hidden_state_ngram (torch.FloatTensor of shape (batch_size, ngram * decoder_sequence_length, config.vocab_size)) — 模型解码器最后一层输出的预测流隐藏状态序列。
  • past_key_values (List[torch.FloatTensor], optional, returned when use_cache=True is passed or when config.use_cache=True) — List of torch.FloatTensor of length config.n_layers, with each tensor of shape (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)).

    包含解码器的预计算隐藏状态(注意力块中的键和值),这些状态可以用于(参见past_key_values输入)以加速顺序解码。

  • hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, decoder_sequence_length, hidden_size).

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

  • ngram_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, ngram * decoder_sequence_length, hidden_size).

    解码器预测流的隐藏状态在每层输出加上初始嵌入输出。

  • attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

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

  • ngram_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

    解码器预测流的注意力权重,在注意力softmax之后,用于计算加权平均值。

  • cross_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, encoder_sequence_length, decoder_sequence_length).

    解码器交叉注意力层的注意力权重,在注意力softmax之后,用于计算加权平均值。

模型输出的基类,可能还包含过去的键/值(以加速顺序解码)。

transformers.models.prophetnet.modeling_prophetnet.ProphetNetDecoderLMOutput

< >

( loss: typing.Optional[torch.FloatTensor] = None logits: FloatTensor = None logits_ngram: typing.Optional[torch.FloatTensor] = None past_key_values: typing.Optional[typing.Tuple[torch.FloatTensor]] = None hidden_states: typing.Optional[typing.Tuple[torch.FloatTensor]] = None hidden_states_ngram: typing.Optional[typing.Tuple[torch.FloatTensor]] = None attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None ngram_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None cross_attentions: typing.Optional[typing.Tuple[torch.FloatTensor]] = None )

参数

  • loss (torch.FloatTensor of shape (1,), optional, returned when labels is provided) — 语言建模损失.
  • logits (torch.FloatTensor of shape (batch_size, decoder_sequence_length, config.vocab_size)) — 主流语言建模头的预测分数(SoftMax之前每个词汇标记的分数)。
  • logits_ngram (torch.FloatTensor of shape (batch_size, ngram * decoder_sequence_length, config.vocab_size)) — 预测流语言建模头的预测分数(在SoftMax之前每个词汇标记的分数)。
  • past_key_values (List[torch.FloatTensor], optional, returned when use_cache=True is passed or when config.use_cache=True) — List of torch.FloatTensor of length config.n_layers, with each tensor of shape (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)).

    包含解码器的预计算隐藏状态(注意力块中的键和值),这些状态可以用于(参见past_key_values输入)以加速顺序解码。

  • hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, decoder_sequence_length, hidden_size).

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

  • ngram_hidden_states (tuple(torch.FloatTensor), optional, returned when output_hidden_states=True is passed or when config.output_hidden_states=True) — Tuple of torch.FloatTensor (one for the output of the embeddings + one for the output of each layer) of shape (batch_size, ngram * decoder_sequence_length, hidden_size).

    解码器预测流的隐藏状态在每层输出加上初始嵌入输出。

  • attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

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

  • ngram_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length).

    解码器预测流的注意力权重,在注意力softmax之后,用于计算加权平均值。

  • cross_attentions (tuple(torch.FloatTensor), optional, returned when output_attentions=True is passed or when config.output_attentions=True) — Tuple of torch.FloatTensor (one for each layer) of shape (batch_size, num_attn_heads, encoder_sequence_length, decoder_sequence_length).

    解码器交叉注意力层的注意力权重,在注意力softmax之后,用于计算加权平均值。

模型输出的基类,可能还包含过去的键/值(以加速顺序解码)。

ProphetNetModel

transformers.ProphetNetModel

< >

( config: ProphetNetConfig )

参数

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

基本的ProphetNet模型输出原始的隐藏状态,没有任何特定的头部。 该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。

原始的ProphetNet代码可以在这里找到。检查点是从原始的Fairseq检查点转换而来。有关检查点转换的更多信息,请查看文件convert_prophetnet_original_pytorch_checkpoint_to_pytorch.py

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

前进

< >

( input_ids: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None decoder_input_ids: typing.Optional[torch.Tensor] = None decoder_attention_mask: typing.Optional[torch.BoolTensor] = None head_mask: typing.Optional[torch.Tensor] = None decoder_head_mask: typing.Optional[torch.Tensor] = None cross_attn_head_mask: typing.Optional[torch.Tensor] = None encoder_outputs: typing.Optional[typing.Tuple] = None past_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.Tensor]]] = None inputs_embeds: typing.Optional[torch.Tensor] = None decoder_inputs_embeds: typing.Optional[torch.Tensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqModelOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.

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

    什么是输入ID?

  • attention_mask (torch.Tensor 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.

    什么是注意力掩码?

  • decoder_input_ids (torch.LongTensor of shape (batch_size, target_sequence_length), optional) — Indices of decoder input sequence tokens in the vocabulary.

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

    什么是解码器输入ID?

    ProphetNet 使用 eos_token_id 作为 decoder_input_ids 生成的起始标记。如果使用了 past_key_values,则可以选择只输入最后一个 decoder_input_ids(参见 past_key_values)。

  • decoder_attention_mask (torch.BoolTensor of shape (batch_size, target_sequence_length), 可选) — 默认行为:生成一个忽略decoder_input_ids中填充标记的张量。默认情况下也会使用因果掩码。
  • head_mask (torch.Tensor of shape (encoder_layers, encoder_attention_heads), optional) — 用于在编码器中屏蔽注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • decoder_head_mask (torch.Tensor of shape (decoder_layers, decoder_attention_heads), optional) — 用于在解码器中取消选择注意力模块的头部。选择的掩码值在 [0, 1] 中:
    • 1 表示头部 未被掩码,
    • 0 表示头部 被掩码.
  • cross_attn_head_mask (torch.Tensor of shape (decoder_layers, decoder_attention_heads), optional) — 用于屏蔽交叉注意力模块中选定头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • encoder_outputs (tuple(tuple(torch.FloatTensor), 可选) — 元组由 (last_hidden_state, 可选: hidden_states, 可选: attentions) last_hidden_state 的形状为 (batch_size, sequence_length, hidden_size), 可选) 是编码器最后一层的输出隐藏状态序列。用于解码器的交叉注意力中。
  • past_key_values (tuple(tuple(torch.FloatTensor)) of length config.n_layers with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)) — Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding.

    如果使用了past_key_values,用户可以选择只输入形状为(batch_size, 1)的最后一个decoder_input_ids(那些没有将其过去键值状态提供给此模型的),而不是形状为(batch_size, sequence_length)的所有decoder_input_ids

  • use_cache (bool, 可选) — 如果设置为 Truepast_key_values 键值状态将被返回,并可用于加速解码(参见 past_key_values)。
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个 ModelOutput 而不是一个普通的元组。

返回

transformers.models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqModelOutputtuple(torch.FloatTensor)

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

  • last_hidden_state (torch.FloatTensor 形状为 (batch_size, decoder_sequence_length, hidden_size)) — 模型解码器最后一层输出的主流隐藏状态序列。

    如果使用了 past_key_values,则只输出形状为 (batch_size, 1, hidden_size) 的序列的最后一个隐藏状态。

  • last_hidden_state_ngram (torch.FloatTensor 形状为 (batch_size,ngram * decoder_sequence_length, config.vocab_size), 可选) — 模型解码器最后一层输出的预测流隐藏状态序列。

  • past_key_values (List[torch.FloatTensor], 可选, 当传递了 use_cache=True 或当 config.use_cache=True 时返回) — 长度为 config.n_layerstorch.FloatTensor 列表,每个张量的形状为 (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head))。

    包含解码器的预计算隐藏状态(注意力块中的键和值),可用于(参见 past_key_values 输入)加速顺序解码。

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

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

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

    解码器预测流在每层输出处的隐藏状态加上初始嵌入输出。

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

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

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

    解码器预测流的注意力权重,经过注意力 softmax 后,用于计算加权平均值。

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

    解码器交叉注意力层的注意力权重,经过注意力 softmax 后,用于计算加权平均值。

  • encoder_last_hidden_state (torch.FloatTensor 形状为 (batch_size, encoder_sequence_length, hidden_size), 可选) — 模型编码器最后一层输出的隐藏状态序列。

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

    编码器在每层输出处的隐藏状态加上初始嵌入输出。

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

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

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

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

示例:

>>> from transformers import AutoTokenizer, ProphetNetModel

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/prophetnet-large-uncased")
>>> model = ProphetNetModel.from_pretrained("microsoft/prophetnet-large-uncased")

>>> input_ids = tokenizer(
...     "Studies have been shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids  # Batch size 1
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids  # Batch size 1
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)

>>> last_hidden_states = outputs.last_hidden_state  # main stream hidden states
>>> last_hidden_states_ngram = outputs.last_hidden_state_ngram  # predict hidden states

ProphetNetEncoder

transformers.ProphetNetEncoder

< >

( config: ProphetNetConfig word_embeddings: Embedding = None )

参数

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

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

原始的ProphetNet代码可以在这里找到。检查点是从原始的Fairseq检查点转换而来。有关检查点转换的更多信息,请查看文件convert_prophetnet_original_pytorch_checkpoint_to_pytorch.py

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

word_embeddings(形状为(config.vocab_size, config.hidden_size)torch.nn.Embeddings可选): 词嵌入参数。这可以用于使用预定义的词嵌入来初始化ProphetNetEncoder,而不是随机初始化的词嵌入。

前进

< >

( input_ids: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None head_mask: typing.Optional[torch.Tensor] = None inputs_embeds: typing.Optional[torch.Tensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.modeling_outputs.BaseModelOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.

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

    什么是输入ID?

  • attention_mask (torch.Tensor 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.

    什么是注意力掩码?

  • head_mask (torch.Tensor of shape (encoder_layers, encoder_attention_heads), optional) — 用于在编码器中屏蔽注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。

返回

transformers.modeling_outputs.BaseModelOutputtuple(torch.FloatTensor)

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

  • last_hidden_state (torch.FloatTensor 形状为 (batch_size, sequence_length, hidden_size)) — 模型最后一层输出的隐藏状态序列。

  • hidden_states (tuple(torch.FloatTensor), 可选, 当传递了 output_hidden_states=True 或当 config.output_hidden_states=True 时返回) — 由 torch.FloatTensor 组成的元组(一个用于嵌入层的输出,如果模型有嵌入层,+ 一个用于每一层的输出)形状为 (batch_size, sequence_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 后的注意力权重,用于计算自注意力头中的加权平均值。

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

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

示例:

>>> from transformers import AutoTokenizer, ProphetNetEncoder
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/prophetnet-large-uncased")
>>> model = ProphetNetEncoder.from_pretrained("patrickvonplaten/prophetnet-large-uncased-standalone")
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> last_hidden_states = outputs.last_hidden_state

ProphetNetDecoder

transformers.ProphetNetDecoder

< >

( config: ProphetNetConfig word_embeddings: typing.Optional[torch.nn.modules.sparse.Embedding] = None )

参数

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

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

原始的ProphetNet代码可以在这里找到。检查点是从原始的Fairseq检查点转换而来。有关检查点转换的更多信息,请查看文件convert_prophetnet_original_pytorch_checkpoint_to_pytorch.py

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

word_embeddings(形状为(config.vocab_size, config.hidden_size)torch.nn.Embeddings可选): 词嵌入参数。这可以用于使用预定义的词嵌入来初始化ProphetNetEncoder,而不是随机初始化的词嵌入。

前进

< >

( input_ids: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None encoder_hidden_states: typing.Optional[torch.Tensor] = None encoder_attention_mask: typing.Optional[torch.Tensor] = None head_mask: typing.Optional[torch.Tensor] = None cross_attn_head_mask: typing.Optional[torch.Tensor] = None past_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.Tensor]]] = None inputs_embeds: typing.Optional[torch.Tensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.prophetnet.modeling_prophetnet.ProphetNetDecoderModelOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.

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

    什么是输入ID?

  • attention_mask (torch.Tensor 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.

    什么是注意力掩码?

  • head_mask (torch.Tensor of shape (encoder_layers, encoder_attention_heads), optional) — 用于在编码器中屏蔽注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, optional) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • encoder_hidden_states (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 编码器最后一层输出的隐藏状态序列。如果模型配置为解码器,则在交叉注意力中使用。
  • encoder_attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — 用于避免在编码器输入的填充标记索引上执行注意力机制的掩码。如果模型配置为解码器,则在交叉注意力中使用此掩码。掩码值在 [0, 1] 中选择:
  • cross_attn_head_mask (torch.Tensor of shape (decoder_layers, decoder_attention_heads), optional) — 用于屏蔽交叉注意力模块中选定头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • past_key_values (tuple(tuple(torch.FloatTensor)) of length config.n_layers with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)) — Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding.

    如果使用了past_key_values,用户可以选择只输入形状为(batch_size, 1)的最后一个decoder_input_ids(那些没有将其过去键值状态提供给此模型的),而不是形状为(batch_size, sequence_length)的所有decoder_input_ids

  • use_cache (bool, 可选) — 如果设置为 Truepast_key_values 键值状态将被返回,并可用于加速解码(参见 past_key_values)。
    • 1 表示 未屏蔽 的标记,
    • 0 表示 屏蔽 的标记。

返回

transformers.models.prophetnet.modeling_prophetnet.ProphetNetDecoderModelOutputtuple(torch.FloatTensor)

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

  • last_hidden_state (torch.FloatTensor 形状为 (batch_size, decoder_sequence_length, hidden_size)) — 模型解码器最后一层输出的主流隐藏状态序列。

    如果使用了 past_key_values,则只输出形状为 (batch_size, 1, hidden_size) 的序列的最后一个隐藏状态。

  • last_hidden_state_ngram (torch.FloatTensor 形状为 (batch_size, ngram * decoder_sequence_length, config.vocab_size)) — 模型解码器最后一层输出的预测流隐藏状态序列。

  • past_key_values (List[torch.FloatTensor], 可选, 当传递了 use_cache=True 或当 config.use_cache=True 时返回) — 长度为 config.n_layerstorch.FloatTensor 列表,每个张量的形状为 (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head))。

    包含解码器的预计算隐藏状态(注意力块中的键和值),可以用于(参见 past_key_values 输入)加速顺序解码。

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

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

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

    解码器预测流在每层输出处的隐藏状态加上初始嵌入输出。

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

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

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

    解码器预测流的注意力权重,经过注意力 softmax 后,用于计算加权平均值。

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

    解码器交叉注意力层的注意力权重,经过注意力 softmax 后,用于计算加权平均值。

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

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

示例:

>>> from transformers import AutoTokenizer, ProphetNetDecoder
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/prophetnet-large-uncased")
>>> model = ProphetNetDecoder.from_pretrained("microsoft/prophetnet-large-uncased", add_cross_attention=False)
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> last_hidden_states = outputs.last_hidden_state

ProphetNetForConditionalGeneration

transformers.ProphetNetForConditionalGeneration

< >

( config: ProphetNetConfig )

参数

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

带有语言建模头的ProphetNet模型。可用于序列生成任务。 该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入大小、修剪头等)。

原始的ProphetNet代码可以在这里找到。检查点是从原始的Fairseq检查点转换而来。有关检查点转换的更多信息,请查看文件convert_prophetnet_original_pytorch_checkpoint_to_pytorch.py

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

前进

< >

( input_ids: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None decoder_input_ids: typing.Optional[torch.Tensor] = None decoder_attention_mask: typing.Optional[torch.BoolTensor] = None head_mask: typing.Optional[torch.Tensor] = None decoder_head_mask: typing.Optional[torch.Tensor] = None cross_attn_head_mask: typing.Optional[torch.Tensor] = None encoder_outputs: typing.Optional[torch.Tensor] = None past_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.Tensor]]] = None inputs_embeds: typing.Optional[torch.Tensor] = None decoder_inputs_embeds: typing.Optional[torch.Tensor] = None labels: typing.Optional[torch.Tensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqLMOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.

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

    什么是输入ID?

  • attention_mask (torch.Tensor 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.

    什么是注意力掩码?

  • decoder_input_ids (torch.LongTensor of shape (batch_size, target_sequence_length), optional) — Indices of decoder input sequence tokens in the vocabulary.

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

    什么是解码器输入ID?

    ProphetNet 使用 eos_token_id 作为 decoder_input_ids 生成的起始标记。如果使用了 past_key_values,则可以选择只输入最后一个 decoder_input_ids(参见 past_key_values)。

  • decoder_attention_mask (torch.BoolTensor of shape (batch_size, target_sequence_length), optional) — 默认行为:生成一个忽略decoder_input_ids中填充标记的张量。默认情况下也会使用因果掩码。
  • head_mask (torch.Tensor 形状为 (encoder_layers, encoder_attention_heads), 可选) — 用于在编码器中屏蔽注意力模块中选定的头。屏蔽值在 [0, 1] 中选择:
    • 1 表示头 未被屏蔽,
    • 0 表示头 被屏蔽
  • decoder_head_mask (torch.Tensor 形状为 (decoder_layers, decoder_attention_heads), 可选) — 用于在解码器中取消选择注意力模块的特定头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被掩码,
    • 0 表示头部 被掩码.
  • cross_attn_head_mask (torch.Tensor 形状为 (decoder_layers, decoder_attention_heads), 可选) — 用于屏蔽交叉注意力模块中选定头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • encoder_outputs (tuple(tuple(torch.FloatTensor), 可选的) — 元组由 (last_hidden_state, 可选的: hidden_states, 可选的: attentions) last_hidden_state 的形状为 (batch_size, sequence_length, hidden_size), 可选的) 是 编码器最后一层输出的隐藏状态序列。用于解码器的交叉注意力中。
  • past_key_values (tuple(tuple(torch.FloatTensor)) of length config.n_layers with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)) — Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding.

    如果使用了past_key_values,用户可以选择只输入形状为(batch_size, 1)的最后一个decoder_input_ids(那些没有将其过去键值状态提供给此模型的),而不是形状为(batch_size, sequence_length)的所有decoder_input_ids

  • use_cache (bool, 可选) — 如果设置为 Truepast_key_values 键值状态将被返回,并可用于加速解码(参见 past_key_values)。
  • output_attentions (bool, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, 可选) — 是否返回一个ModelOutput而不是一个普通的元组。
  • labels (torch.LongTensor of shape (batch_size,), optional) — 用于计算序列分类/回归损失的标签。索引应在 [-100, 0, ..., config.vocab_size - 1] 范围内。所有设置为 -100 的标签将被忽略(掩码),损失仅计算在 [0, ..., config.vocab_size] 范围内的标签

返回

transformers.models.prophetnet.modeling_prophetnet.ProphetNetSeq2SeqLMOutputtuple(torch.FloatTensor)

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

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

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

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

  • past_key_values (List[torch.FloatTensor]可选,当传递 use_cache=True 或当 config.use_cache=True 时返回) — 长度为 config.n_layerstorch.FloatTensor 列表,每个张量的形状为 (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)

    包含解码器的预计算隐藏状态(注意力块中的键和值),可用于(参见 past_key_values 输入)加速顺序解码。

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

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

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

    解码器预测流在每层输出处的隐藏状态加上初始嵌入输出。

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

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

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

    解码器预测流的注意力权重,经过注意力 softmax 后,用于计算自注意力头中的加权平均值。

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

    解码器交叉注意力层的注意力权重,经过注意力 softmax 后,用于计算加权平均值。

  • encoder_last_hidden_state (torch.FloatTensor 形状为 (batch_size, encoder_sequence_length, hidden_size)可选) — 模型编码器最后一层输出处的隐藏状态序列。

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

    编码器在每层输出处的隐藏状态加上初始嵌入输出。

  • encoder_attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — 由 torch.FloatTensor 组成的元组(每层一个),形状为 (batch_size, num_attn_heads, encoder_sequence_length, encoder_sequence_length)。编码器的注意力权重,经过注意力 softmax 后,用于计算自注意力头中的加权平均值。

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

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

示例:

>>> from transformers import AutoTokenizer, ProphetNetForConditionalGeneration

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/prophetnet-large-uncased")
>>> model = ProphetNetForConditionalGeneration.from_pretrained("microsoft/prophetnet-large-uncased")

>>> input_ids = tokenizer(
...     "Studies have been shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids  # Batch size 1
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids  # Batch size 1
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)

>>> logits_next_token = outputs.logits  # logits to predict next token as usual
>>> logits_ngram_next_tokens = outputs.logits_ngram  # logits to predict 2nd, 3rd, ... next tokens

ProphetNetForCausalLM

transformers.ProphetNetForCausalLM

< >

( config: ProphetNetConfig )

参数

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

ProphetNetModel 的独立解码器部分,顶部有一个语言模型头。该模型可用于因果语言建模。 此模型继承自 PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法 (如下载或保存、调整输入嵌入的大小、修剪头部等)。

原始的ProphetNet代码可以在这里找到。检查点是从原始的Fairseq检查点转换而来。有关检查点转换的更多信息,请查看文件convert_prophetnet_original_pytorch_checkpoint_to_pytorch.py

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

前进

< >

( input_ids: typing.Optional[torch.Tensor] = None attention_mask: typing.Optional[torch.Tensor] = None encoder_hidden_states: typing.Optional[torch.Tensor] = None encoder_attention_mask: typing.Optional[torch.Tensor] = None head_mask: typing.Optional[torch.Tensor] = None cross_attn_head_mask: typing.Optional[torch.Tensor] = None past_key_values: typing.Optional[typing.Tuple[typing.Tuple[torch.Tensor]]] = None inputs_embeds: typing.Optional[torch.Tensor] = None labels: typing.Optional[torch.Tensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None ) transformers.models.prophetnet.modeling_prophetnet.ProphetNetDecoderLMOutputtuple(torch.FloatTensor)

参数

  • input_ids (torch.LongTensor of shape (batch_size, sequence_length)) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.

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

    什么是输入ID?

  • attention_mask (torch.Tensor 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.

    什么是注意力掩码?

  • head_mask (torch.Tensor of shape (encoder_layers, encoder_attention_heads), optional) — 用于在编码器中屏蔽注意力模块中选定的头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • output_attentions (bool, optional) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量中的attentions
  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
  • return_dict (bool, optional) — 是否返回一个ModelOutput而不是一个普通的元组。
  • encoder_hidden_states (torch.FloatTensor of shape (batch_size, sequence_length, hidden_size), optional) — 编码器最后一层输出的隐藏状态序列。如果模型配置为解码器,则在交叉注意力中使用。
  • encoder_attention_mask (torch.FloatTensor of shape (batch_size, sequence_length), optional) — 用于避免在编码器输入的填充标记索引上执行注意力。如果模型配置为解码器,则在交叉注意力中使用此掩码。掩码值在 [0, 1] 中选择:
  • cross_attn_head_mask (torch.Tensor of shape (decoder_layers, decoder_attention_heads), optional) — 用于屏蔽交叉注意力模块中选定头部的掩码。掩码值在 [0, 1] 中选择:
    • 1 表示头部 未被屏蔽,
    • 0 表示头部 被屏蔽.
  • past_key_values (tuple(tuple(torch.FloatTensor)) of length config.n_layers with each tuple having 4 tensors of shape (batch_size, num_heads, sequence_length - 1, embed_size_per_head)) — Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding.

    如果使用了past_key_values,用户可以选择只输入形状为(batch_size, 1)的最后一个decoder_input_ids(那些没有将其过去键值状态提供给此模型的),而不是形状为(batch_size, sequence_length)的所有decoder_input_ids

  • use_cache (bool, 可选) — 如果设置为 Truepast_key_values 键值状态将被返回,并可用于加速解码(参见 past_key_values)。
    • 1 表示 未屏蔽 的标记,
    • 0 表示 屏蔽 的标记。
  • labels (torch.LongTensor of shape (batch_size, sequence_length), optional) — 用于计算从左到右语言建模损失(下一个词预测)的标签。索引应在 [-100, 0, ..., config.vocab_size] 范围内(参见 input_ids 文档字符串)。索引设置为 -100 的 标记将被忽略(掩码),损失仅针对标签在 [0, ..., config.vocab_size] 范围内的标记计算

返回

transformers.models.prophetnet.modeling_prophetnet.ProphetNetDecoderLMOutputtuple(torch.FloatTensor)

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

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

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

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

  • past_key_values (List[torch.FloatTensor]可选,当传递 use_cache=True 或当 config.use_cache=True 时返回) — 长度为 config.n_layerstorch.FloatTensor 列表,每个张量的形状为 (2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)

    包含解码器的预计算隐藏状态(注意力块中的键和值),可用于(参见 past_key_values 输入)加速顺序解码。

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

    解码器主流在每层输出处的隐藏状态加上初始嵌入输出。

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

    解码器预测流在每层输出处的隐藏状态加上初始嵌入输出。

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

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

  • ngram_attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — torch.FloatTensor 元组(每层一个),形状为 (batch_size, num_attn_heads, decoder_sequence_length, decoder_sequence_length)

    解码器预测流的注意力权重,在注意力 softmax 之后,用于计算加权平均值。

  • cross_attentions (tuple(torch.FloatTensor)可选,当传递 output_attentions=True 或当 config.output_attentions=True 时返回) — torch.FloatTensor 元组(每层一个),形状为 (batch_size, num_attn_heads, encoder_sequence_length, decoder_sequence_length)

    解码器交叉注意力层的注意力权重,在注意力 softmax 之后,用于计算加权平均值。

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

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

示例:

>>> from transformers import AutoTokenizer, ProphetNetForCausalLM
>>> import torch

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/prophetnet-large-uncased")
>>> model = ProphetNetForCausalLM.from_pretrained("microsoft/prophetnet-large-uncased")
>>> assert model.config.is_decoder, f"{model.__class__} has to be configured as a decoder."
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> logits = outputs.logits

>>> # Model can also be used with EncoderDecoder framework
>>> from transformers import BertTokenizer, EncoderDecoderModel, AutoTokenizer
>>> import torch

>>> tokenizer_enc = BertTokenizer.from_pretrained("google-bert/bert-large-uncased")
>>> tokenizer_dec = AutoTokenizer.from_pretrained("microsoft/prophetnet-large-uncased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
...     "google-bert/bert-large-uncased", "microsoft/prophetnet-large-uncased"
... )

>>> ARTICLE = (
...     "the us state department said wednesday it had received no "
...     "formal word from bolivia that it was expelling the us ambassador there "
...     "but said the charges made against him are `` baseless ."
... )
>>> input_ids = tokenizer_enc(ARTICLE, return_tensors="pt").input_ids
>>> labels = tokenizer_dec(
...     "us rejects charges against its ambassador in bolivia", return_tensors="pt"
... ).input_ids
>>> outputs = model(input_ids=input_ids, decoder_input_ids=labels[:, :-1], labels=labels[:, 1:])

>>> loss = outputs.loss
< > Update on GitHub