class_docstr: The ``CrfTagger`` encodes a sequence of text with a ``Seq2SeqEncoder``,
  then uses a Conditional Random Field model to predict a tag for each token in the sequence.
  
  Parameters
  ----------
  vocab : ``Vocabulary``, required
      A Vocabulary, required in order to compute sizes for input/output projections.
  text_field_embedder : ``TextFieldEmbedder``, required
      Used to embed the tokens ``TextField`` we get as input to the model.
  encoder : ``Seq2SeqEncoder``
      The encoder that we will use in between embedding tokens and predicting output tags.
  label_namespace : ``str``, optional (default=``labels``)
      This is needed to compute the SpanBasedF1Measure metric.
      Unless you did something unusual, the default value should be what you want.
  feedforward : ``FeedForward``, optional, (default = None).
      An optional feedforward layer to apply after the encoder.
  dropout:  ``float``, optional (detault=``None``)
  verbose_metrics : ``bool``, optional (default = False)
      If true, metrics will be returned per label class in addition
      to the overall statistics.
  constraint_type : ``str``, optional (default=``None``)
      If provided, the CRF will be constrained at decoding time
      to produce valid labels based on the specified type (e.g. "BIO", or "BIOUL").
  include_start_end_transitions : ``bool``, optional (default=``True``)
      Whether to include start and end transition parameters in the CRF.
  initializer : ``InitializerApplicator``, optional (default=``InitializerApplicator()``)
      Used to initialize the model parameters.
  regularizer : ``RegularizerApplicator``, optional (default=``None``)
      If provided, will be used to calculate the regularization penalty during training.
class_docstr_tok: ["The","``","CrfTagger","``","encodes","a","sequence","of","text","with","a","``","Seq2SeqEncoder","``",",","then","uses","a","Conditional","Random","Field","model","to","predict","a","tag","for","each","token","in","the","sequence",".","Parameters","--","--","--","--","--","vocab",":","``","Vocabulary","``",",","required","A","Vocabulary",",","required","in","order","to","compute","sizes","for","input\/output","projections",".","text_field_embedder",":","``","TextFieldEmbedder","``",",","required","Used","to","embed","the","tokens","``","TextField","``","we","get","as","input","to","the","model",".","encoder",":","``","Seq2SeqEncoder","``","The","encoder","that","we","will","use","in","between","embedding","tokens","and","predicting","output","tags",".","label_namespace",":","``","str","``",",","optional","(","default=","``","labels","``",")","This","is","needed","to","compute","the","SpanBasedF1Measure","metric",".","Unless","you","did","something","unusual",",","the","default","value","should","be","what","you","want",".","feedforward",":","``","FeedForward","``",",","optional",",","(","default","=","None",")",".","An","optional","feedforward","layer","to","apply","after","the","encoder",".","dropout",":","``","float","``",",","optional","(","detault=","``","None","``",")","verbose_metrics",":","``","bool","``",",","optional","(","default","=","False",")","If","true",",","metrics","will","be","returned","per","label","class","in","addition","to","the","overall","statistics",".","constraint_type",":","``","str","``",",","optional","(","default=","``","None","``",")","If","provided",",","the","CRF","will","be","constrained","at","decoding","time","to","produce","valid","labels","based","on","the","specified","type","(","e.g",".","``","BIO","''",",","or","``","BIOUL","''",")",".","include_start_end_transitions",":","``","bool","``",",","optional","(","default=","``","True","``",")","Whether","to","include","start","and","end","transition","parameters","in","the","CRF",".","initializer",":","``","InitializerApplicator","``",",","optional","(","default=","``","InitializerApplicator","(",")","``",")","Used","to","initialize","the","model","parameters",".","regularizer",":","``","RegularizerApplicator","``",",","optional","(","default=","``","None","``",")","If","provided",",","will","be","used","to","calculate","the","regularization","penalty","during","training","."]
docstr: Parameters
  ----------
  tokens : ``Dict[str, torch.LongTensor]``, required
      The output of ``TextField.as_array()``, which should typically be passed directly to a
      ``TextFieldEmbedder``. This output is a dictionary mapping keys to ``TokenIndexer``
      tensors.  At its most basic, using a ``SingleIdTokenIndexer`` this is: ``{"tokens":
      Tensor(batch_size, num_tokens)}``. This dictionary will have the same keys as were used
      for the ``TokenIndexers`` when you created the ``TextField`` representing your
      sequence.  The dictionary is designed to be passed directly to a ``TextFieldEmbedder``,
      which knows how to combine different word representations into a single vector per
      token in your input.
  tags : ``torch.LongTensor``, optional (default = ``None``)
      A torch tensor representing the sequence of integer gold class labels of shape
      ``(batch_size, num_tokens)``.
  metadata : ``List[Dict[str, Any]]``, optional, (default = None)
      metadata containg the original words in the sentence to be tagged under a 'words' key.
  
  Returns
  -------
  An output dictionary consisting of:
  
  logits : ``torch.FloatTensor``
      The logits that are the output of the ``tag_projection_layer``
  mask : ``torch.LongTensor``
      The text field mask for the input tokens
  tags : ``List[List[int]]``
      The predicted tags using the Viterbi algorithm.
  loss : ``torch.FloatTensor``, optional
      A scalar loss to be optimised. Only computed if gold label ``tags`` are provided.
