class allennlp.models.basic_classifier.BasicClassifier(vocab:, 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>)[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 and/or a Seq2VecEncoder and then pass the embeddings to a linear classification layer, which projects into the label space. If neither Seq2SeqEncoder nor Seq2VecEncoder is provided, we will pass the embedded text directly to the linear classification layer.

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.

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

From a TextField

label : torch.IntTensor, optional (default = None)

From a LabelField

An output dictionary consisting of:
logits : torch.FloatTensor

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

probs : torch.FloatTensor

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

loss : torch.FloatTensor, optional

A scalar loss to be optimised.

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

Returns a dictionary of metrics. This method will be called by 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.