class_docstr: This ``Field`` represents a production rule from a grammar, like "S -> [NP, VP]", "N -> John",
  or "<b,c> -> [<a,<b,c>>, a]".
  
  We assume a few things about how these rules are formatted:
  
      - There is a left-hand side (LHS) and a right-hand side (RHS), where the LHS is always a
        non-terminal, and the RHS is either a terminal, a non-terminal, or a sequence of
        terminals and/or non-terminals.
      - The LHS and the RHS are joined by " -> ", and this sequence of characters appears nowhere
        else in the rule.
      - Non-terminal sequences in the RHS are formatted as "[NT1, NT2, ...]".
      - Some rules come from a global grammar used for a whole dataset, while other rules are
        specific to a particular ``Instance``.
  
  We don't make use of most of these assumptions in this class, but the code that consumes this
  ``Field`` relies heavily on them in some places.
  
  If the given rule is in the global grammar, we treat the rule as a vocabulary item that will
  get an index and (in the model) an embedding.  If the rule is not in the global grammar, we do
  not create a vocabulary item from the rule, and don't produce a tensor for the rule - we assume
  the model will handle representing this rule in some other way.
  
  Because we represent global grammar rules and instance-specific rules differently, this
  ``Field`` does not lend itself well to batching its arrays, even in a sequence for a single
  training instance.  A model using this field will have to manually batch together rule
  representations after splitting apart the global rules from the ``Instance`` rules.
  
  In a model, this will get represented as a ``ProductionRuleArray``, which is defined above as
  ``Tuple[str, bool, Optional[torch.Tensor]]``.  This is a tuple of ``(rule_string,
  is_global_rule, [rule_id])``, where the ``rule_id`` ``Tensor``, if present, will have shape
  ``(1,)``.  We don't do any batching of the ``Tensors``, so this gets passed to
  ``Model.forward()`` as a ``List[ProductionRuleArray]``.  We pass along the rule string because
  there isn't another way to recover it for instance-specific rules that do not make it into the
  vocabulary.
  
  Parameters
  ----------
  rule : ``str``
      The production rule, formatted as described above.  If this field is just padding, ``rule``
      will be the empty string.
  is_global_rule : ``bool``
      Whether this rule comes from the global grammar or is an instance-specific production rule.
  vocab_namespace : ``str``, optional (default="rule_labels")
      The vocabulary namespace to use for the global production rules.  We use "rule_labels" by
      default, because we typically do not want padding and OOV tokens for these, and ending the
      namespace with "labels" means we don't get padding and OOV tokens.
class_docstr_tok: ["This","``","Field","``","represents","a","production","rule","from","a","grammar",",","like","``","S","-",">","[","NP",",","VP","]","''",",","``","N","-",">","John","''",",","or","``","<","b",",","c",">","-",">","[","<","a",",","<","b",",","c",">",">",",","a","]","''",".","We","assume","a","few","things","about","how","these","rules","are","formatted",":","-","There","is","a","left-hand","side","(","LHS",")","and","a","right-hand","side","(","RHS",")",",","where","the","LHS","is","always","a","non-terminal",",","and","the","RHS","is","either","a","terminal",",","a","non-terminal",",","or","a","sequence","of","terminals","and\/or","non-terminals",".","-","The","LHS","and","the","RHS","are","joined","by","``","-",">","``",",","and","this","sequence","of","characters","appears","nowhere","else","in","the","rule",".","-","Non-terminal","sequences","in","the","RHS","are","formatted","as","``","[","NT1",",","NT2",",","...","]","''",".","-","Some","rules","come","from","a","global","grammar","used","for","a","whole","dataset",",","while","other","rules","are","specific","to","a","particular","``","Instance","``",".","We","do","n't","make","use","of","most","of","these","assumptions","in","this","class",",","but","the","code","that","consumes","this","``","Field","``","relies","heavily","on","them","in","some","places",".","If","the","given","rule","is","in","the","global","grammar",",","we","treat","the","rule","as","a","vocabulary","item","that","will","get","an","index","and","(","in","the","model",")","an","embedding",".","If","the","rule","is","not","in","the","global","grammar",",","we","do","not","create","a","vocabulary","item","from","the","rule",",","and","do","n't","produce","a","tensor","for","the","rule","-","we","assume","the","model","will","handle","representing","this","rule","in","some","other","way",".","Because","we","represent","global","grammar","rules","and","instance-specific","rules","differently",",","this","``","Field","``","does","not","lend","itself","well","to","batching","its","arrays",",","even","in","a","sequence","for","a","single","training","instance",".","A","model","using","this","field","will","have","to","manually","batch","together","rule","representations","after","splitting","apart","the","global","rules","from","the","``","Instance","``","rules",".","In","a","model",",","this","will","get","represented","as","a","``","ProductionRuleArray","``",",","which","is","defined","above","as","``","Tuple","[","str",",","bool",",","Optional","[","torch.Tensor","]","]","``",".","This","is","a","tuple","of","``","(","rule_string",",","is_global_rule",",","[","rule_id","]",")","``",",","where","the","``","rule_id","``","``","Tensor","``",",","if","present",",","will","have","shape","``","(","1",",",")","``",".","We","do","n't","do","any","batching","of","the","``","Tensors","``",",","so","this","gets","passed","to","``","Model.forward","(",")","``","as","a","``","List","[","ProductionRuleArray","]","``",".","We","pass","along","the","rule","string","because","there","is","n't","another","way","to","recover","it","for","instance-specific","rules","that","do","not","make","it","into","the","vocabulary",".","Parameters","--","--","--","--","--","rule",":","``","str","``","The","production","rule",",","formatted","as","described","above",".","If","this","field","is","just","padding",",","``","rule","``","will","be","the","empty","string",".","is_global_rule",":","``","bool","``","Whether","this","rule","comes","from","the","global","grammar","or","is","an","instance-specific","production","rule",".","vocab_namespace",":","``","str","``",",","optional","(","default=","''","rule_labels","''",")","The","vocabulary","namespace","to","use","for","the","global","production","rules",".","We","use","``","rule_labels","''","by","default",",","because","we","typically","do","not","want","padding","and","OOV","tokens","for","these",",","and","ending","the","namespace","with","``","labels","''","means","we","do","n't","get","padding","and","OOV","tokens","."]
code: def as_tensor(self, padding_lengths, cuda_device=-1):
      # pylint: disable=unused-argument
      if self.is_global_rule:
          tensor = torch.LongTensor([self._rule_id])
      else:
          tensor = None
      return (self.rule, self.is_global_rule, tensor)
  
file_path: magnitude/pymagnitude/third_party/allennlp/data/fields/production_rule_field.py
imports: from __future__ import absolute_import
  import torch
  from allennlp.data.fields.field import Field
  from allennlp.data.vocabulary import Vocabulary
label_desc: Use image node_id 5 for calling the ProductionRuleField obj's underlying member method code with example usage: obj.as_tensor(padding_lengths, cuda_device) and returns: self,  self,  tensor