allennlp.models.basic_classifier

class allennlp.models.basic_classifier.BasicClassifier(vocab: allennlp.data.vocabulary.Vocabulary, text_field_embedder: allennlp.modules.text_field_embedders.text_field_embedder.TextFieldEmbedder, seq2vec_encoder: allennlp.modules.seq2vec_encoders.seq2vec_encoder.Seq2VecEncoder, seq2seq_encoder: allennlp.modules.seq2seq_encoders.seq2seq_encoder.Seq2SeqEncoder = None, dropout: float = None, num_labels: int = None, label_namespace: str = 'labels', initializer: allennlp.nn.initializers.InitializerApplicator = <allennlp.nn.initializers.InitializerApplicator object>, regularizer: Optional[allennlp.nn.regularizers.regularizer_applicator.RegularizerApplicator] = None)[source]

Bases: allennlp.models.model.Model

This Model implements a basic text classifier. After embedding the text into a text field, we will optionally encode the embeddings with a Seq2SeqEncoder. The resulting sequence is pooled using a Seq2VecEncoder and then passed to a linear classification layer, which projects into the label space. If a Seq2SeqEncoder is not provided, we will pass the embedded text directly to the Seq2VecEncoder.

Parameters
vocabVocabulary
text_field_embedderTextFieldEmbedder

Used to embed the input text into a TextField

seq2seq_encoderSeq2SeqEncoder, optional (default=``None``)

Optional Seq2Seq encoder layer for the input text.

seq2vec_encoderSeq2VecEncoder

Required Seq2Vec encoder layer. If seq2seq_encoder is provided, this encoder will pool its output. Otherwise, this encoder will operate directly on the output of the text_field_embedder.

dropoutfloat, optional (default = None)

Dropout percentage to use.

num_labels: ``int``, optional (default = ``None``)

Number of labels to project to in classification layer. By default, the classification layer will project to the size of the vocabulary namespace corresponding to labels.

label_namespace: ``str``, optional (default = “labels”)

Vocabulary namespace corresponding to labels. By default, we use the “labels” namespace.

initializerInitializerApplicator, optional (default=``InitializerApplicator()``)

If provided, will be used to initialize the model parameters.

regularizerRegularizerApplicator, optional (default=``None``)

If provided, will be used to calculate the regularization penalty during training.

decode(self, output_dict: Dict[str, torch.Tensor]) → Dict[str, torch.Tensor][source]

Does a simple argmax over the probabilities, converts index to string label, and add "label" key to the dictionary with the result.

forward(self, tokens: Dict[str, torch.LongTensor], label: torch.IntTensor = None) → Dict[str, torch.Tensor][source]
Parameters
tokensDict[str, torch.LongTensor]

From a TextField

labeltorch.IntTensor, optional (default = None)

From a LabelField

Returns
An output dictionary consisting of:
logitstorch.FloatTensor

A tensor of shape (batch_size, num_labels) representing unnormalized log probabilities of the label.

probstorch.FloatTensor

A tensor of shape (batch_size, num_labels) representing probabilities of the label.

losstorch.FloatTensor, optional

A scalar loss to be optimised.

get_metrics(self, reset: bool = False) → Dict[str, float][source]

Returns a dictionary of metrics. This method will be called by allennlp.training.Trainer in order to compute and use model metrics for early stopping and model serialization. We return an empty dictionary here rather than raising as it is not required to implement metrics for a new model. A boolean reset parameter is passed, as frequently a metric accumulator will have some state which should be reset between epochs. This is also compatible with Metrics should be populated during the call to ``forward`, with the Metric handling the accumulation of the metric until this method is called.