class_docstr: Calculates the loss for each sequence up to its length t.
class_docstr_tok: ["Calculates","the","loss","for","each","sequence","up","to","its","length","t","."]
docstr: Formats targets into matrix indices.
  
  CRF scores contain per sentence, per token a (tagset_size x tagset_size) matrix, containing emission score for
  token j + transition prob from previous token i. Means, if we think of our rows as "to tag" and our columns
  as "from tag", the matrix in cell [10,5] would contain the emission score for tag 10 + transition score
  from previous tag 5 and could directly be addressed through the 1-dim indices (10 + tagset_size * 5) = 70,
  if our tagset consists of 12 tags.
  
  Args:
      targets: targets as in tag dictionary
      lengths: lengths of sentences in batch
docstr_tok: ["Formats","targets","into","matrix","indices",".","CRF","scores","contain","per","sentence",",","per","token","a","(","tagset_size","x","tagset_size",")","matrix",",","containing","emission","score","for","token","j","+","transition","prob","from","previous","token","i",".","Means",",","if","we","think","of","our","rows","as","``","to","tag","''","and","our","columns","as","``","from","tag","''",",","the","matrix","in","cell","[","10,5","]","would","contain","the","emission","score","for","tag","10","+","transition","score","from","previous","tag","5","and","could","directly","be","addressed","through","the","1-dim","indices","(","10","+","tagset_size","*","5",")","=","70",",","if","our","tagset","consists","of","12","tags",".","Args",":","targets",":","targets","as","in","tag","dictionary","lengths",":","lengths","of","sentences","in","batch"]
code: def _format_targets(
      self, targets: torch.Tensor, lengths: torch.IntTensor
  ):
      """Formats targets into matrix indices.
  
      CRF scores contain per sentence, per token a (tagset_size x tagset_size) matrix, containing emission score for
      token j + transition prob from previous token i. Means, if we think of our rows as "to tag" and our columns
      as "from tag", the matrix in cell [10,5] would contain the emission score for tag 10 + transition score
      from previous tag 5 and could directly be addressed through the 1-dim indices (10 + tagset_size * 5) = 70,
      if our tagset consists of 12 tags.
  
      Args:
          targets: targets as in tag dictionary
          lengths: lengths of sentences in batch
      """
      targets_per_sentence = []
  
      targets_list = targets.tolist()
      for cut in lengths:
          targets_per_sentence.append(targets_list[:cut])
          targets_list = targets_list[cut:]
  
      for t in targets_per_sentence:
          t += [self.tag_dictionary.get_idx_for_item(STOP_TAG)] * (
              int(lengths.max().item()) - len(t)
          )
  
      matrix_indices = [
          [
              self.tag_dictionary.get_idx_for_item(START_TAG)
              + (s[0] * self.tagset_size)
          ]
          + [
              s[i] + (s[i + 1] * self.tagset_size)
              for i in range(len(s) - 1)
          ]
          for s in targets_per_sentence
      ]
  
      return targets_per_sentence, matrix_indices
  
file_path: flair/flair/models/sequence_tagger_utils/viterbi.py
imports: from typing import Tuple
  import numpy
  import torch
  import torch.nn
  from torch.nn.functional import softmax
  from torch.nn.utils.rnn import pack_padded_sequence
  import flair
  from flair.data import Dictionary, Label, List, Sentence
label_desc: Use image node_id 4 for calling the ViterbiLoss obj's underlying member method code with example usage: obj._format_targets(targets, lengths) and returns: targets_per_sentence,  matrix_indices