class_docstr: Conditional random field.
  This module implements a conditional random field [LMP01]_. The forward computation
  of this class computes the log likelihood of the given sequence of tags and
  emission score tensor. This class also has `~CRF.decode` method which finds
  the best tag sequence given an emission score tensor using `Viterbi algorithm`_.
  Args:
      num_tags: Number of tags.
      batch_first: Whether the first dimension corresponds to the size of a minibatch.
  Attributes:
      start_transitions (`~torch.nn.Parameter`): Start transition score tensor of size
          ``(num_tags,)``.
      end_transitions (`~torch.nn.Parameter`): End transition score tensor of size
          ``(num_tags,)``.
      transitions (`~torch.nn.Parameter`): Transition score tensor of size
          ``(num_tags, num_tags)``.
  .. [LMP01] Lafferty, J., McCallum, A., Pereira, F. (2001).
     "Conditional random fields: Probabilistic models for segmenting and
     labeling sequence data". *Proc. 18th International Conf. on Machine
     Learning*. Morgan Kaufmann. pp. 282–289.
  .. _Viterbi algorithm: https://en.wikipedia.org/wiki/Viterbi_algorithm
docstr: Compute the conditional log likelihood of a sequence of tags given emission scores.
  Args:
      emissions (`~torch.Tensor`): Emission score tensor of size
          ``(seq_length, batch_size, num_tags)`` if ``batch_first`` is ``False``,
          ``(batch_size, seq_length, num_tags)`` otherwise.
      tags (`~torch.LongTensor`): Sequence of tags tensor of size
          ``(seq_length, batch_size)`` if ``batch_first`` is ``False``,
          ``(batch_size, seq_length)`` otherwise.
      mask (`~torch.ByteTensor`): Mask tensor of size ``(seq_length, batch_size)``
          if ``batch_first`` is ``False``, ``(batch_size, seq_length)`` otherwise.
      reduction: Specifies  the reduction to apply to the output:
          ``none|sum|mean|token_mean``. ``none``: no reduction will be applied.
          ``sum``: the output will be summed over batches. ``mean``: the output will be
          averaged over batches. ``token_mean``: the output will be averaged over tokens.
  Returns:
      `~torch.Tensor`: The log likelihood. This will have size ``(batch_size,)`` if
      reduction is ``none``, ``()`` otherwise.
docstr_tok: ["Compute","the","conditional","log","likelihood","of","a","sequence","of","tags","given","emission","scores",".","Args",":","emissions","(","`","~torch.Tensor","`",")",":","Emission","score","tensor","of","size","``","(","seq_length",",","batch_size",",","num_tags",")","``","if","``","batch_first","``","is","``","False","``",",","``","(","batch_size",",","seq_length",",","num_tags",")","``","otherwise",".","tags","(","`","~torch.LongTensor","`",")",":","Sequence","of","tags","tensor","of","size","``","(","seq_length",",","batch_size",")","``","if","``","batch_first","``","is","``","False","``",",","``","(","batch_size",",","seq_length",")","``","otherwise",".","mask","(","`","~torch.ByteTensor","`",")",":","Mask","tensor","of","size","``","(","seq_length",",","batch_size",")","``","if","``","batch_first","``","is","``","False","``",",","``","(","batch_size",",","seq_length",")","``","otherwise",".","reduction",":","Specifies","the","reduction","to","apply","to","the","output",":","``","none|sum|mean|token_mean","``",".","``","none","``",":","no","reduction","will","be","applied",".","``","sum","``",":","the","output","will","be","summed","over","batches",".","``","mean","``",":","the","output","will","be","averaged","over","batches",".","``","token_mean","``",":","the","output","will","be","averaged","over","tokens",".","Returns",":","`","~torch.Tensor","`",":","The","log","likelihood",".","This","will","have","size","``","(","batch_size",",",")","``","if","reduction","is","``","none","``",",","``","(",")","``","otherwise","."]
code: def forward(
      self,
      emissions: torch.Tensor,
      tags: torch.LongTensor,
      mask: Optional[torch.ByteTensor] = None,
      reduction: str = "sum",
  ) -> torch.Tensor:
      """Compute the conditional log likelihood of a sequence of tags given emission scores.
      Args:
          emissions (`~torch.Tensor`): Emission score tensor of size
              ``(seq_length, batch_size, num_tags)`` if ``batch_first`` is ``False``,
              ``(batch_size, seq_length, num_tags)`` otherwise.
          tags (`~torch.LongTensor`): Sequence of tags tensor of size
              ``(seq_length, batch_size)`` if ``batch_first`` is ``False``,
              ``(batch_size, seq_length)`` otherwise.
          mask (`~torch.ByteTensor`): Mask tensor of size ``(seq_length, batch_size)``
              if ``batch_first`` is ``False``, ``(batch_size, seq_length)`` otherwise.
          reduction: Specifies  the reduction to apply to the output:
              ``none|sum|mean|token_mean``. ``none``: no reduction will be applied.
              ``sum``: the output will be summed over batches. ``mean``: the output will be
              averaged over batches. ``token_mean``: the output will be averaged over tokens.
      Returns:
          `~torch.Tensor`: The log likelihood. This will have size ``(batch_size,)`` if
          reduction is ``none``, ``()`` otherwise.
      """
      self._validate(emissions, tags=tags, mask=mask)
      if reduction not in ("none", "sum", "mean", "token_mean"):
          raise ValueError(f"invalid reduction: {reduction}")
      if mask is None:
          mask = torch.ones_like(tags, dtype=torch.uint8)
  
      if self.batch_first:
          emissions = emissions.transpose(0, 1)
          tags = tags.transpose(0, 1)
          mask = mask.transpose(0, 1)
  
      # shape: (batch_size,)
      numerator = self._compute_score(emissions, tags, mask)
      # shape: (batch_size,)
      denominator = self._compute_normalizer(emissions, mask)
      # shape: (batch_size,)
      llh = numerator - denominator
  
      if reduction == "none":
          return llh
      if reduction == "sum":
          return llh.sum()
      if reduction == "mean":
          return llh.mean()
      assert reduction == "token_mean"
      return llh.sum() / mask.float().sum()
  
file_path: nlp-architect/nlp_architect/nn/torch/layers/crf.py
imports: from typing import List, Optional
  import torch
  import torch.nn
label_desc: Use image node_id 4 for calling the CRF obj's underlying member method code with example usage: obj.forward(emissions, tags, mask, reduction) and returns: unknown, llh, llh, llh