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 aSeq2SeqEncoder
. The resulting sequence is pooled using aSeq2VecEncoder
and then passed to a linear classification layer, which projects into the label space. If aSeq2SeqEncoder
is not provided, we will pass the embedded text directly to theSeq2VecEncoder
.- Parameters
- vocab
Vocabulary
- text_field_embedder
TextFieldEmbedder
Used to embed the input text into a
TextField
- seq2seq_encoder
Seq2SeqEncoder
, optional (default=``None``) Optional Seq2Seq encoder layer for the input text.
- seq2vec_encoder
Seq2VecEncoder
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.
- dropout
float
, 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.
- initializer
InitializerApplicator
, optional (default=``InitializerApplicator()``) If provided, will be used to initialize the model parameters.
- regularizer
RegularizerApplicator
, optional (default=``None``) If provided, will be used to calculate the regularization penalty during training.
- vocab
-
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 withMetrics should be populated during the call to ``forward`
, with theMetric
handling the accumulation of the metric until this method is called.