modeling

class XLMModel(vocab_size=95000, is_encoder=True, causal=False, n_langs=15, use_lang_embeddings=True, hidden_size=1024, hidden_act='gelu', num_attention_heads=8, num_hidden_layers=12, hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, use_sinusoidal_embeddings=False, layer_norm_eps=1e-12, embed_init_std=0.02209708691207961, init_std=0.02, pad_token_id=2, lang_id=4, lang2id={'ar': 0, 'bg': 1, 'de': 2, 'el': 3, 'en': 4, 'es': 5, 'fr': 6, 'hi': 7, 'ru': 8, 'sw': 9, 'th': 10, 'tr': 11, 'ur': 12, 'vi': 13, 'zh': 14})[源代码]

基类:paddlenlp.transformers.xlm.modeling.XLMPretrainedModel

The bare XLM Model transformer outputting raw hidden-states.

This model inherits from PretrainedModel. Refer to the superclass documentation for the generic methods.

This model is also a Paddle paddle.nn.Layer subclass. Use it as a regular Paddle Layer and refer to the Paddle documentation for all matter related to general usage and behavior.

参数
  • vocab_size (int, optional) -- Vocabulary size of inputs_ids in XLMModel. Also is the vocab size of token embedding matrix. Defines the number of different tokens that can be represented by the inputs_ids passed when calling XLMModel. Defaults to 95000.

  • is_encoder (bool, optional) -- Whether or not the initialized model should be a transformer encoder or decoder as seen in Vaswani et al. Defaults to True.

  • causal (bool, optional) -- Whether or not the model should behave in a causal manner. Causal models use a triangular attention mask in order to only attend to the left-side context instead if a bidirectional context. Defaults to False.

  • n_langs (int, optional) -- The number of languages the model handles. Set to 1 for monolingual models. Defaults to 15.

  • use_lang_embeddings (bool, optional) -- Whether to use language embeddings. Some models use additional language embeddings. Defaults to True.

  • hidden_size (int, optional) -- Dimensionality of the embedding layer, encoder layer. Defaults to 1024.

  • hidden_act (str, optional) -- The non-linear activation function in the feed-forward layer. "gelu", "relu" and any other paddle supported activation functions are supported. Defaults to "gelu".

  • num_attention_heads (int, optional) -- Number of attention heads for each attention layer in the Transformer encoder. Defaults to 8.

  • num_hidden_layers (int, optional) -- Number of hidden layers in the Transformer encoder. Defaults to 12.

  • hidden_dropout_prob (float, optional) -- The dropout probability for all fully connected layers in the embeddings and encoder. Defaults to 0.1.

  • attention_probs_dropout_prob (float, optional) -- The dropout probability used in MultiHeadAttention in all encoder layers to drop some attention target. Defaults to 0.1.

  • max_position_embeddings (int, optional) -- The maximum value of the dimensionality of position encoding, which dictates the maximum supported length of an input sequence. Defaults to 512.

  • use_sinusoidal_embeddings (bool, optional) -- Whether or not to use sinusoidal positional embeddings instead of absolute positional embeddings. Defaults to False.

  • layer_norm_eps (float, optional) -- The epsilon used by the layer normalization layers. Defaults to 1e-12.

  • embed_init_std (float, optional) -- The standard deviation of the truncated_normal_initializer for initializing the embedding matrices. Defaults to (1024*2)^-0.5.

  • init_std (int, optional) -- The standard deviation of the truncated_normal_initializer for initializing all weight matrices except the embedding matrices. Defaults to 0.02.

  • pad_token_id (int, optional) -- The index of padding token in the token vocabulary. Defaults to 2.

  • lang_id (int, optional) -- The ID of the language used by the model. This parameter is used when generating text in a given language. Defaults to 4.

  • lang2id (Dict[str, int], optional) -- Dictionary mapping languages string identifiers to their IDs.

forward(input_ids=None, langs=None, attention_mask=None, position_ids=None, lengths=None, cache=None, output_attentions=False, output_hidden_states=False)[源代码]

The XLMModel forward method, overrides the __call__() special method.

