class_docstr: This module uses the "forward-backward" algorithm to compute
  the log-likelihood of its inputs assuming a conditional random field model.
  
  See, e.g. http://www.cs.columbia.edu/~mcollins/fb.pdf
  
  # Parameters
  
  num_tags : `int`, required
      The number of tags.
  constraints : `List[Tuple[int, int]]`, optional (default = `None`)
      An optional list of allowed transitions (from_tag_id, to_tag_id).
      These are applied to `viterbi_tags()` but do not affect `forward()`.
      These should be derived from `allowed_transitions` so that the
      start and end transitions are handled correctly for your tag type.
  include_start_end_transitions : `bool`, optional (default = `True`)
      Whether to include the start and end transition parameters.
class_docstr_tok: ["This","module","uses","the","``","forward-backward","''","algorithm","to","compute","the","log-likelihood","of","its","inputs","assuming","a","conditional","random","field","model",".","See",",","e.g",".","http",":","\/\/www.cs.columbia.edu\/~mcollins\/fb.pdf","#","Parameters","num_tags",":","`","int","`",",","required","The","number","of","tags",".","constraints",":","`","List","[","Tuple","[","int",",","int","]","]","`",",","optional","(","default","=","`","None","`",")","An","optional","list","of","allowed","transitions","(","from_tag_id",",","to_tag_id",")",".","These","are","applied","to","`","viterbi_tags","(",")","`","but","do","not","affect","`","forward","(",")","`",".","These","should","be","derived","from","`","allowed_transitions","`","so","that","the","start","and","end","transitions","are","handled","correctly","for","your","tag","type",".","include_start_end_transitions",":","`","bool","`",",","optional","(","default","=","`","True","`",")","Whether","to","include","the","start","and","end","transition","parameters","."]
code: def __init__(
      self,
      num_tags: int,
      constraints: List[Tuple[int, int]] = None,
      include_start_end_transitions: bool = True,
  ) -> None:
      super().__init__()
      self.num_tags = num_tags
  
      # transitions[i, j] is the logit for transitioning from state i to state j.
      self.transitions = torch.nn.Parameter(
          torch.empty(num_tags, num_tags)
      )
  
      # _constraint_mask indicates valid transitions (based on supplied constraints).
      # Include special start of sequence (num_tags + 1) and end of sequence tags (num_tags + 2)
      if constraints is None:
          # All transitions are valid.
          constraint_mask = torch.full(
              (num_tags + 2, num_tags + 2), 1.0
          )
      else:
          constraint_mask = torch.full(
              (num_tags + 2, num_tags + 2), 0.0
          )
          for i, j in constraints:
              constraint_mask[i, j] = 1.0
  
      self._constraint_mask = torch.nn.Parameter(
          constraint_mask, requires_grad=False
      )
  
      # Also need logits for transitioning from "start" state and to "end" state.
      self.include_start_end_transitions = include_start_end_transitions
      if include_start_end_transitions:
          self.start_transitions = torch.nn.Parameter(
              torch.Tensor(num_tags)
          )
          self.end_transitions = torch.nn.Parameter(
              torch.Tensor(num_tags)
          )
  
      self.reset_parameters()
  
file_path: allennlp/allennlp/modules/conditional_random_field/conditional_random_field.py
imports: from typing import List, Tuple, Dict, Union
  import torch
  from allennlp.common.checks import ConfigurationError
  import allennlp.nn.util
label_desc: Use image node_id 1 to create a new ConditionalRandomField object from inherited base classes: torch.nn with example: obj = ConditionalRandomField(num_tags, constraints, include_start_end_transitions)