modeling#
- class DistilBertModel(config: DistilBertConfig)[source]#
Bases:
DistilBertPretrainedModelThe bare DistilBert 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.
- Parameters:
vocab_size (int) – Vocabulary size of
inputs_idsinDistilBertModel. Defines the number of different tokens that can be represented by theinputs_idspassed when callingDistilBertModel.hidden_size (int, optional) – Dimensionality of the embedding layer, encoder layers and the pooler layer. Defaults to
768.num_hidden_layers (int, optional) – Number of hidden layers in the Transformer encoder. Defaults to
12.num_attention_heads (int, optional) – Number of attention heads for each attention layer in the Transformer encoder. Defaults to
12.intermediate_size (int, optional) – Dimensionality of the feed-forward (ff) layer in the encoder. Input tensors to ff layers are firstly projected from
hidden_sizetointermediate_size, and then projected back tohidden_size. Typicallyintermediate_sizeis larger thanhidden_size. Defaults to3072.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".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.initializer_range (float, optional) –
The standard deviation of the normal initializer. Defaults to
0.02.Note
A normal_initializer initializes weight matrices as normal distributions. See
DistilBertPretrainedModel.init_weights()for how weights are initialized inDistilBertModel.pad_token_id (int, optional) – The index of padding token in the token vocabulary. Defaults to
0.
- forward(input_ids, attention_mask=None)[source]#
The DistilBertModel forward method, overrides the
__call__()special method.- Parameters:
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
int64and it has a shape of [batch_size, sequence_length].attention_mask (Tensor, optional) – Mask used in multi-head attention to avoid performing attention 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
maskedtokens haveFalsevalues and the others haveTruevalues. When the data type is int, themaskedtokens have0values and the others have1values. When the data type is float, themaskedtokens have-INFvalues and the others have0values. It is a tensor with shape broadcasted to[batch_size, num_attention_heads, sequence_length, sequence_length]. For example, its shape can be [batch_size, sequence_length], [batch_size, sequence_length, sequence_length], [batch_size, num_attention_heads, sequence_length, sequence_length]. Defaults toNone, which means nothing needed to be prevented attention to.
- Returns:
Returns tensor
encoder_output, which means the sequence of hidden-states at the last layer of the model. Its data type should be float32 and its shape is [batch_size, sequence_length, hidden_size].- Return type:
Tensor
Example
import paddle from paddlenlp.transformers import DistilBertModel, DistilBertTokenizer tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased') model = DistilBertModel.from_pretrained('distilbert-base-uncased') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} output = model(**inputs)
- class DistilBertPretrainedModel(*args, **kwargs)[source]#
Bases:
PretrainedModelAn abstract class for pretrained DistilBert models. It provides DistilBert related
model_config_file,pretrained_init_configuration,resource_files_names,pretrained_resource_files_map,base_model_prefixfor downloading and loading pretrained models. SeePretrainedModelfor more details.- config_class#
alias of
DistilBertConfig
- base_model_class#
alias of
DistilBertModel
- class DistilBertForSequenceClassification(config: DistilBertConfig)[source]#
Bases:
DistilBertPretrainedModelDistilBert Model with a linear layer on top of the output layer, designed for sequence classification/regression tasks like GLUE tasks.
- Parameters:
config (
DistilBertConfig) – An instance of DistilBertConfig used to construct DistilBertForSequenceClassification.
- forward(input_ids, attention_mask=None)[source]#
The DistilBertForSequenceClassification forward method, overrides the __call__() special method.
- Parameters:
input_ids (Tensor) – See
DistilBertModel.attention_mask (list, optional) – See
DistilBertModel.
- Returns:
Returns tensor
logits, a tensor of the input text classification logits. Shape as[batch_size, num_classes]and dtype asfloat32.- Return type:
Tensor
Example
import paddle from paddlenlp.transformers.distilbert.modeling import DistilBertForSequenceClassification from paddlenlp.transformers.distilbert.tokenizer import DistilBertTokenizer tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased') model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} outputs = model(**inputs) logits = outputs[0]
- class DistilBertForTokenClassification(config: DistilBertConfig)[source]#
Bases:
DistilBertPretrainedModelDistilBert Model with a linear layer on top of the hidden-states output layer, designed for token classification tasks like NER tasks.
- Parameters:
config (
DistilBertConfig) – An instance of DistilBertConfig used to construct DistilBertForTokenClassification.
- forward(input_ids, attention_mask=None)[source]#
The DistilBertForTokenClassification forward method, overrides the __call__() special method.
- Parameters:
input_ids (Tensor) – See
DistilBertModel.attention_mask (list, optional) – See
DistilBertModel.
- Returns:
Returns tensor
logits, a tensor of the input token classification logits. Shape as[batch_size, sequence_length, num_classes]and dtype asfloat32.- Return type:
Tensor
Example
import paddle from paddlenlp.transformers.distilbert.modeling import DistilBertForTokenClassification from paddlenlp.transformers.distilbert.tokenizer import DistilBertTokenizer tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased') model = DistilBertForTokenClassification.from_pretrained('distilbert-base-uncased') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} outputs = model(**inputs) logits = outputs[0]
- class DistilBertForQuestionAnswering(config: DistilBertConfig)[source]#
Bases:
DistilBertPretrainedModelDistilBert Model with a linear layer on top of the hidden-states output to compute
span_start_logitsandspan_end_logits, designed for question-answering tasks like SQuAD.- Parameters:
config (
DistilBertConfig) – An instance of DistilBertConfig used to construct DistilBertForQuestionAnswering.
- forward(input_ids, attention_mask=None)[source]#
The DistilBertForQuestionAnswering forward method, overrides the __call__() special method.
- Parameters:
input_ids (Tensor) – See
DistilBertModel.attention_mask (list, optional) – See
DistilBertModel.
- Returns:
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].
- Return type:
tuple
Example
import paddle from paddlenlp.transformers.distilbert.modeling import DistilBertForQuestionAnswering from paddlenlp.transformers.distilbert.tokenizer import DistilBertTokenizer tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased') model = DistilBertForQuestionAnswering.from_pretrained('distilbert-base-uncased') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} outputs = model(**inputs) start_logits = outputs[0] end_logits =outputs[1]
- class DistilBertForMaskedLM(config: DistilBertConfig)[source]#
Bases:
DistilBertPretrainedModelDistilBert Model with a
language modelinghead on top.- Parameters:
config (
DistilBertConfig) – An instance of DistilBertConfig used to construct DistilBertForMaskedLM
- forward(input_ids=None, attention_mask=None)[source]#
The DistilBertForMaskedLM forward method, overrides the
__call__()special method.- Parameters:
input_ids (Tensor) – See
DistilBertModel.attention_mask (Tensor, optional) – See
DistilBertModel.
- Returns:
Returns tensor
prediction_logits, the scores of masked token prediction. Its data type should be float32 and its shape is [batch_size, sequence_length, vocab_size].- Return type:
Tensor
Example
import paddle from paddlenlp.transformers import DistilBertForMaskedLM, DistilBertTokenizer tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased') model = DistilBertForMaskedLM.from_pretrained('distilbert-base-uncased') inputs = tokenizer("Welcome to use PaddlePaddle and PaddleNLP!") inputs = {k:paddle.to_tensor([v]) for (k, v) in inputs.items()} prediction_logits = model(**inputs)