参数
  • input_ids (Tensor) -- Indices of input sequence tokens in the vocabulary. They are numerical representations of tokens that build the input sequence. Its data type should be int64 and it has a shape of [batch_size, sequence_length].

  • langs (Tensor, optional) -- A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are languages ids which can be obtained from the language names by using two conversion mappings provided in the configuration of the model (only provided for multilingual models). More precisely, the language name to language id mapping is in model.config['lang2id'] (which is a dictionary string to int). Shape as [batch_size, sequence_length] and dtype as int64. Defaults to None.

  • attention_mask (Tensor, optional) -- Mask used in multi-head attention to avoid performing attention on to some unwanted positions, usually the paddings or the subsequent positions. Its data type can be int, float and bool. When the data type is bool, the masked tokens have False values and the others have True values. When the data type is int, the masked tokens have 0 values and the others have 1 values. When the data type is float, the masked tokens have 0.0 values and the others have 1.0 values. It is a tensor with shape broadcasted to [batch_size, num_attention_heads, sequence_length, sequence_length]. Defaults to None, which means nothing needed to be prevented attention to.

  • position_ids (Tensor, optional) -- Indices of positions of each input sequence tokens in the position embeddings. Selected in the range [0, max_position_embeddings - 1]. Shape as [batch_size, sequence_length] and dtype as int64. Defaults to None.

  • lengths (Tensor, optional) -- Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use attention_mask for the same result (see above), kept here for compatibility. Indices selected in [0, ..., sequence_length]. Shape as [batch_size] and dtype as int64. Defaults to None.

  • cache (Tuple[Tuple[Tensor]], optional) -- Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model. Can be used to speed up sequential decoding. The input_ids which have their past given to this model should not be passed as input ids as they have already been computed. Defaults to None.

  • output_attentions (bool, optional) -- Whether or not to return the attentions tensors of all attention layers. Defaults to False.

  • output_hidden_states (bool, optional) -- Whether or not to return the output of all hidden layers. Defaults to False.

返回

Returns tuple (last_hidden_state, hidden_states, attentions)

With the fields:

  • last_hidden_state (Tensor):

    Sequence of hidden-states at the last layer of the model. It's data type should be float32 and its shape is [batch_size, sequence_length, hidden_size].

  • hidden_states (tuple(Tensor), optional):

    returned when output_hidden_states=True is passed. Tuple of Tensor (one for the output of the embeddings + one for the output of each layer). Each Tensor has a data type of float32 and its shape is [batch_size, sequence_length, hidden_size].

  • attentions (tuple(Tensor), optional):

    returned when output_attentions=True is passed. Tuple of Tensor (one for each layer) of shape. Each Tensor has a data type of float32 and its shape is [batch_size, num_heads, sequence_length, sequence_length].

返回类型

tuple

示例

import paddle
from paddlenlp.transformers import XLMModel, XLMTokenizer

tokenizer = XLMTokenizer.from_pretrained("xlm-mlm-tlm-xnli15-1024")
model = XLMModel.from_pretrained("xlm-mlm-tlm-xnli15-1024")

inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!", lang="en")
inputs = {k:paddle.to_tensor([v], dtype="int64") for (k, v) in inputs.items()}
inputs["langs"] = paddle.ones_like(inputs["input_ids"]) * tokenizer.lang2id["en"]

last_hidden_state = model(**inputs)[0]
class XLMPretrainedModel(*args, **kwargs)[源代码]

基类:paddlenlp.transformers.model_utils.PretrainedModel

An abstract class for pretrained XLM models. It provides XLM related model_config_file, resource_files_names, pretrained_resource_files_map, pretrained_init_configuration, base_model_prefix for downloading and loading pretrained models. See PretrainedModel for more details.

base_model_class

alias of paddlenlp.transformers.xlm.modeling.XLMModel

class XLMWithLMHeadModel(xlm)[源代码]

基类:paddlenlp.transformers.xlm.modeling.XLMPretrainedModel

The XLM Model transformer with a masked language modeling head on top (linear layer with weights tied to the input embeddings).

参数

xlm (XLMModel) -- An instance of XLMModel.

forward(input_ids=None, langs=None, attention_mask=None, position_ids=None, lengths=None, cache=None, labels=None)[源代码]

The XLMWithLMHeadModel forward method, overrides the __call__() special method.

参数
  • input_ids (Tensor) -- See XLMModel.

  • langs (Tensor, optional) -- See XLMModel.

  • attention_mask (Tensor, optional) -- See XLMModel.

  • position_ids (Tensor, optional) -- See XLMModel.

  • lengths (Tensor, optional) -- See XLMModel.

  • cache (Dict[str, Tensor], optional) -- See XLMModel.

  • labels (Tensor, optional) -- The Labels for computing the masked language modeling loss. Indices are selected in [-100, 0, ..., vocab_size-1] All labels set to -100 are ignored (masked), the loss is only computed for labels in [0, ..., vocab_size-1] Shape as [batch_size, sequence_length] and dtype as int64. Defaults to None.

返回