docstr_tok: ["Parameters","--","--","--","--","--","tokens",":","``","Dict","[","str",",","torch.LongTensor","]","``",",","required","The","output","of","``","TextField.as_array","(",")","``",",","which","should","typically","be","passed","directly","to","a","``","TextFieldEmbedder","``",".","This","output","is","a","dictionary","mapping","keys","to","``","TokenIndexer","``","tensors",".","At","its","most","basic",",","using","a","``","SingleIdTokenIndexer","``","this","is",":","``","{","``","tokens","''",":","Tensor","(","batch_size",",","num_tokens",")","}","``",".","This","dictionary","will","have","the","same","keys","as","were","used","for","the","``","TokenIndexers","``","when","you","created","the","``","TextField","``","representing","your","sequence",".","The","dictionary","is","designed","to","be","passed","directly","to","a","``","TextFieldEmbedder","``",",","which","knows","how","to","combine","different","word","representations","into","a","single","vector","per","token","in","your","input",".","tags",":","``","torch.LongTensor","``",",","optional","(","default","=","``","None","``",")","A","torch","tensor","representing","the","sequence","of","integer","gold","class","labels","of","shape","``","(","batch_size",",","num_tokens",")","``",".","metadata",":","``","List","[","Dict","[","str",",","Any","]","]","``",",","optional",",","(","default","=","None",")","metadata","containg","the","original","words","in","the","sentence","to","be","tagged","under","a","'words","'","key",".","Returns","--","--","--","-","An","output","dictionary","consisting","of",":","logits",":","``","torch.FloatTensor","``","The","logits","that","are","the","output","of","the","``","tag_projection_layer","``","mask",":","``","torch.LongTensor","``","The","text","field","mask","for","the","input","tokens","tags",":","``","List","[","List","[","int","]","]","``","The","predicted","tags","using","the","Viterbi","algorithm",".","loss",":","``","torch.FloatTensor","``",",","optional","A","scalar","loss","to","be","optimised",".","Only","computed","if","gold","label","``","tags","``","are","provided","."]
code: def forward(self, tokens, tags=None, metadata=None):  # type: ignore
      # pylint: disable=arguments-differ
      """
      Parameters
      ----------
      tokens : ``Dict[str, torch.LongTensor]``, required
          The output of ``TextField.as_array()``, which should typically be passed directly to a
          ``TextFieldEmbedder``. This output is a dictionary mapping keys to ``TokenIndexer``
          tensors.  At its most basic, using a ``SingleIdTokenIndexer`` this is: ``{"tokens":
          Tensor(batch_size, num_tokens)}``. This dictionary will have the same keys as were used
          for the ``TokenIndexers`` when you created the ``TextField`` representing your
          sequence.  The dictionary is designed to be passed directly to a ``TextFieldEmbedder``,
          which knows how to combine different word representations into a single vector per
          token in your input.
      tags : ``torch.LongTensor``, optional (default = ``None``)
          A torch tensor representing the sequence of integer gold class labels of shape
          ``(batch_size, num_tokens)``.
      metadata : ``List[Dict[str, Any]]``, optional, (default = None)
          metadata containg the original words in the sentence to be tagged under a 'words' key.
  
      Returns
      -------
      An output dictionary consisting of:
  
      logits : ``torch.FloatTensor``
          The logits that are the output of the ``tag_projection_layer``
      mask : ``torch.LongTensor``
          The text field mask for the input tokens
      tags : ``List[List[int]]``
          The predicted tags using the Viterbi algorithm.
      loss : ``torch.FloatTensor``, optional
          A scalar loss to be optimised. Only computed if gold label ``tags`` are provided.
      """
      embedded_text_input = self.text_field_embedder(tokens)
      mask = util.get_text_field_mask(tokens)
  
      if self.dropout:
          embedded_text_input = self.dropout(embedded_text_input)
  
      encoded_text = self.encoder(embedded_text_input, mask)
  
      if self.dropout:
          encoded_text = self.dropout(encoded_text)
  
      if self._feedforward is not None:
          encoded_text = self._feedforward(encoded_text)
  
      logits = self.tag_projection_layer(encoded_text)
      best_paths = self.crf.viterbi_tags(logits, mask)
  
      # Just get the tags and ignore the score.
      predicted_tags = [x for x, y in best_paths]
  
      output = {"logits": logits, "mask": mask, "tags": predicted_tags}
  
      if tags is not None:
          # Add negative log-likelihood as loss
          log_likelihood = self.crf(logits, tags, mask)
          output["loss"] = -log_likelihood
  
          # Represent viterbi tags as "class probabilities" that we can
          # feed into the `span_metric`
          class_probabilities = logits * 0.0
          for i, instance_tags in enumerate(predicted_tags):
              for j, tag_id in enumerate(instance_tags):
                  class_probabilities[i, j, tag_id] = 1
  
          self.span_metric(class_probabilities, tags, mask)
      if metadata is not None:
          output["words"] = [x["words"] for x in metadata]
      return output
  
file_path: magnitude/pymagnitude/third_party/allennlp/models/crf_tagger.py
imports: from __future__ import absolute_import
  import torch
  from torch.nn.modules.linear import Linear
  from allennlp.common.checks import check_dimensions_match
  from allennlp.data import Vocabulary
  from allennlp.modules import Seq2SeqEncoder, TimeDistributed, TextFieldEmbedder
  from allennlp.modules import ConditionalRandomField, FeedForward
  from allennlp.modules.conditional_random_field import allowed_transitions
  from allennlp.models.model import Model
  from allennlp.nn import InitializerApplicator, RegularizerApplicator
  import allennlp.nn.util
  from allennlp.training.metrics import SpanBasedF1Measure
label_desc: Use image node_id 2 for calling the CrfTagger obj's underlying member method code with example usage: obj.forward(tokens, tags, metadata) and returns: output