modeling#
- class DistilBertModel(config: DistilBertConfig)[源代码]#
-
The 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.
- 参数:
vocab_size (int) -- Vocabulary size of
inputs_ids
inDistilBertModel
. Defines the number of different tokens that can be represented by theinputs_ids
passed 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_size
tointermediate_size
, and then projected back tohidden_size
. Typicallyintermediate_size
is 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
.备注
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)[源代码]#
The DistilBertModel 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].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
masked
tokens haveFalse
values and the others haveTrue
values. When the data type is int, themasked
tokens have0
values and the others have1
values. When the data type is float, themasked
tokens have-INF
values and the others have0
values. 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 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].- 返回类型:
Tensor
示例
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)[源代码]#
-
An 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_prefix
for downloading and loading pretrained models. SeePretrainedModel
for more details.- config_class#
DistilBertConfig
的别名
- base_model_class#
DistilBertModel
的别名
- class DistilBertForSequenceClassification(config: DistilBertConfig)[源代码]#
-
DistilBert Model with a linear layer on top of the output layer, designed for sequence classification/regression tasks like GLUE tasks.
- 参数:
config (
DistilBertConfig
) -- An instance of DistilBertConfig used to construct DistilBertForSequenceClassification.
- forward(input_ids, attention_mask=None)[源代码]#
The DistilBertForSequenceClassification forward method, overrides the __call__() special method.
- 参数:
input_ids (Tensor) -- See
DistilBertModel
.attention_mask (list, optional) -- See
DistilBertModel
.
- 返回:
Returns tensor
logits
, a tensor of the input text classification logits. Shape as[batch_size, num_classes]
and dtype asfloat32
.- 返回类型:
Tensor
示例
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)[源代码]#
-
DistilBert Model with a linear layer on top of the hidden-states output layer, designed for token classification tasks like NER tasks.
- 参数:
config (
DistilBertConfig
) -- An instance of DistilBertConfig used to construct DistilBertForTokenClassification.
- forward(input_ids, attention_mask=None)[源代码]#
The DistilBertForTokenClassification forward method, overrides the __call__() special method.
- 参数:
input_ids (Tensor) -- See
DistilBertModel
.attention_mask (list, optional) -- See
DistilBertModel
.
- 返回:
Returns tensor
logits
, a tensor of the input token classification logits. Shape as[batch_size, sequence_length, num_classes]
and dtype asfloat32
.- 返回类型:
Tensor
示例
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)[源代码]#
-
DistilBert Model with a linear layer on top of the hidden-states output to compute
span_start_logits
andspan_end_logits
, designed for question-answering tasks like SQuAD.- 参数:
config (
DistilBertConfig
) -- An instance of DistilBertConfig used to construct DistilBertForQuestionAnswering.
- forward(input_ids, attention_mask=None)[源代码]#
The DistilBertForQuestionAnswering forward method, overrides the __call__() special method.
- 参数:
input_ids (Tensor) -- See
DistilBertModel
.attention_mask (list, optional) -- See
DistilBertModel
.
- 返回:
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.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)[源代码]#
-
DistilBert Model with a
language modeling
head on top.- 参数:
config (
DistilBertConfig
) -- An instance of DistilBertConfig used to construct DistilBertForMaskedLM
- forward(input_ids=None, attention_mask=None)[源代码]#
The DistilBertForMaskedLM forward method, overrides the
__call__()
special method.- 参数:
input_ids (Tensor) -- See
DistilBertModel
.attention_mask (Tensor, optional) -- See
DistilBertModel
.
- 返回:
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].- 返回类型:
Tensor
示例
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)