编码器解码器模型
概述
EncoderDecoderModel 可以用来初始化一个序列到序列的模型,其中编码器可以是任何预训练的自编码模型,解码器可以是任何预训练的自回归模型。
使用预训练检查点初始化序列到序列模型在序列生成任务中的有效性在Leveraging Pre-trained Checkpoints for Sequence Generation Tasks中由Sascha Rothe, Shashi Narayan, Aliaksei Severyn展示。
在训练/微调了这样的EncoderDecoderModel之后,它可以像其他模型一样保存/加载(更多信息请参见示例)。
这种架构的一个应用可能是利用两个预训练的BertModel作为编码器和解码器,用于摘要模型,如Yang Liu和Mirella Lapata在Text Summarization with Pretrained Encoders中所展示的。
从模型配置中随机初始化EncoderDecoderModel。
EncoderDecoderModel 可以从编码器和解码器配置中随机初始化。在下面的示例中,我们展示了如何使用默认的 BertModel 配置作为编码器,以及默认的 BertForCausalLM
配置作为解码器来实现这一点。
>>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel
>>> config_encoder = BertConfig()
>>> config_decoder = BertConfig()
>>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)
>>> model = EncoderDecoderModel(config=config)
从预训练的编码器和预训练的解码器初始化EncoderDecoderModel。
EncoderDecoderModel 可以从预训练的编码器检查点和预训练的解码器检查点初始化。请注意,任何预训练的自动编码模型,例如 BERT,都可以作为编码器,而预训练的自动编码模型,例如 BERT,预训练的因果语言模型,例如 GPT2,以及序列到序列模型的预训练解码器部分,例如 BART 的解码器,都可以用作解码器。
根据您选择作为解码器的架构,交叉注意力层可能会被随机初始化。
从预训练的编码器和解码器检查点初始化 EncoderDecoderModel 需要在下游任务上对模型进行微调,如 《Warm-starting-encoder-decoder 博客文章》 中所示。
为此,EncoderDecoderModel
类提供了 EncoderDecoderModel.from_encoder_decoder_pretrained() 方法。
>>> from transformers import EncoderDecoderModel, BertTokenizer
>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased")
加载现有的EncoderDecoderModel检查点并执行推理。
要加载EncoderDecoderModel
类的微调检查点,EncoderDecoderModel提供了from_pretrained(...)
方法,就像Transformers中的任何其他模型架构一样。
要进行推理,可以使用generate
方法,该方法允许自回归生成文本。此方法支持各种形式的解码,例如贪婪、束搜索和多采样。
>>> from transformers import AutoTokenizer, EncoderDecoderModel
>>> # load a fine-tuned seq2seq model and corresponding tokenizer
>>> model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail")
>>> tokenizer = AutoTokenizer.from_pretrained("patrickvonplaten/bert2bert_cnn_daily_mail")
>>> # let's perform inference on a long piece of text
>>> ARTICLE_TO_SUMMARIZE = (
... "PG&E stated it scheduled the blackouts in response to forecasts for high winds "
... "amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were "
... "scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow."
... )
>>> input_ids = tokenizer(ARTICLE_TO_SUMMARIZE, return_tensors="pt").input_ids
>>> # autoregressively generate summary (uses greedy decoding by default)
>>> generated_ids = model.generate(input_ids)
>>> generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
>>> print(generated_text)
nearly 800 thousand customers were affected by the shutoffs. the aim is to reduce the risk of wildfires. nearly 800, 000 customers were expected to be affected by high winds amid dry conditions. pg & e said it scheduled the blackouts to last through at least midday tomorrow.
将 PyTorch 检查点加载到 TFEncoderDecoderModel 中。
TFEncoderDecoderModel.from_pretrained() 目前不支持从 PyTorch 检查点初始化模型。向此方法传递 from_pt=True
将会抛出异常。如果某个编码器-解码器模型只有 PyTorch 检查点,可以采取以下解决方法:
>>> # a workaround to load from pytorch checkpoint
>>> from transformers import EncoderDecoderModel, TFEncoderDecoderModel
>>> _model = EncoderDecoderModel.from_pretrained("patrickvonplaten/bert2bert-cnn_dailymail-fp16")
>>> _model.encoder.save_pretrained("./encoder")
>>> _model.decoder.save_pretrained("./decoder")
>>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained(
... "./encoder", "./decoder", encoder_from_pt=True, decoder_from_pt=True
... )
>>> # This is only for copying some specific attributes of this particular model.
>>> model.config = _model.config
训练
一旦模型创建完成,它可以像BART、T5或任何其他编码器-解码器模型一样进行微调。
如你所见,模型只需要2个输入来计算损失:input_ids
(这是编码输入序列的input_ids
)和labels
(这是编码目标序列的input_ids
)。
>>> from transformers import BertTokenizer, EncoderDecoderModel
>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased")
>>> model.config.decoder_start_token_id = tokenizer.cls_token_id
>>> model.config.pad_token_id = tokenizer.pad_token_id
>>> input_ids = tokenizer(
... "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side.During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft).Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
... return_tensors="pt",
... ).input_ids
>>> labels = tokenizer(
... "the eiffel tower surpassed the washington monument to become the tallest structure in the world. it was the first structure to reach a height of 300 metres in paris in 1930. it is now taller than the chrysler building by 5. 2 metres ( 17 ft ) and is the second tallest free - standing structure in paris.",
... return_tensors="pt",
... ).input_ids
>>> # the forward function automatically creates the correct decoder_input_ids
>>> loss = model(input_ids=input_ids, labels=labels).loss
详细的colab用于训练。
该模型由thomwolf贡献。该模型的TensorFlow和Flax版本由ydshieh贡献。
EncoderDecoderConfig
类 transformers.EncoderDecoderConfig
< source >( **kwargs )
参数
- kwargs (可选) —
关键字参数字典。特别是:
- encoder (PretrainedConfig, 可选) — 定义编码器配置的配置对象实例。
- decoder (PretrainedConfig, 可选) — 定义解码器配置的配置对象实例。
EncoderDecoderConfig 是用于存储 EncoderDecoderModel 配置的配置类。它用于根据指定的参数实例化一个编码器-解码器模型,定义编码器和解码器的配置。
配置对象继承自PretrainedConfig,可用于控制模型输出。阅读PretrainedConfig的文档以获取更多信息。
示例:
>>> from transformers import BertConfig, EncoderDecoderConfig, EncoderDecoderModel
>>> # Initializing a BERT google-bert/bert-base-uncased style configuration
>>> config_encoder = BertConfig()
>>> config_decoder = BertConfig()
>>> config = EncoderDecoderConfig.from_encoder_decoder_configs(config_encoder, config_decoder)
>>> # Initializing a Bert2Bert model (with random weights) from the google-bert/bert-base-uncased style configurations
>>> model = EncoderDecoderModel(config=config)
>>> # Accessing the model configuration
>>> config_encoder = model.config.encoder
>>> config_decoder = model.config.decoder
>>> # set decoder config to causal lm
>>> config_decoder.is_decoder = True
>>> config_decoder.add_cross_attention = True
>>> # Saving the model, including its configuration
>>> model.save_pretrained("my-model")
>>> # loading model and config from pretrained folder
>>> encoder_decoder_config = EncoderDecoderConfig.from_pretrained("my-model")
>>> model = EncoderDecoderModel.from_pretrained("my-model", config=encoder_decoder_config)
from_encoder_decoder_configs
< source >( encoder_config: PretrainedConfig decoder_config: PretrainedConfig **kwargs ) → EncoderDecoderConfig
从预训练的编码器模型配置和解码器模型配置实例化一个EncoderDecoderConfig(或派生类)。
EncoderDecoderModel
类 transformers.EncoderDecoderModel
< source >( config: typing.Optional[transformers.configuration_utils.PretrainedConfig] = None encoder: typing.Optional[transformers.modeling_utils.PreTrainedModel] = None decoder: typing.Optional[transformers.modeling_utils.PreTrainedModel] = None )
参数
- config (EncoderDecoderConfig) — 包含模型所有参数的模型配置类。 使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。
此类可用于初始化一个序列到序列模型,其中编码器可以是任何预训练的自动编码模型,解码器可以是任何预训练的自回归模型。编码器通过from_pretrained()函数加载,解码器也通过from_pretrained()函数加载。交叉注意力层会自动添加到解码器中,并且应该在生成任务(如摘要生成)上进行微调。
使用预训练检查点初始化序列到序列模型在序列生成任务中的有效性在Leveraging Pre-trained Checkpoints for Sequence Generation Tasks中由Sascha Rothe、Shashi Narayan、Aliaksei Severyn、Michael Matena、Yanqi Zhou、Wei Li和Peter J. Liu展示。
在训练/微调这样的编码器解码器模型之后,它可以像任何其他模型一样保存/加载(更多信息请参见示例)。
该模型继承自PreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。
该模型也是一个PyTorch torch.nn.Module 子类。 将其作为常规的PyTorch模块使用,并参考PyTorch文档以获取与一般使用和行为相关的所有信息。
EncoderDecoderModel 是一个通用模型类,当使用 :meth~transformers.AutoModel.from_pretrained 类方法创建编码器和使用 :meth~transformers.AutoModelForCausalLM.from_pretrained 类方法创建解码器时,它将作为一个具有库中基础模型类之一的编码器和另一个作为解码器的变压器架构实例化。
前进
< source >( input_ids: typing.Optional[torch.LongTensor] = None attention_mask: typing.Optional[torch.FloatTensor] = None decoder_input_ids: typing.Optional[torch.LongTensor] = None decoder_attention_mask: typing.Optional[torch.BoolTensor] = None encoder_outputs: typing.Optional[typing.Tuple[torch.FloatTensor]] = None past_key_values: typing.Tuple[typing.Tuple[torch.FloatTensor]] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None decoder_inputs_embeds: typing.Optional[torch.FloatTensor] = None labels: typing.Optional[torch.LongTensor] = 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 **kwargs ) → transformers.modeling_outputs.Seq2SeqLMOutput 或 tuple(torch.FloatTensor)
参数
- input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary.可以使用PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
- 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.
- decoder_input_ids (
torch.LongTensor
of shape(batch_size, target_sequence_length)
, optional) — Indices of decoder input sequence tokens in the vocabulary.可以使用PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
如果使用了
past_key_values
,则可以选择性地仅输入最后一个decoder_input_ids
(参见past_key_values
)。在训练过程中,
decoder_input_ids
由模型自动生成,通过将labels
向右移动,用pad_token_id
替换 -100,并在前面加上decoder_start_token_id
。 - decoder_attention_mask (
torch.BoolTensor
of shape(batch_size, target_sequence_length)
, 可选) — 默认行为:生成一个忽略decoder_input_ids
中填充标记的张量。默认情况下也会使用因果掩码。 - encoder_outputs (
tuple(torch.FloatTensor)
, optional) — 这个元组必须包含 (last_hidden_state
, optional:hidden_states
, optional:attentions
)last_hidden_state
(torch.FloatTensor
形状为(batch_size, sequence_length, hidden_size)
) 是一个张量, 表示编码器最后一层的隐藏状态输出。用于解码器的交叉注意力机制中。 - past_key_values (
tuple(tuple(torch.FloatTensor))
of lengthconfig.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
。 - inputs_embeds (
torch.FloatTensor
of shape(batch_size, sequence_length, hidden_size)
, optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids
。如果您希望对如何将input_ids
索引转换为相关向量有更多控制权,而不是使用模型的内部嵌入查找矩阵,这将非常有用。 - decoder_inputs_embeds (
torch.FloatTensor
of shape(batch_size, target_sequence_length, hidden_size)
, optional) — 可选地,您可以选择直接传递嵌入表示,而不是传递decoder_input_ids
。如果您希望对如何将decoder_input_ids
索引转换为相关向量有更多控制权,而不是使用模型的内部嵌入查找矩阵,这将非常有用。 - labels (
torch.LongTensor
of shape(batch_size, sequence_length)
, optional) — 用于计算解码器的掩码语言建模损失的标签。索引应在[-100, 0, ..., config.vocab_size]
范围内(参见input_ids
文档字符串)。索引设置为-100
的标记将被忽略 (掩码),损失仅计算标签在[0, ..., config.vocab_size]
范围内的标记 - use_cache (
bool
, 可选) — 如果设置为True
,past_key_values
键值状态将被返回,并可用于加速解码(参见past_key_values
)。 - output_attentions (
bool
, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量中的attentions
。 - output_hidden_states (
bool
, 可选) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
。 - return_dict (
bool
, 可选) — 如果设置为True
,模型将返回一个~utils.Seq2SeqLMOutput
而不是一个普通的元组。 - kwargs (可选) — 剩余的关键字参数字典。关键字参数有两种形式:
- 没有前缀的将作为编码器前向函数的
**encoder_kwargs
输入。 - 带有 decoder_ 前缀的将作为解码器前向函数的
**decoder_kwargs
输入。
- 没有前缀的将作为编码器前向函数的
返回
transformers.modeling_outputs.Seq2SeqLMOutput 或 tuple(torch.FloatTensor)
一个 transformers.modeling_outputs.Seq2SeqLMOutput 或一个由
torch.FloatTensor
组成的元组(如果传递了 return_dict=False
或当 config.return_dict=False
时),包含各种
元素,具体取决于配置(EncoderDecoderConfig)和输入。
-
loss (
torch.FloatTensor
形状为(1,)
, 可选, 当提供labels
时返回) — 语言建模损失。 -
logits (
torch.FloatTensor
形状为(batch_size, sequence_length, config.vocab_size)
) — 语言建模头的预测分数(SoftMax 之前的每个词汇标记的分数)。 -
past_key_values (
tuple(tuple(torch.FloatTensor))
, 可选, 当传递use_cache=True
或当config.use_cache=True
时返回) — 长度为config.n_layers
的tuple(torch.FloatTensor)
元组,每个元组包含 2 个形状为(batch_size, num_heads, sequence_length, embed_size_per_head)
的张量和 2 个形状为(batch_size, num_heads, encoder_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, sequence_length, hidden_size)
。解码器在每层输出处的隐藏状态加上初始嵌入输出。
-
decoder_attentions (
tuple(torch.FloatTensor)
, 可选, 当传递output_attentions=True
或当config.output_attentions=True
时返回) — 由torch.FloatTensor
组成的元组(每层一个)形状为(batch_size, num_heads, sequence_length, sequence_length)
。解码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。
-
cross_attentions (
tuple(torch.FloatTensor)
, 可选, 当传递output_attentions=True
或当config.output_attentions=True
时返回) — 由torch.FloatTensor
组成的元组(每层一个)形状为(batch_size, num_heads, sequence_length, sequence_length)
。解码器的交叉注意力层的注意力权重,在注意力 softmax 之后,用于计算交叉注意力头中的加权平均值。
-
encoder_last_hidden_state (
torch.FloatTensor
形状为(batch_size, sequence_length, hidden_size)
, 可选) — 模型编码器最后一层输出的隐藏状态序列。 -
encoder_hidden_states (
tuple(torch.FloatTensor)
, 可选, 当传递output_hidden_states=True
或当config.output_hidden_states=True
时返回) — 由torch.FloatTensor
组成的元组(一个用于嵌入层的输出,如果模型有嵌入层,+ 一个用于每层的输出)形状为(batch_size, sequence_length, hidden_size)
。编码器在每层输出处的隐藏状态加上初始嵌入输出。
-
encoder_attentions (
tuple(torch.FloatTensor)
, 可选, 当传递output_attentions=True
或当config.output_attentions=True
时返回) — 由torch.FloatTensor
组成的元组(每层一个)形状为(batch_size, num_heads, sequence_length, sequence_length)
。编码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。
EncoderDecoderModel 的前向方法,重写了 __call__
特殊方法。
尽管前向传递的配方需要在此函数内定义,但之后应该调用Module
实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。
示例:
>>> from transformers import EncoderDecoderModel, BertTokenizer
>>> import torch
>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-uncased")
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained(
... "google-bert/bert-base-uncased", "google-bert/bert-base-uncased"
... ) # initialize Bert2Bert from pre-trained checkpoints
>>> # training
>>> model.config.decoder_start_token_id = tokenizer.cls_token_id
>>> model.config.pad_token_id = tokenizer.pad_token_id
>>> model.config.vocab_size = model.config.decoder.vocab_size
>>> input_ids = tokenizer("This is a really long text", return_tensors="pt").input_ids
>>> labels = tokenizer("This is the corresponding summary", return_tensors="pt").input_ids
>>> outputs = model(input_ids=input_ids, labels=labels)
>>> loss, logits = outputs.loss, outputs.logits
>>> # save and load from pretrained
>>> model.save_pretrained("bert2bert")
>>> model = EncoderDecoderModel.from_pretrained("bert2bert")
>>> # generation
>>> generated = model.generate(input_ids)
from_encoder_decoder_pretrained
< source >( encoder_pretrained_model_name_or_path: str = None decoder_pretrained_model_name_or_path: str = None *model_args **kwargs )
参数
- encoder_pretrained_model_name_or_path (
str
, optional) — Information necessary to initiate the encoder. Can be either:- A string, the model id of a pretrained model hosted inside a model repo on huggingface.co.
- A path to a directory containing model weights saved using
save_pretrained(), e.g.,
./my_model_directory/
. - A path or url to a tensorflow index checkpoint file (e.g,
./tf_model/model.ckpt.index
). In this case,from_tf
should be set toTrue
and a configuration object should be provided asconfig
argument. This loading path is slower than converting the TensorFlow checkpoint in a PyTorch model using the provided conversion scripts and loading the PyTorch model afterwards.
- decoder_pretrained_model_name_or_path (
str
, optional, defaults toNone
) — Information necessary to initiate the decoder. Can be either:- A string, the model id of a pretrained model hosted inside a model repo on huggingface.co.
- A path to a directory containing model weights saved using
save_pretrained(), e.g.,
./my_model_directory/
. - A path or url to a tensorflow index checkpoint file (e.g,
./tf_model/model.ckpt.index
). In this case,from_tf
should be set toTrue
and a configuration object should be provided asconfig
argument. This loading path is slower than converting the TensorFlow checkpoint in a PyTorch model using the provided conversion scripts and loading the PyTorch model afterwards.
- model_args(剩余的位置参数,可选)—
所有剩余的位置参数将传递给底层模型的
__init__
方法。 - kwargs (remaining dictionary of keyword arguments, optional) —
Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
output_attentions=True
).- To update the encoder configuration, use the prefix encoder_ for each configuration parameter.
- To update the decoder configuration, use the prefix decoder_ for each configuration parameter.
- To update the parent model configuration, do not use a prefix for each configuration parameter.
根据是否提供了
config
或自动加载而表现不同。
从一个或多个库的基类实例化编码器和解码器,使用预训练模型的检查点。
模型默认使用model.eval()
设置为评估模式(Dropout模块被停用)。要训练模型,你需要首先使用model.train()
将其重新设置为训练模式。
示例:
>>> from transformers import EncoderDecoderModel
>>> # initialize a bert2bert from two pretrained BERT models. Note that the cross-attention layers will be randomly initialized
>>> model = EncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "google-bert/bert-base-uncased")
>>> # saving model after fine-tuning
>>> model.save_pretrained("./bert2bert")
>>> # load fine-tuned model
>>> model = EncoderDecoderModel.from_pretrained("./bert2bert")
TFEncoderDecoderModel
类 transformers.TFEncoderDecoderModel
< source >( config: Optional[PretrainedConfig] = None encoder: Optional[TFPreTrainedModel] = None decoder: Optional[TFPreTrainedModel] = None )
参数
- config (EncoderDecoderConfig) — 包含模型所有参数的模型配置类。 使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。
此类可用于初始化一个序列到序列模型,其中编码器可以是任何预训练的自动编码模型,解码器可以是任何预训练的自回归模型。编码器通过from_pretrained()函数加载,解码器也通过from_pretrained()函数加载。交叉注意力层会自动添加到解码器中,并且应该在生成任务(如摘要生成)上进行微调。
使用预训练检查点初始化序列到序列模型在序列生成任务中的有效性在Leveraging Pre-trained Checkpoints for Sequence Generation Tasks中由Sascha Rothe、Shashi Narayan、Aliaksei Severyn、Michael Matena、Yanqi Zhou、Wei Li和Peter J. Liu展示。
在训练/微调这样的编码器解码器模型之后,它可以像任何其他模型一样保存/加载(更多信息请参见示例)。
该模型继承自 TFPreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头部等)。
该模型也是一个keras.Model子类。可以将其作为常规的TF 2.0 Keras模型使用,并参考TF 2.0文档以了解与一般使用和行为相关的所有事项。
TFEncoderDecoderModel 是一个通用模型类,当使用 from_pretrained() 类方法为编码器和 from_pretrained() 类方法为解码器创建时,它将实例化为一个具有库中一个基础模型类作为编码器和另一个作为解码器的变压器架构。
调用
< source >( input_ids: TFModelInputType | None = None attention_mask: np.ndarray | tf.Tensor | None = None decoder_input_ids: np.ndarray | tf.Tensor | None = None decoder_attention_mask: np.ndarray | tf.Tensor | None = None encoder_outputs: np.ndarray | tf.Tensor | None = None past_key_values: Tuple[Tuple[tf.Tensor]] | None = None inputs_embeds: np.ndarray | tf.Tensor | None = None decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None labels: np.ndarray | tf.Tensor | None = None use_cache: Optional[bool] = None output_attentions: Optional[bool] = None output_hidden_states: Optional[bool] = None return_dict: Optional[bool] = None training: bool = False **kwargs ) → transformers.modeling_tf_outputs.TFSeq2SeqLMOutput 或 tuple(tf.Tensor)
参数
- input_ids (
np.ndarray
,tf.Tensor
,List[tf.Tensor]
`Dict[str, tf.Tensor]
orDict[str, np.ndarray]
and each example must have the shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary.可以使用PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
- attention_mask (
np.ndarray
ortf.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 (
np.ndarray
ortf.Tensor
of shape(batch_size, target_sequence_length)
, optional) — Indices of decoder input sequence tokens in the vocabulary.可以使用PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
如果使用了
past_key_values
,则可以选择性地仅输入最后一个decoder_input_ids
(参见past_key_values
)。为解码器提供序列到序列的训练。可以使用 PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和PreTrainedTokenizer.call()。
- decoder_attention_mask (
np.ndarray
或tf.Tensor
,形状为(batch_size, target_sequence_length)
,可选) — 默认行为:生成一个忽略decoder_input_ids
中填充标记的张量。默认情况下也会使用因果掩码。 - encoder_outputs (
tuple(tuple(tf.Tensor)
, 可选) — 这个元组必须包含 (last_hidden_state
, 可选:hidden_states
, 可选:attentions
)last_hidden_state
(tf.Tensor
形状为(batch_size, sequence_length, hidden_size)
) 是编码器最后一层的隐藏状态张量。用于解码器的交叉注意力机制中。 - past_key_values (
tuple(tuple(tf.Tensor))
of lengthconfig.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
。 - inputs_embeds (
np.ndarray
或tf.Tensor
,形状为(batch_size, sequence_length, hidden_size)
,可选) — 可选地,您可以选择直接传递嵌入表示,而不是传递input_ids
。如果您希望对如何将input_ids
索引转换为相关向量有更多控制,而不是使用模型的内部嵌入查找矩阵,这将非常有用。 - decoder_inputs_embeds (
np.ndarray
或tf.Tensor
形状为(batch_size, target_sequence_length, hidden_size)
, 可选) — 可选地,您可以选择直接传递嵌入表示,而不是传递decoder_input_ids
。如果您希望对如何将decoder_input_ids
索引转换为相关向量有更多控制权,而不是使用模型的内部嵌入查找矩阵,这将非常有用。 - labels (
np.ndarray
或tf.Tensor
形状为(batch_size, sequence_length)
, 可选) — 用于计算解码器的掩码语言建模损失的标签。索引应在[-100, 0, ..., config.vocab_size]
范围内(参见input_ids
文档字符串)。索引设置为-100
的标记将被忽略 (掩码),损失仅计算标签在[0, ..., config.vocab_size]
范围内的标记 - use_cache (
bool
, 可选) — 如果设置为True
,past_key_values
键值状态将被返回,并可用于加速解码(参见past_key_values
)。 - output_attentions (
bool
, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量下的attentions
。 - output_hidden_states (
bool
, optional) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
。 - return_dict (
bool
, optional) — 如果设置为True
,模型将返回一个~utils.Seq2SeqLMOutput
而不是一个普通的元组。 - 训练 (
bool
, 可选, 默认为False
) — 是否在训练模式下使用模型(一些模块如dropout模块在训练和评估时具有不同的行为)。 - kwargs (可选) — 剩余的关键字参数字典。关键字参数有两种形式:
- 没有前缀的将作为编码器前向函数的
**encoder_kwargs
输入。 - 带有decoder_前缀的将作为解码器前向函数的`**decoder_kwargs“输入。
- 没有前缀的将作为编码器前向函数的
返回
transformers.modeling_tf_outputs.TFSeq2SeqLMOutput 或 tuple(tf.Tensor)
一个 transformers.modeling_tf_outputs.TFSeq2SeqLMOutput 或一个 tf.Tensor
元组(如果
return_dict=False
被传递或当 config.return_dict=False
时)包含各种元素,具体取决于
配置 (EncoderDecoderConfig) 和输入。
-
loss (
tf.Tensor
形状为(n,)
, 可选, 其中 n 是非掩码标签的数量,当提供labels
时返回) — 语言建模损失。 -
logits (
tf.Tensor
形状为(batch_size, sequence_length, config.vocab_size)
) — 语言建模头的预测分数(SoftMax 之前的每个词汇标记的分数)。 -
past_key_values (
List[tf.Tensor]
, 可选, 当传递use_cache=True
或config.use_cache=True
时返回) — 长度为config.n_layers
的tf.Tensor
列表,每个张量的形状为(2, batch_size, num_heads, sequence_length, embed_size_per_head)
)。包含解码器的预计算隐藏状态(注意力块中的键和值),可以用于(参见
past_key_values
输入)加速顺序解码。 -
decoder_hidden_states (
tuple(tf.Tensor)
, 可选, 当传递output_hidden_states=True
或config.output_hidden_states=True
时返回) —tf.Tensor
元组(一个用于嵌入的输出 + 一个用于每层的输出)形状为(batch_size, sequence_length, hidden_size)
。解码器在每层输出处的隐藏状态加上初始嵌入输出。
-
decoder_attentions (
tuple(tf.Tensor)
, 可选, 当传递output_attentions=True
或config.output_attentions=True
时返回) —tf.Tensor
元组(每层一个)形状为(batch_size, num_heads, sequence_length, sequence_length)
。解码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。
-
cross_attentions (
tuple(tf.Tensor)
, 可选, 当传递output_attentions=True
或config.output_attentions=True
时返回) —tf.Tensor
元组(每层一个)形状为(batch_size, num_heads, sequence_length, sequence_length)
。解码器的交叉注意力层的注意力权重,在注意力 softmax 之后,用于计算交叉注意力头中的加权平均值。
-
encoder_last_hidden_state (
tf.Tensor
形状为(batch_size, sequence_length, hidden_size)
, 可选) — 模型编码器最后一层输出的隐藏状态序列。 -
encoder_hidden_states (
tuple(tf.Tensor)
, 可选, 当传递output_hidden_states=True
或config.output_hidden_states=True
时返回) —tf.Tensor
元组(一个用于嵌入的输出 + 一个用于每层的输出)形状为(batch_size, sequence_length, hidden_size)
。编码器在每层输出处的隐藏状态加上初始嵌入输出。
-
encoder_attentions (
tuple(tf.Tensor)
, 可选, 当传递output_attentions=True
或config.output_attentions=True
时返回) —tf.Tensor
元组(每层一个)形状为(batch_size, num_heads, sequence_length, sequence_length)
。编码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。
TFEncoderDecoderModel 的前向方法,重写了 __call__
特殊方法。
尽管前向传递的配方需要在此函数内定义,但之后应该调用Module
实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。
示例:
>>> from transformers import TFEncoderDecoderModel, BertTokenizer
>>> # initialize a bert2gpt2 from a pretrained BERT and GPT2 models. Note that the cross-attention layers will be randomly initialized
>>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-cased", "openai-community/gpt2")
>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased")
>>> # forward
>>> input_ids = tokenizer.encode(
... "Hello, my dog is cute", add_special_tokens=True, return_tensors="tf"
... ) # Batch size 1
>>> outputs = model(input_ids=input_ids, decoder_input_ids=input_ids)
>>> # training
>>> outputs = model(input_ids=input_ids, decoder_input_ids=input_ids, labels=input_ids)
>>> loss, logits = outputs.loss, outputs.logits
>>> # save and load from pretrained
>>> model.save_pretrained("bert2gpt2")
>>> model = TFEncoderDecoderModel.from_pretrained("bert2gpt2")
>>> # generation
>>> generated = model.generate(input_ids, decoder_start_token_id=model.config.decoder.bos_token_id)
from_encoder_decoder_pretrained
< source >( encoder_pretrained_model_name_or_path: str = None decoder_pretrained_model_name_or_path: str = None *model_args **kwargs )
参数
- encoder_pretrained_model_name_or_path (
str
, optional) — Information necessary to initiate the encoder. Can be either:- A string, the model id of a pretrained model hosted inside a model repo on huggingface.co.
- A path to a directory containing model weights saved using
save_pretrained(), e.g.,
./my_model_directory/
. - A path or url to a pytorch index checkpoint file (e.g,
./pt_model/
). In this case,encoder_from_pt
should be set toTrue
.
- decoder_pretrained_model_name_or_path (
str
, optional, defaults toNone
) — Information necessary to initiate the decoder. Can be either:- A string, the model id of a pretrained model hosted inside a model repo on huggingface.co.
- A path to a directory containing model weights saved using
save_pretrained(), e.g.,
./my_model_directory/
. - A path or url to a pytorch checkpoint file (e.g,
./pt_model/
). In this case,decoder_from_pt
should be set toTrue
.
- model_args (剩余的位置参数, 可选) —
所有剩余的位置参数将传递给底层模型的
__init__
方法. - kwargs (remaining dictionary of keyword arguments, optional) —
Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
output_attentions=True
).- To update the encoder configuration, use the prefix encoder_ for each configuration parameter.
- To update the decoder configuration, use the prefix decoder_ for each configuration parameter.
- To update the parent model configuration, do not use a prefix for each configuration parameter.
根据是否提供了
config
或自动加载而表现不同。
从一个或多个库的基类实例化编码器和解码器,使用预训练模型的检查点。
示例:
>>> from transformers import TFEncoderDecoderModel
>>> # initialize a bert2gpt2 from two pretrained BERT models. Note that the cross-attention layers will be randomly initialized
>>> model = TFEncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-uncased", "openai-community/gpt2")
>>> # saving model after fine-tuning
>>> model.save_pretrained("./bert2gpt2")
>>> # load fine-tuned model
>>> model = TFEncoderDecoderModel.from_pretrained("./bert2gpt2")
FlaxEncoderDecoderModel
类 transformers.FlaxEncoderDecoderModel
< source >( config: EncoderDecoderConfig input_shape: typing.Optional[typing.Tuple] = None seed: int = 0 dtype: dtype =
参数
- config (EncoderDecoderConfig) — 包含模型所有参数的模型配置类。 使用配置文件初始化不会加载与模型相关的权重,只会加载配置。查看 from_pretrained() 方法以加载模型权重。
- dtype (
jax.numpy.dtype
, optional, defaults tojax.numpy.float32
) — The data type of the computation. Can be one ofjax.numpy.float32
,jax.numpy.float16
(on GPUs) andjax.numpy.bfloat16
(on TPUs).这可以用于在GPU或TPU上启用混合精度训练或半精度推理。如果指定,所有计算将使用给定的
dtype
执行。请注意,这仅指定了计算的数据类型,并不影响模型参数的数据类型。
此类可用于初始化一个序列到序列模型,其中编码器可以是任何预训练的自动编码模型,解码器可以是任何预训练的自回归模型。编码器通过from_pretrained()函数加载,解码器也通过from_pretrained()函数加载。交叉注意力层会自动添加到解码器中,并且应该在生成任务(如摘要生成)上进行微调。
使用预训练检查点初始化序列到序列模型在序列生成任务中的有效性在Leveraging Pre-trained Checkpoints for Sequence Generation Tasks中由Sascha Rothe、Shashi Narayan、Aliaksei Severyn、Michael Matena、Yanqi Zhou、Wei Li和Peter J. Liu展示。
在训练/微调这样的编码器解码器模型之后,它可以像任何其他模型一样保存/加载(更多信息请参见示例)。
该模型继承自FlaxPreTrainedModel。请查看超类文档以了解库为其所有模型实现的通用方法(如下载或保存、调整输入嵌入的大小、修剪头等)。
该模型也是一个Flax Linen flax.nn.Module 子类。将其作为常规的Flax模块使用,并参考Flax文档以获取与一般用法和行为相关的所有信息。
FlaxEncoderDecoderModel 是一个通用模型类,当使用 :meth~transformers.FlaxAutoModel.from_pretrained 类方法创建编码器和使用 :meth~transformers.FlaxAutoModelForCausalLM.from_pretrained 类方法创建解码器时,它将作为具有库中一个基础模型类的模块(flax.nn.Module)的编码器模块和另一个作为解码器模块的变压器架构实例化。
__call__
< source >( input_ids: 数组 attention_mask: 可选的[jax.Array] = 无 decoder_input_ids: 可选的[jax.Array] = 无 decoder_attention_mask: 可选的[jax.Array] = 无 position_ids: 可选的[jax.Array] = 无 decoder_position_ids: 可选的[jax.Array] = 无 output_attentions: 可选的[bool] = 无 output_hidden_states: 可选的[bool] = 无 return_dict: 可选的[bool] = 无 train: bool = 假 params: 字典 = 无 dropout_rng: tuple(torch.FloatTensor)
参数
- input_ids (
jnp.ndarray
of shape(batch_size, sequence_length)
) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.可以使用PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
- attention_mask (
jnp.ndarray
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 (
jnp.ndarray
of shape(batch_size, target_sequence_length)
, optional) — Indices of decoder input sequence tokens in the vocabulary.可以使用PreTrainedTokenizer获取索引。详情请参见PreTrainedTokenizer.encode()和 PreTrainedTokenizer.call()。
对于序列到序列的训练,应该提供
decoder_input_ids
。decoder_input_ids
应该在模型外部通过将labels
向右移动,用pad_token_id
替换-100,并在前面加上decoder_start_token_id
来创建。 - decoder_attention_mask (
jnp.ndarray
of shape(batch_size, target_sequence_length)
, optional) — 默认行为:生成一个忽略decoder_input_ids
中填充标记的张量。默认情况下也会使用因果掩码。 - position_ids (
numpy.ndarray
of shape(batch_size, sequence_length)
, optional) — 每个输入序列标记在位置嵌入中的位置索引。选择范围在[0, config.encoder.max_position_embeddings - 1]
之间。 - decoder_position_ids (
numpy.ndarray
of shape(batch_size, sequence_length)
, optional) — 每个解码器输入序列标记在位置嵌入中的位置索引。选择范围为[0, config.decoder.max_position_embeddings - 1]
. - output_attentions (
bool
, 可选) — 是否返回所有注意力层的注意力张量。有关更多详细信息,请参见返回张量中的attentions
。 - output_hidden_states (
bool
, optional) — 是否返回所有层的隐藏状态。有关更多详细信息,请参见返回张量下的hidden_states
。 - return_dict (
bool
, 可选) — 如果设置为True
,模型将返回一个~utils.FlaxSeq2SeqLMOutput
而不是一个普通的元组。
返回
transformers.modeling_flax_outputs.FlaxSeq2SeqLMOutput 或 tuple(torch.FloatTensor)
一个 transformers.modeling_flax_outputs.FlaxSeq2SeqLMOutput 或一个由
torch.FloatTensor
组成的元组(如果传递了 return_dict=False
或当 config.return_dict=False
时),包含各种
元素,具体取决于配置(EncoderDecoderConfig)和输入。
-
logits (
jnp.ndarray
形状为(batch_size, sequence_length, config.vocab_size)
) — 语言建模头的预测分数(SoftMax 之前的每个词汇标记的分数)。 -
past_key_values (
tuple(tuple(jnp.ndarray))
, 可选, 当传递use_cache=True
或当config.use_cache=True
时返回) — 长度为config.n_layers
的tuple(jnp.ndarray)
元组,每个元组包含 2 个形状为(batch_size, num_heads, sequence_length, embed_size_per_head)
的张量和 2 个形状为(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)
的额外张量。包含预先计算的隐藏状态(自注意力块和交叉注意力块中的键和值),可用于(参见
past_key_values
输入)加速顺序解码。 -
decoder_hidden_states (
tuple(jnp.ndarray)
, 可选, 当传递output_hidden_states=True
或当config.output_hidden_states=True
时返回) — 形状为(batch_size, sequence_length, hidden_size)
的jnp.ndarray
元组(一个用于嵌入的输出 + 一个用于每层的输出)。解码器在每层输出处的隐藏状态加上初始嵌入输出。
-
decoder_attentions (
tuple(jnp.ndarray)
, 可选, 当传递output_attentions=True
或当config.output_attentions=True
时返回) — 形状为(batch_size, num_heads, sequence_length, sequence_length)
的jnp.ndarray
元组(每层一个)。解码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。
-
cross_attentions (
tuple(jnp.ndarray)
, 可选, 当传递output_attentions=True
或当config.output_attentions=True
时返回) — 形状为(batch_size, num_heads, sequence_length, sequence_length)
的jnp.ndarray
元组(每层一个)。解码器的交叉注意力层的注意力权重,在注意力 softmax 之后,用于计算交叉注意力头中的加权平均值。
-
encoder_last_hidden_state (
jnp.ndarray
形状为(batch_size, sequence_length, hidden_size)
, 可选) — 模型编码器最后一层输出的隐藏状态序列。 -
encoder_hidden_states (
tuple(jnp.ndarray)
, 可选, 当传递output_hidden_states=True
或当config.output_hidden_states=True
时返回) — 形状为(batch_size, sequence_length, hidden_size)
的jnp.ndarray
元组(一个用于嵌入的输出 + 一个用于每层的输出)。编码器在每层输出处的隐藏状态加上初始嵌入输出。
-
encoder_attentions (
tuple(jnp.ndarray)
, 可选, 当传递output_attentions=True
或当config.output_attentions=True
时返回) — 形状为(batch_size, num_heads, sequence_length, sequence_length)
的jnp.ndarray
元组(每层一个)。编码器的注意力权重,在注意力 softmax 之后,用于计算自注意力头中的加权平均值。
FlaxEncoderDecoderModel 的前向方法,重写了 __call__
特殊方法。
尽管前向传递的配方需要在此函数内定义,但之后应该调用Module
实例而不是这个,因为前者负责运行预处理和后处理步骤,而后者会默默地忽略它们。
示例:
>>> from transformers import FlaxEncoderDecoderModel, BertTokenizer, GPT2Tokenizer
>>> # load a fine-tuned bert2gpt2 model
>>> model = FlaxEncoderDecoderModel.from_pretrained("patrickvonplaten/bert2gpt2-cnn_dailymail-fp16")
>>> # load input & output tokenizer
>>> tokenizer_input = BertTokenizer.from_pretrained("google-bert/bert-base-cased")
>>> tokenizer_output = GPT2Tokenizer.from_pretrained("openai-community/gpt2")
>>> article = '''Sigma Alpha Epsilon is under fire for a video showing party-bound fraternity members
>>> singing a racist chant. SAE's national chapter suspended the students,
>>> but University of Oklahoma President David Boren took it a step further,
>>> saying the university's affiliation with the fraternity is permanently done.'''
>>> input_ids = tokenizer_input(article, add_special_tokens=True, return_tensors="np").input_ids
>>> # use GPT2's eos_token as the pad as well as eos token
>>> model.config.eos_token_id = model.config.decoder.eos_token_id
>>> model.config.pad_token_id = model.config.eos_token_id
>>> sequences = model.generate(input_ids, num_beams=4, max_length=12).sequences
>>> summary = tokenizer_output.batch_decode(sequences, skip_special_tokens=True)[0]
>>> assert summary == "SAS Alpha Epsilon suspended Sigma Alpha Epsilon members"
from_encoder_decoder_pretrained
< source >( encoder_pretrained_model_name_or_path: typing.Union[str, os.PathLike, NoneType] = None decoder_pretrained_model_name_or_path: typing.Union[str, os.PathLike, NoneType] = None *model_args **kwargs )
参数
- encoder_pretrained_model_name_or_path (
Union[str, os.PathLike]
, optional) — 用于初始化编码器的必要信息。可以是以下之一:- 一个字符串,表示托管在huggingface.co上的模型仓库中的预训练模型的模型ID。
- 一个路径,指向使用save_pretrained()保存的模型权重的目录,例如
./my_model_directory/
。
- decoder_pretrained_model_name_or_path (
Union[str, os.PathLike]
, 可选, 默认为None
) — 初始化解码器所需的信息。可以是以下之一:- 一个字符串,表示托管在huggingface.co上的模型仓库中的预训练模型的模型ID。
- 一个路径,指向使用save_pretrained()保存的模型权重的目录,例如
./my_model_directory/
。
- model_args (剩余的位置参数, 可选) —
所有剩余的位置参数将传递给底层模型的
__init__
方法. - kwargs (remaining dictionary of keyword arguments, optional) —
Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
output_attentions=True
).- To update the encoder configuration, use the prefix encoder_ for each configuration parameter.
- To update the decoder configuration, use the prefix decoder_ for each configuration parameter.
- To update the parent model configuration, do not use a prefix for each configuration parameter.
根据是否提供了
config
或自动加载而表现不同。
从一个或多个库的基类实例化编码器和解码器,使用预训练模型的检查点。
示例:
>>> from transformers import FlaxEncoderDecoderModel
>>> # initialize a bert2gpt2 from pretrained BERT and GPT2 models. Note that the cross-attention layers will be randomly initialized
>>> model = FlaxEncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-cased", "openai-community/gpt2")
>>> # saving model after fine-tuning
>>> model.save_pretrained("./bert2gpt2")
>>> # load fine-tuned model
>>> model = FlaxEncoderDecoderModel.from_pretrained("./bert2gpt2")