Returns tuple (loss, logits). With the fields:

  • loss (Tensor):

    returned when labels is provided. Language modeling loss (for next-token prediction). It's data type should be float32 and its shape is [1,].

  • logits (Tensor):

    Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). It's data type should be float32 and its shape is [batch_size, sequence_length, vocab_size].

返回类型

tuple

示例

import paddle
from paddlenlp.transformers import XLMWithLMHeadModel, XLMTokenizer

tokenizer = XLMTokenizer.from_pretrained('xlm-mlm-tlm-xnli15-1024')
model = XLMWithLMHeadModel.from_pretrained('xlm-mlm-tlm-xnli15-1024')

inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!", lang="en")
inputs = {k:paddle.to_tensor([v], dtype="int64") for (k, v) in inputs.items()}
inputs["langs"] = paddle.ones_like(inputs["input_ids"]) * tokenizer.lang2id["en"]
inputs["labels"] = inputs["input_ids"]

loss, logits = model(**inputs)
class XLMForSequenceClassification(xlm, num_classes=2, dropout=None)[源代码]

基类:paddlenlp.transformers.xlm.modeling.XLMPretrainedModel

The XLMModel with a sequence classification head on top (linear layer). XLMForSequenceClassification uses the first token in order to do the classification.

参数
  • xlm (XLMModel) -- An instance of XLMModel.

  • num_classes (int, optional) -- The number of classes. Defaults to 2.

  • dropout (float, optional) -- The dropout probability for output of XLM. If None, use the same value as hidden_dropout_prob of XLMModel instance xlm. Defaults to None.

forward(input_ids=None, langs=None, attention_mask=None, position_ids=None, lengths=None)[源代码]

The XLMForSequenceClassification forward method, overrides the __call__() special method.

参数
  • input_ids (Tensor) -- See XLMModel.

  • langs (Tensor, optional) -- See XLMModel.

  • attention_mask (Tensor, optional) -- See XLMModel.

  • position_ids (Tensor, optional) -- See XLMModel.

  • lengths (Tensor, optional) -- See XLMModel.

返回

A tensor of the input text classification logits. Shape as [batch_size, num_classes] and dtype as float32.

返回类型

logits (Tensor)

示例

import paddle
from paddlenlp.transformers import XLMForSequenceClassification, XLMTokenizer

tokenizer = XLMTokenizer.from_pretrained("xlm-mlm-tlm-xnli15-1024")
model = XLMForSequenceClassification.from_pretrained("xlm-mlm-tlm-xnli15-1024", num_classes=2)

inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!", lang="en")
inputs = {k:paddle.to_tensor([v], dtype="int64") for (k, v) in inputs.items()}
inputs["langs"] = paddle.ones_like(inputs["input_ids"]) * tokenizer.lang2id["en"]

logits = model(**inputs)
class XLMForTokenClassification(xlm, num_classes=2, dropout=None)[源代码]

基类:paddlenlp.transformers.xlm.modeling.XLMPretrainedModel

XLMModel with a linear layer on top of the hidden-states output layer, designed for token classification tasks like NER tasks.

参数
  • xlm (XLMModel) -- An instance of XLMModel.

  • num_classes (int, optional) -- The number of classes. Defaults to 2.

  • dropout (float, optional) -- The dropout probability for output of XLM. If None, use the same value as hidden_dropout_prob of XLMModel instance xlm. Defaults to None.

forward(input_ids=None, langs=None, attention_mask=None, position_ids=None, lengths=None)[源代码]

The XLMForTokenClassification forward method, overrides the __call__() special method.

参数
  • input_ids (Tensor) -- See XLMModel.

  • langs (Tensor, optional) -- See XLMModel.

  • attention_mask (Tensor, optional) -- See XLMModel.

  • position_ids (Tensor, optional) -- See XLMModel.

  • lengths (Tensor, optional) -- See XLMModel.

返回

A tensor of the input token classification logits. Shape as [batch_size, sequence_length, num_classes] and dtype as float32.

返回类型

logits (Tensor)

示例

import paddle
from paddlenlp.transformers import XLMForTokenClassification, XLMTokenizer

tokenizer = XLMTokenizer.from_pretrained("xlm-mlm-tlm-xnli15-1024")
model = XLMForTokenClassification.from_pretrained("xlm-mlm-tlm-xnli15-1024", num_classes=2)

inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!", lang="en")
inputs = {k:paddle.to_tensor([v], dtype="int64") for (k, v) in inputs.items()}
inputs["langs"] = paddle.ones_like(inputs["input_ids"]) * tokenizer.lang2id["en"]

logits = model(**inputs)
class XLMForQuestionAnsweringSimple(xlm)[源代码]

基类:paddlenlp.transformers.xlm.modeling.XLMPretrainedModel

