Transformer XL¶
Overview¶
The Transformer-XL model was proposed in Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov. It’s a causal (uni-directional) transformer with relative positioning (sinusoïdal) embeddings which can reuse previously computed hidden-states to attend to longer context (memory). This model also uses adaptive softmax inputs and outputs (tied).
The abstract from the paper is the following:
Transformers have a potential of learning longer-term dependency, but are limited by a fixed-length context in the setting of language modeling. We propose a novel neural architecture Transformer-XL that enables learning dependency beyond a fixed length without disrupting temporal coherence. It consists of a segment-level recurrence mechanism and a novel positional encoding scheme. Our method not only enables capturing longer-term dependency, but also resolves the context fragmentation problem. As a result, Transformer-XL learns dependency that is 80% longer than RNNs and 450% longer than vanilla Transformers, achieves better performance on both short and long sequences, and is up to 1,800+ times faster than vanilla Transformers during evaluation. Notably, we improve the state-of-the-art results of bpc/perplexity to 0.99 on enwiki8, 1.08 on text8, 18.3 on WikiText-103, 21.8 on One Billion Word, and 54.5 on Penn Treebank (without finetuning). When trained only on WikiText-103, Transformer-XL manages to generate reasonably coherent, novel text articles with thousands of tokens.
Tips:
Transformer-XL uses relative sinusoidal positional embeddings. Padding can be done on the left or on the right. The original implementation trains on SQuAD with padding on the left, therefore the padding defaults are set to left.
Transformer-XL is one of the few models that has no sequence length limit.
The original code can be found here.
TransfoXLConfig¶
-
class
transformers.
TransfoXLConfig
(vocab_size=267735, cutoffs=[20000, 40000, 200000], d_model=1024, d_embed=1024, n_head=16, d_head=64, d_inner=4096, div_val=4, pre_lnorm=False, n_layer=18, mem_len=1600, clamp_len=1000, same_length=True, proj_share_all_but_first=True, attn_type=0, sample_softmax=- 1, adaptive=True, dropout=0.1, dropatt=0.0, untie_r=True, init='normal', init_range=0.01, proj_init_std=0.01, init_std=0.02, layer_norm_epsilon=1e-05, eos_token_id=0, **kwargs)[source]¶ This is the configuration class to store the configuration of a
TransfoXLModel
. It is used to instantiate a Transformer XL model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the Transformer XL architecture.Configuration objects inherit from
PretrainedConfig
and can be used to control the model outputs. Read the documentation fromPretrainedConfig
for more information.- Parameters
vocab_size (
int
, optional, defaults to 267735) – Vocabulary size of the Transformer XL model. Defines the different tokens that can be represented by the inputs_ids passed to the forward method ofTransfoXLModel
.cutoffs (
List[int]
, optional, defaults to[20000, 40000, 200000]
) – Cutoffs for the adaptive softmaxd_model (
int
, optional, defaults to 1024) – Dimensionality of the model’s hidden states.d_embed (
int
, optional, defaults to 1024) – Dimensionality of the embeddingsn_head (
int
, optional, defaults to 16) – Number of attention heads for each attention layer in the Transformer encoder.d_head (
int
, optional, defaults to 64) – Dimensionality of the model’s heads.d_inner (
int
, optional, defaults to 4096) – Inner dimension in FFdiv_val (
int
, optional, defaults to 4) – Divident value for adapative input and softmaxpre_lnorm (
boolean
, optional, defaults toFalse
) – Apply LayerNorm to the input instead of the outputn_layer (
int
, optional, defaults to 18) – Number of hidden layers in the Transformer encoder.mem_len (
int
, optional, defaults to 1600) – Length of the retained previous headsclamp_len (
int
, optional, defaults to 1000) – use the same pos embeddings after clamp_lensame_length (
boolean
, optional, defaults toTrue
) – Use the same attn length for all tokensproj_share_all_but_first (
boolean
, optional, defaults toTrue
) – True to share all but first projs, False not to share.attn_type (
int
, optional, defaults to 0) – Attention type. 0 for Transformer-XL, 1 for Shaw et al, 2 for Vaswani et al, 3 for Al Rfou et al.sample_softmax (
int
, optional, defaults to -1) – number of samples in sampled softmaxadaptive (
boolean
, optional, defaults toTrue
) – use adaptive softmaxdropout (
float
, optional, defaults to 0.1) – The dropout probabilitiy for all fully connected layers in the embeddings, encoder, and pooler.dropatt (
float
, optional, defaults to 0) – The dropout ratio for the attention probabilities.untie_r (
boolean
, optional, defaults toTrue
) – Untie relative position biasesinit (
string
, optional, defaults to normal) – Parameter initializer to useinit_range (
float
, optional, defaults to 0.01) – Parameters initialized by U(-init_range, init_range).proj_init_std (
float
, optional, defaults to 0.01) – Parameters initialized by N(0, init_std)init_std (
float
, optional, defaults to 0.02) – Parameters initialized by N(0, init_std)layer_norm_epsilon (
float
, optional, defaults to 1e-5) – The epsilon to use in the layer normalization layers
Example:
>>> from transformers import TransfoXLConfig, TransfoXLModel >>> # Initializing a Transformer XL configuration >>> configuration = TransfoXLConfig() >>> # Initializing a model from the configuration >>> model = TransfoXLModel(configuration) >>> # Accessing the model configuration >>> configuration = model.config
TransfoXLTokenizer¶
-
class
transformers.
TransfoXLTokenizer
(special=None, min_freq=0, max_size=None, lower_case=False, delimiter=None, vocab_file=None, pretrained_vocab_file=None, never_split=None, unk_token='<unk>', eos_token='<eos>', additional_special_tokens=['<formula>'], language='en', **kwargs)[source]¶ Transformer-XL tokenizer adapted from Vocab class in https://github.com/kimiyoung/transformer-xl
This tokenizer inherits from
PreTrainedTokenizer
which contains most of the methods. Users should refer to the superclass for more information regarding methods.
TransfoXLTokenizerFast¶
-
class
transformers.
TransfoXLTokenizerFast
(special=None, min_freq=0, max_size=None, lower_case=False, delimiter=None, vocab_file=None, pretrained_vocab_file=None, never_split=None, unk_token='<unk>', eos_token='<eos>', additional_special_tokens=['<formula>'], add_eos=False, add_double_eos=False, normalization=None, **kwargs)[source]¶ Construct a “Fast” Transformer-XL tokenizer (backed by HuggingFace’s tokenizers library).
The Transformer-XL tokenizer is a word-level tokenizer (no sub-word tokenization).
Adapted from Vocab class in https://github.com/kimiyoung/transformer-xl
This tokenizer inherits from
PreTrainedTokenizerFast
which contains most of the methods. Users should refer to the superclass for more information regarding methods.-
save_pretrained
(save_directory)[source]¶ Save the tokenizer vocabulary files together with:
added tokens,
special tokens to class attributes mapping,
tokenizer instantiation positional and keywords inputs (e.g. do_lower_case for Bert).
This method make sure the full tokenizer can then be re-loaded using the
from_pretrained()
class method.Warning
This won’t save modifications you may have applied to the tokenizer after the instantiation (for instance, modifying
tokenizer.do_lower_case
after creation).- Parameters
save_directory (
str
) – The path to adirectory where the tokenizer will be saved.- Returns
The files saved.
- Return type
A tuple of
str
-
TransfoXL specific outputs¶
-
class
transformers.modeling_transfo_xl.
TransfoXLModelOutput
(last_hidden_state: torch.FloatTensor, mems: List[torch.FloatTensor] = None, hidden_states: Optional[Tuple[torch.FloatTensor]] = None, attentions: Optional[Tuple[torch.FloatTensor]] = None)[source]¶ Base class for model’s outputs that may also contain a past key/values (to speed up sequential decoding).
- Parameters
last_hidden_state (
torch.FloatTensor
of shape(batch_size, sequence_length, hidden_size)
) – Sequence of hidden-states at the output of the last layer of the model.mems (
List[torch.FloatTensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.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, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) –Tuple of
torch.FloatTensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
-
class
transformers.modeling_transfo_xl.
TransfoXLLMHeadModelOutput
(losses: Optional[torch.FloatTensor] = None, prediction_scores: torch.FloatTensor = None, mems: List[torch.FloatTensor] = None, hidden_states: Optional[Tuple[torch.FloatTensor]] = None, attentions: Optional[Tuple[torch.FloatTensor]] = None)[source]¶ Base class for model’s outputs that may also contain a past key/values (to speed up sequential decoding).
- Parameters
losses (
torch.FloatTensor
of shape (batch_size, sequence_length-1), optional, returned whenlabels
is provided) – Language modeling losses (not reduced).prediction_scores (
torch.FloatTensor
of shape(batch_size, sequence_length, config.vocab_size)
) – Prediction scores of the language modeling head (scores for each vocabulary token after SoftMax).mems (
List[torch.FloatTensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.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, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) –Tuple of
torch.FloatTensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
-
class
transformers.modeling_tf_transfo_xl.
TFTransfoXLModelOutput
(last_hidden_state: tensorflow.python.framework.ops.Tensor = None, mems: List[tensorflow.python.framework.ops.Tensor] = None, hidden_states: Optional[Tuple[tensorflow.python.framework.ops.Tensor]] = None, attentions: Optional[Tuple[tensorflow.python.framework.ops.Tensor]] = None)[source]¶ Base class for model’s outputs that may also contain a past key/values (to speed up sequential decoding).
- Parameters
last_hidden_state (
tf.Tensor
of shape(batch_size, sequence_length, hidden_size)
) – Sequence of hidden-states at the output of the last layer of the model.mems (
List[tf.Tensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) –Tuple of
tf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) –Tuple of
tf.Tensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
-
class
transformers.modeling_tf_transfo_xl.
TFTransfoXLLMHeadModelOutput
(prediction_scores: tensorflow.python.framework.ops.Tensor = None, mems: List[tensorflow.python.framework.ops.Tensor] = None, hidden_states: Optional[Tuple[tensorflow.python.framework.ops.Tensor]] = None, attentions: Optional[Tuple[tensorflow.python.framework.ops.Tensor]] = None)[source]¶ Base class for model’s outputs that may also contain a past key/values (to speed up sequential decoding).
- Parameters
losses (
tf.Tensor
of shape (batch_size, sequence_length-1), optional, returned whenlabels
is provided) – Language modeling losses (not reduced).prediction_scores (
tf.Tensor
of shape(batch_size, sequence_length, config.vocab_size)
) – Prediction scores of the language modeling head (scores for each vocabulary token after SoftMax).mems (
List[tf.Tensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) –Tuple of
tf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) –Tuple of
tf.Tensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
TransfoXLModel¶
-
class
transformers.
TransfoXLModel
(config)[source]¶ The bare Bert Model transformer outputting raw hidden-states without any specific head on top.
This model is a PyTorch torch.nn.Module sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
- Parameters
config (
TransfoXLConfig
) – Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out thefrom_pretrained()
method to load the model weights.
-
forward
(input_ids=None, mems=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)[source]¶ The
TransfoXLModel
forward method, overrides the__call__()
special method.Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.- Parameters
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) –Indices of input sequence tokens in the vocabulary.
Indices can be obtained using
transformers.TransfoXLTokenizer
. Seetransformers.PreTrainedTokenizer.encode()
andtransformers.PreTrainedTokenizer.__call__()
for details.mems (
List[torch.FloatTensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see mems output below). Can be used to speed up sequential decoding. The token ids which have their mems given to this model should not be passed as input ids as they have already been computed.head_mask (
torch.FloatTensor
of shape(num_heads,)
or(num_layers, num_heads)
, optional) – Mask to nullify selected heads of the self-attention modules. Mask values selected in[0, 1]
:1
indicates the head is not masked,0
indicates the head is masked.inputs_embeds (
torch.FloatTensor
of shape(batch_size, sequence_length, hidden_size)
, optional) – Optionally, instead of passinginput_ids
you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the model’s internal embedding lookup matrix.output_attentions (
bool
, optional) – If set toTrue
, the attentions tensors of all attention layers are returned. Seeattentions
under returned tensors for more detail.output_hidden_states (
bool
, optional) – If set toTrue
, the hidden states of all layers are returned. Seehidden_states
under returned tensors for more detail.return_dict (
bool
, optional) – If set toTrue
, the model will return aModelOutput
instead of a plain tuple.
- Returns
A
TransfoXLModelOutput
(ifreturn_dict=True
is passed or whenconfig.return_dict=True
) or a tuple oftorch.FloatTensor
comprising various elements depending on the configuration (TransfoXLConfig
) and inputs.last_hidden_state (
torch.FloatTensor
of shape(batch_size, sequence_length, hidden_size)
) – Sequence of hidden-states at the output of the last layer of the model.mems (
List[torch.FloatTensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) – Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) – Tuple oftorch.FloatTensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
- Return type
TransfoXLModelOutput
ortuple(torch.FloatTensor)
Example:
>>> from transformers import TransfoXLTokenizer, TransfoXLModel >>> import torch >>> tokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') >>> model = TransfoXLModel.from_pretrained('transfo-xl-wt103', return_dict=True) >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") >>> outputs = model(**inputs) >>> last_hidden_states = outputs.last_hidden_state
TransfoXLLMHeadModel¶
-
class
transformers.
TransfoXLLMHeadModel
(config)[source]¶ The Transformer-XL Model with a language modeling head on top (adaptive softmax with weights tied to the adaptive input embeddings)
This model is a PyTorch torch.nn.Module sub-class. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.
- Parameters
config (
TransfoXLConfig
) – Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out thefrom_pretrained()
method to load the model weights.
-
forward
(input_ids=None, mems=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)[source]¶ The
TransfoXLLMHeadModel
forward method, overrides the__call__()
special method.Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.- Parameters
input_ids (
torch.LongTensor
of shape(batch_size, sequence_length)
) –Indices of input sequence tokens in the vocabulary.
Indices can be obtained using
transformers.TransfoXLTokenizer
. Seetransformers.PreTrainedTokenizer.encode()
andtransformers.PreTrainedTokenizer.__call__()
for details.mems (
List[torch.FloatTensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see mems output below). Can be used to speed up sequential decoding. The token ids which have their mems given to this model should not be passed as input ids as they have already been computed.head_mask (
torch.FloatTensor
of shape(num_heads,)
or(num_layers, num_heads)
, optional) – Mask to nullify selected heads of the self-attention modules. Mask values selected in[0, 1]
:1
indicates the head is not masked,0
indicates the head is masked.inputs_embeds (
torch.FloatTensor
of shape(batch_size, sequence_length, hidden_size)
, optional) – Optionally, instead of passinginput_ids
you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the model’s internal embedding lookup matrix.output_attentions (
bool
, optional) – If set toTrue
, the attentions tensors of all attention layers are returned. Seeattentions
under returned tensors for more detail.output_hidden_states (
bool
, optional) – If set toTrue
, the hidden states of all layers are returned. Seehidden_states
under returned tensors for more detail.return_dict (
bool
, optional) – If set toTrue
, the model will return aModelOutput
instead of a plain tuple.labels (
torch.LongTensor
of shape(batch_size, sequence_length)
, optional) – Labels for language modeling. Note that the labels are shifted inside the model, i.e. you can setlabels = input_ids
Indices are selected in[-100, 0, ..., config.vocab_size]
All labels set to-100
are ignored (masked), the loss is only computed for labels in[0, ..., config.vocab_size]
- Returns
A
TransfoXLLMHeadModelOutput
(ifreturn_dict=True
is passed or whenconfig.return_dict=True
) or a tuple oftorch.FloatTensor
comprising various elements depending on the configuration (TransfoXLConfig
) and inputs.losses (
torch.FloatTensor
of shape (batch_size, sequence_length-1), optional, returned whenlabels
is provided) Language modeling losses (not reduced).prediction_scores (
torch.FloatTensor
of shape(batch_size, sequence_length, config.vocab_size)
) – Prediction scores of the language modeling head (scores for each vocabulary token after SoftMax).mems (
List[torch.FloatTensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(torch.FloatTensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) – Tuple oftorch.FloatTensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(torch.FloatTensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) – Tuple oftorch.FloatTensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
- Return type
TransfoXLLMHeadModelOutput
ortuple(torch.FloatTensor)
Example:
>>> import torch >>> from transformers import TransfoXLTokenizer, TransfoXLLMHeadModel >>> tokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') >>> model = TransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103', return_dict=True) >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") >>> outputs = model(**inputs, labels=inputs["input_ids"]) >>> loss = outputs.loss >>> logits = outputs.logits
TFTransfoXLModel¶
-
class
transformers.
TFTransfoXLModel
(*args, **kwargs)[source]¶ The bare Bert Model transformer outputing raw hidden-states without any specific head on top.
Note
TF 2.0 models accepts two formats as inputs:
having all inputs as keyword arguments (like PyTorch models), or
having all inputs as a list, tuple or dict in the first positional arguments.
This second option is useful when using
tf.keras.Model.fit()
method which currently requires having all the tensors in the first argument of the model call function:model(inputs)
.If you choose this second option, there are three possibilities you can use to gather all the input Tensors in the first positional argument :
a single Tensor with input_ids only and nothing else:
model(inputs_ids)
a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
model([input_ids, attention_mask])
ormodel([input_ids, attention_mask, token_type_ids])
a dictionary with one or several input Tensors associated to the input names given in the docstring:
model({'input_ids': input_ids, 'token_type_ids': token_type_ids})
- Parameters
config (
TransfoXLConfig
) – Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out thefrom_pretrained()
method to load the model weights.
-
call
(inputs, **kwargs)[source]¶ The
TFTransfoXLModel
forward method, overrides the__call__()
special method.Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.- Parameters
input_ids (
tf.Tensor
orNumpy array
of shape(batch_size, sequence_length)
) –Indices of input sequence tokens in the vocabulary.
Indices can be obtained using
transformers.TransfoXLTokenizer
. Seetransformers.PreTrainedTokenizer.encode()
andtransformers.PreTrainedTokenizer.__call__()
for details.mems (
List[tf.Tensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see mems output below). Can be used to speed up sequential decoding. The token ids which have their mems given to this model should not be passed as input ids as they have already been computed.head_mask (
tf.Tensor
orNumpy array
of shape(num_heads,)
or(num_layers, num_heads)
, optional) – Mask to nullify selected heads of the self-attention modules. Mask values selected in[0, 1]
:1
indicates the head is not masked,0
indicates the head is masked.inputs_embeds (
tf.Tensor
orNumpy array
of shape(batch_size, sequence_length, hidden_size)
, optional) – Optionally, instead of passinginput_ids
you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the model’s internal embedding lookup matrix.output_attentions (
bool
, optional) – If set toTrue
, the attentions tensors of all attention layers are returned. Seeattentions
under returned tensors for more detail.output_hidden_states (
bool
, optional) – If set toTrue
, the hidden states of all layers are returned. Seehidden_states
under returned tensors for more detail.return_dict (
bool
, optional) – If set toTrue
, the model will return aModelOutput
instead of a plain tuple.
- Returns
A
TFTransfoXLModelOutput
(ifreturn_dict=True
is passed or whenconfig.return_dict=True
) or a tuple oftf.Tensor
comprising various elements depending on the configuration (TransfoXLConfig
) and inputs.last_hidden_state (
tf.Tensor
of shape(batch_size, sequence_length, hidden_size)
) – Sequence of hidden-states at the output of the last layer of the model.mems (
List[tf.Tensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) – Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) – Tuple oftf.Tensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
- Return type
TFTransfoXLModelOutput
ortuple(tf.Tensor)
Example:
>>> from transformers import TransfoXLTokenizer, TFTransfoXLModel >>> import tensorflow as tf >>> tokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') >>> model = TFTransfoXLModel.from_pretrained('transfo-xl-wt103') >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="tf") >>> outputs = model(inputs) >>> last_hidden_states = outputs[0] # The last hidden-state is the first element of the output tuple
TFTransfoXLLMHeadModel¶
-
class
transformers.
TFTransfoXLLMHeadModel
(*args, **kwargs)[source]¶ The Transformer-XL Model with a language modeling head on top (adaptive softmax with weights tied to the adaptive input embeddings)
Note
TF 2.0 models accepts two formats as inputs:
having all inputs as keyword arguments (like PyTorch models), or
having all inputs as a list, tuple or dict in the first positional arguments.
This second option is useful when using
tf.keras.Model.fit()
method which currently requires having all the tensors in the first argument of the model call function:model(inputs)
.If you choose this second option, there are three possibilities you can use to gather all the input Tensors in the first positional argument :
a single Tensor with input_ids only and nothing else:
model(inputs_ids)
a list of varying length with one or several input Tensors IN THE ORDER given in the docstring:
model([input_ids, attention_mask])
ormodel([input_ids, attention_mask, token_type_ids])
a dictionary with one or several input Tensors associated to the input names given in the docstring:
model({'input_ids': input_ids, 'token_type_ids': token_type_ids})
- Parameters
config (
TransfoXLConfig
) – Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out thefrom_pretrained()
method to load the model weights.
-
call
(inputs, mems=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None, labels=None, training=False)[source]¶ The
TFTransfoXLLMHeadModel
forward method, overrides the__call__()
special method.Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.- Parameters
input_ids (
tf.Tensor
orNumpy array
of shape(batch_size, sequence_length)
) –Indices of input sequence tokens in the vocabulary.
Indices can be obtained using
transformers.TransfoXLTokenizer
. Seetransformers.PreTrainedTokenizer.encode()
andtransformers.PreTrainedTokenizer.__call__()
for details.mems (
List[tf.Tensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see mems output below). Can be used to speed up sequential decoding. The token ids which have their mems given to this model should not be passed as input ids as they have already been computed.head_mask (
tf.Tensor
orNumpy array
of shape(num_heads,)
or(num_layers, num_heads)
, optional) – Mask to nullify selected heads of the self-attention modules. Mask values selected in[0, 1]
:1
indicates the head is not masked,0
indicates the head is masked.inputs_embeds (
tf.Tensor
orNumpy array
of shape(batch_size, sequence_length, hidden_size)
, optional) – Optionally, instead of passinginput_ids
you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert input_ids indices into associated vectors than the model’s internal embedding lookup matrix.output_attentions (
bool
, optional) – If set toTrue
, the attentions tensors of all attention layers are returned. Seeattentions
under returned tensors for more detail.output_hidden_states (
bool
, optional) – If set toTrue
, the hidden states of all layers are returned. Seehidden_states
under returned tensors for more detail.return_dict (
bool
, optional) – If set toTrue
, the model will return aModelOutput
instead of a plain tuple.
- Returns
A
TFTransfoXLLMHeadModelOutput
(ifreturn_dict=True
is passed or whenconfig.return_dict=True
) or a tuple oftf.Tensor
comprising various elements depending on the configuration (TransfoXLConfig
) and inputs.losses (
tf.Tensor
of shape (batch_size, sequence_length-1), optional, returned whenlabels
is provided) Language modeling losses (not reduced).prediction_scores (
tf.Tensor
of shape(batch_size, sequence_length, config.vocab_size)
) – Prediction scores of the language modeling head (scores for each vocabulary token after SoftMax).mems (
List[tf.Tensor]
of lengthconfig.n_layers
) – Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see mems input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed.hidden_states (
tuple(tf.Tensor)
, optional, returned whenoutput_hidden_states=True
is passed or whenconfig.output_hidden_states=True
) – Tuple oftf.Tensor
(one for the output of the embeddings + one for the output of each layer) of shape(batch_size, sequence_length, hidden_size)
.Hidden-states of the model at the output of each layer plus the initial embedding outputs.
attentions (
tuple(tf.Tensor)
, optional, returned whenoutput_attentions=True
is passed or whenconfig.output_attentions=True
) – Tuple oftf.Tensor
(one for each layer) of shape(batch_size, num_heads, sequence_length, sequence_length)
.Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.
- Return type
TFTransfoXLLMHeadModelOutput
ortuple(tf.Tensor)
Example:
>>> from transformers import TransfoXLTokenizer, TFTransfoXLLMHeadModel >>> import tensorflow as tf >>> tokenizer = TransfoXLTokenizer.from_pretrained('transfo-xl-wt103') >>> model = TFTransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103') >>> inputs = tokenizer("Hello, my dog is cute", return_tensors="tf") >>> outputs = model(inputs) >>> logits = outputs[0]