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","."]
docstr: Computes the log likelihood for the given batch of input sequences $(x,y)$
  
  Args:
      inputs (torch.Tensor): (batch_size, sequence_length, num_tags) tensor of logits for the inputs $x$
      tags (torch.Tensor): (batch_size, sequence_length) tensor of tags $y$
      mask (torch.BoolTensor, optional): (batch_size, sequence_length) tensor of masking flags.
          Defaults to None.
  
  Returns:
      torch.Tensor: (batch_size,) log likelihoods $log P(y|x)$ for each input
docstr_tok: ["Computes","the","log","likelihood","for","the","given","batch","of","input","sequences","$","(","x",",","y",")","$","Args",":","inputs","(","torch.Tensor",")",":","(","batch_size",",","sequence_length",",","num_tags",")","tensor","of","logits","for","the","inputs","$","x","$","tags","(","torch.Tensor",")",":","(","batch_size",",","sequence_length",")","tensor","of","tags","$","y","$","mask","(","torch.BoolTensor",",","optional",")",":","(","batch_size",",","sequence_length",")","tensor","of","masking","flags",".","Defaults","to","None",".","Returns",":","torch.Tensor",":","(","batch_size",",",")","log","likelihoods","$","log","P","(","y|x",")","$","for","each","input"]
code: def forward(
      self,
      inputs: torch.Tensor,
      tags: torch.Tensor,
      mask: torch.BoolTensor = None,
  ) -> torch.Tensor:
      """Computes the log likelihood for the given batch of input sequences $(x,y)$
  
      Args:
          inputs (torch.Tensor): (batch_size, sequence_length, num_tags) tensor of logits for the inputs $x$
          tags (torch.Tensor): (batch_size, sequence_length) tensor of tags $y$
          mask (torch.BoolTensor, optional): (batch_size, sequence_length) tensor of masking flags.
              Defaults to None.
  
      Returns:
          torch.Tensor: (batch_size,) log likelihoods $log P(y|x)$ for each input
      """
      if mask is None:
          mask = torch.ones(
              *tags.size(), dtype=torch.bool, device=inputs.device
          )
      else:
          # The code below fails in weird ways if this isn't a bool tensor, so we make sure.
          mask = mask.to(torch.bool)
  
      log_denominator = self._input_likelihood(
          inputs, self.transitions, mask
      )
      log_numerator = self._joint_likelihood(
          inputs, self.transitions, tags, mask
      )
  
      return torch.sum(log_numerator - log_denominator)
  
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 5 for calling the ConditionalRandomField obj's underlying member method code with example usage: obj.forward(inputs, tags, mask) and returns: torch