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"]
code: def _compute_score(
      self,
      emissions: torch.Tensor,
      tags: torch.LongTensor,
      mask: torch.ByteTensor,
  ) -> torch.Tensor:
      # emissions: (seq_length, batch_size, num_tags)
      # tags: (seq_length, batch_size)
      # mask: (seq_length, batch_size)
      seq_length, batch_size = tags.shape
      mask = mask.float()
  
      # Start transition score and first emission
      # shape: (batch_size,)
      score = self.start_transitions[tags[0]]
      score += emissions[0, torch.arange(batch_size), tags[0]]
  
      for i in range(1, seq_length):
          # Transition score to next tag, only added if next timestep is valid (mask == 1)
          # shape: (batch_size,)
          score += self.transitions[tags[i - 1], tags[i]] * mask[i]
  
          # Emission score for next tag, only added if next timestep is valid (mask == 1)
          # shape: (batch_size,)
          score += (
              emissions[i, torch.arange(batch_size), tags[i]] * mask[i]
          )
  
      # End transition score
      # shape: (batch_size,)
      seq_ends = mask.long().sum(dim=0) - 1
      # shape: (batch_size,)
      last_tags = tags[seq_ends, torch.arange(batch_size)]
      # shape: (batch_size,)
      score += self.end_transitions[last_tags]
  
      return score
  
file_path: lightseq/lightseq/csrc/tests/cuda/torch_crf.py
imports: import torch
  import torch.nn
  from typing import List, Optional
label_desc: Use image node_id 7 for calling the CRF obj's underlying member method code with example usage: obj._compute_score(emissions, tags, mask) and returns: score