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
class_docstr_tok: ["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\u2013289",".","..","_Viterbi","algorithm",":","https",":","\/\/en.wikipedia.org\/wiki\/Viterbi_algorithm"]
docstr: Find the most likely tag sequence using Viterbi algorithm.
  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.
      mask (`~torch.ByteTensor`): Mask tensor of size ``(seq_length, batch_size)``
          if ``batch_first`` is ``False``, ``(batch_size, seq_length)`` otherwise.
      nbest (`int`): Number of most probable paths for each sequence
      pad_tag (`int`): Tag at padded positions. Often input varies in length and
          the length will be padded to the maximum length in the batch. Tags at
          the padded positions will be assigned with a padding tag, i.e. `pad_tag`
  Returns:
      A PyTorch tensor of the best tag sequence for each batch of shape
      (nbest, batch_size, seq_length)
docstr_tok: ["Find","the","most","likely","tag","sequence","using","Viterbi","algorithm",".","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",".","mask","(","`","~torch.ByteTensor","`",")",":","Mask","tensor","of","size","``","(","seq_length",",","batch_size",")","``","if","``","batch_first","``","is","``","False","``",",","``","(","batch_size",",","seq_length",")","``","otherwise",".","nbest","(","`","int","`",")",":","Number","of","most","probable","paths","for","each","sequence","pad_tag","(","`","int","`",")",":","Tag","at","padded","positions",".","Often","input","varies","in","length","and","the","length","will","be","padded","to","the","maximum","length","in","the","batch",".","Tags","at","the","padded","positions","will","be","assigned","with","a","padding","tag",",","i.e",".","`","pad_tag","`","Returns",":","A","PyTorch","tensor","of","the","best","tag","sequence","for","each","batch","of","shape","(","nbest",",","batch_size",",","seq_length",")"]
code: def decode(
      self,
      emissions: torch.Tensor,
      mask: Optional[torch.ByteTensor] = None,
      nbest: Optional[int] = None,
      pad_tag: Optional[int] = None,
  ) -> List[List[List[int]]]:
      """Find the most likely tag sequence using Viterbi algorithm.
      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.
          mask (`~torch.ByteTensor`): Mask tensor of size ``(seq_length, batch_size)``
              if ``batch_first`` is ``False``, ``(batch_size, seq_length)`` otherwise.
          nbest (`int`): Number of most probable paths for each sequence
          pad_tag (`int`): Tag at padded positions. Often input varies in length and
              the length will be padded to the maximum length in the batch. Tags at
              the padded positions will be assigned with a padding tag, i.e. `pad_tag`
      Returns:
          A PyTorch tensor of the best tag sequence for each batch of shape
          (nbest, batch_size, seq_length)
      """
      if nbest is None:
          nbest = 1
      if mask is None:
          mask = torch.ones(
              emissions.shape[:2],
              dtype=torch.uint8,
              device=emissions.device,
          )
      if mask.dtype != torch.uint8:
          mask = mask.byte()
      self._validate(emissions, mask=mask)
  
      if self.batch_first:
          emissions = emissions.transpose(0, 1)
          mask = mask.transpose(0, 1)
  
      if nbest == 1:
          best_tags, best_score = self._viterbi_decode(
              emissions, mask, pad_tag
          )
          return best_tags.unsqueeze(0), best_score
      return self._viterbi_decode_nbest(emissions, mask, nbest, pad_tag)
  
file_path: lightseq/tests/torch_crf.py
imports: import torch
  import torch.nn
  from typing import List, Optional
label_desc: Use image node_id 5 for calling the CRF obj's underlying member method code with example usage: obj.decode(emissions, mask, nbest, pad_tag) and returns: self, best_tags,  best_score