XLMModel with a span classification head on top for extractive question-answering tasks like SQuAD (a linear layers on top of the hidden-states output to compute span start logits and span end logits).

参数

xlm (XLMModel) -- An instance of XLMModel.

forward(input_ids=None, langs=None, attention_mask=None, position_ids=None, lengths=None)[源代码]

The XLMForQuestionAnswering forward method, overrides the __call__() special method.

参数
  • input_ids (Tensor) -- See XLMModel.

  • langs (Tensor, optional) -- See XLMModel.

  • attention_mask (Tensor, optional) -- See XLMModel.

  • position_ids (Tensor, optional) -- See XLMModel.

  • lengths (Tensor, optional) -- See XLMModel.

返回

Returns tuple (start_logits, end_logits).

With the fields:

  • start_logits (Tensor):

    A tensor of the input token classification logits, indicates the start position of the labelled span. Its data type should be float32 and its shape is [batch_size, sequence_length].

  • end_logits (Tensor):

    A tensor of the input token classification logits, indicates the end position of the labelled span. Its data type should be float32 and its shape is [batch_size, sequence_length].

返回类型

tuple

示例

import paddle
from paddlenlp.transformers import XLMForQuestionAnswering, XLMTokenizer

tokenizer = XLMTokenizer.from_pretrained("xlm-mlm-tlm-xnli15-1024")
model = XLMForQuestionAnswering.from_pretrained("xlm-mlm-tlm-xnli15-1024", num_classes=2)

inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!", lang="en")
inputs = {k:paddle.to_tensor([v], dtype="int64") for (k, v) in inputs.items()}
inputs["langs"] = paddle.ones_like(inputs["input_ids"]) * tokenizer.lang2id["en"]

outputs = model(**inputs)

start_logits = outputs[0]
end_logits = outputs[1]
class XLMForMultipleChoice(xlm, num_choices=2, dropout=None)[源代码]

基类:paddlenlp.transformers.xlm.modeling.XLMPretrainedModel

XLMModel with a linear layer on top of the hidden-states output layer, designed for multiple choice tasks like RocStories/SWAG tasks.

参数
  • xlm (XLMModel) -- An instance of XLMModel.

  • num_choices (int, optional) -- The number of choices. Defaults to 2.

  • dropout (float, optional) -- The dropout probability for output of XLM. If None, use the same value as hidden_dropout_prob of XLMModel instance xlm. Defaults to None.

forward(input_ids=None, langs=None, attention_mask=None, position_ids=None, lengths=None)[源代码]

The XLMForMultipleChoice forward method, overrides the __call__() special method. :param input_ids: See XLMModel and shape as [batch_size, num_choice, sequence_length]. :type input_ids: Tensor :param langs: See XLMModel and shape as [batch_size, num_choice, sequence_length]. :type langs: Tensor, optional :param attention_mask: See XLMModel and shape as [batch_size, num_choice, sequence_length]. :type attention_mask: Tensor, optional :param position_ids: See XLMModel and shape as [batch_size, num_choice, sequence_length]. :type position_ids: Tensor, optional :param lengths: See XLMModel and shape as [batch_size, num_choice]. :type lengths: Tensor, optional

返回

A tensor of the multiple choice classification logits. Shape as [batch_size, num_choice] and dtype as float32.

返回类型

reshaped_logits (Tensor)

示例

import paddle
from paddlenlp.transformers import XLMForMultipleChoice, XLMTokenizer
from paddlenlp.data import Pad

tokenizer = XLMTokenizer.from_pretrained("xlm-mlm-tlm-xnli15-1024")
model = XLMForMultipleChoice.from_pretrained("xlm-mlm-tlm-xnli15-1024", num_choices=2)

data = [
    {
        "question": "how do you turn on an ipad screen?",
        "answer1": "press the volume button.",
        "answer2": "press the lock button.",
        "label": 1,
    },
    {
        "question": "how do you indent something?",
        "answer1": "leave a space before starting the writing",
        "answer2": "press the spacebar",
        "label": 0,
    },
]
text = []
text_pair = []
for d in data:
    text.append(d["question"])
    text_pair.append(d["answer1"])
    text.append(d["question"])
    text_pair.append(d["answer2"])

inputs = tokenizer(text, text_pair, lang="en")
input_ids = Pad(axis=0, pad_val=tokenizer.pad_token_id)(inputs["input_ids"])
input_ids = paddle.to_tensor(input_ids, dtype="int64")
langs = paddle.ones_like(input_ids) * tokenizer.lang2id["en"]

reshaped_logits = model(
    input_ids=input_ids,
    langs=langs,
)