modeling#

class SqueezeBertModel(config: SqueezeBertConfig)[source]#

Bases: SqueezeBertPreTrainedModel

get_input_embeddings()[source]#

get input embedding of model

Returns:

embedding of model

Return type:

nn.Embedding

set_input_embeddings(new_embeddings)[source]#

set new input embedding for model

Parameters:

value (Embedding) – the new embedding of model

Raises:

NotImplementedError – Model has not implement set_input_embeddings method

forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, output_attentions=None, output_hidden_states=None)[source]#

The forward method, overrides the __call__() special method. :param input_ids: 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].

Parameters:
  • 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. If its data type is int, the values should be either 0 or 1. - 1 for tokens that not masked, - 0 for tokens that masked. 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.

  • token_type_ids (Tensor, optional) – Segment token indices to indicate different portions of the inputs. Selected in the range [0, type_vocab_size - 1]. If type_vocab_size is 2, which means the inputs have two portions. Indices can either be 0 or 1: - 0 corresponds to a sentence A token, - 1 corresponds to a sentence B token. Its data type should be int64 and it has a shape of [batch_size, sequence_length]. Defaults to None, which means we don’t add segment embeddings.

  • 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, num_tokens) and dtype as int64. Defaults to None.

  • output_attentions (bool, optional) – Whether to return the attention_weight of each hidden layers. Defaults to False.

  • output_hidden_states (bool, optional) – Whether to return the output of each hidden layers. Defaults to False.

Returns:

Returns tuple (sequence_output, pooled_output) with (encoder_outputs, encoder_attentions) by optional. With the fields: - sequence_output (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].

  • pooled_output (Tensor):

    The output of first token ([CLS]) in sequence. We “pool” the model by simply taking the hidden state corresponding to the first token. Its data type should be float32 and its shape is [batch_size, hidden_size].

  • encoder_outputs (List(Tensor)):

    A list of Tensor containing hidden-states of the model at each hidden layer in the Transformer encoder. The length of the list is num_hidden_layers + 1 (Embedding Layer output). Each Tensor has a data type of float32 and its shape is [batch_size, sequence_length, hidden_size].

Return type:

tuple

class SqueezeBertPreTrainedModel(*args, **kwargs)[source]#

Bases: PretrainedModel

An abstract class for pretrained SqueezBert models. It provides SqueezBert 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.

config_class#

alias of SqueezeBertConfig

base_model_class#

alias of SqueezeBertModel

class SqueezeBertForSequenceClassification(config: SqueezeBertConfig)[source]#

Bases: SqueezeBertPreTrainedModel

SqueezeBert Model with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. :param config: An instance of SqueezeBertConfig. :type config: SqueezeBertConfig

forward(input_ids, token_type_ids=None, position_ids=None, attention_mask=None)[source]#

The SqueezeBertForSequenceClassification forward method, overrides the __call__() special method. :param input_ids: See SqueezeBertModel. :type input_ids: Tensor :param token_type_ids: See SqueezeBertModel. :type token_type_ids: Tensor, optional :param position_ids: See SqueezeBertModel. :type position_ids: Tensor, optional :param attention_mask: See SqueezeBertModel. :type attention_mask: list, optional

Returns:

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

Return type:

Tensor

class SqueezeBertForTokenClassification(config: SqueezeBertConfig)[source]#

Bases: SqueezeBertPreTrainedModel

SqueezeBert Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. :param config: An instance of SqueezeBertConfig. :type config: SqueezeBertConfig

forward(input_ids, token_type_ids=None, position_ids=None, attention_mask=None)[source]#

The SqueezeBertForTokenClassification forward method, overrides the __call__() special method. :param input_ids: See SqueezeBertModel. :type input_ids: Tensor :param token_type_ids: See SqueezeBertModel. :type token_type_ids: Tensor, optional :param position_ids: See SqueezeBertModel. :type position_ids: Tensor, optional :param attention_mask: See SqueezeBertModel. :type attention_mask: list, optional

Returns:

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

Return type:

Tensor

class SqueezeBertForQuestionAnswering(config: SqueezeBertConfig)[source]#

Bases: SqueezeBertPreTrainedModel

SqueezeBert Model 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). :param config: An instance of SqueezeBertConfig. :type config: SqueezeBertConfig

forward(input_ids, token_type_ids=None)[source]#

The SqueezeBertForQuestionAnswering forward method, overrides the __call__() special method. :param input_ids: See SqueezeBertModel. :type input_ids: Tensor :param token_type_ids: See SqueezeBertModel. :type token_type_ids: Tensor, optional

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