class_docstr: This class calculates Viterbi alignments in the forward method.
  
  It also records alignments and creates batches of them for use
  in Viterbi training.
  
  Arguments
  ---------
  states_per_phoneme : int
      Number of hidden states to use per phoneme.
  output_folder : str
      It is the folder that the alignments will be stored in when
      saved to disk. Not yet implemented.
  neg_inf : float
      The float used to represent a negative infinite log probability.
      Using `-float("Inf")` tends to give numerical instability.
      A number more negative than -1e5 also sometimes gave errors when
      the `genbmm` library was used (currently not in use). (default: -1e5)
  batch_reduction : string
      One of "none", "sum" or "mean".
      What kind of batch-level reduction to apply to the loss calculated
      in the forward method.
  input_len_norm : bool
      Whether to normalize the loss in the forward method by the length of
      the inputs.
  target_len_norm : bool
      Whether to normalize the loss in the forward method by the length of
      the targets.
  lexicon_path : string
      The location of the lexicon.
  
  Example
  -------
  >>> log_posteriors = torch.tensor([[[ -1., -10., -10.],
  ...                                 [-10.,  -1., -10.],
  ...                                 [-10., -10.,  -1.]],
  ...
  ...                                [[ -1., -10., -10.],
  ...                                 [-10.,  -1., -10.],
  ...                                 [-10., -10., -10.]]])
  >>> lens = torch.tensor([1., 0.66])
  >>> phns = torch.tensor([[0, 1, 2],
  ...                      [0, 1, 0]])
  >>> phn_lens = torch.tensor([1., 0.66])
  >>> aligner = HMMAligner()
  >>> forward_scores = aligner(
  ...        log_posteriors, lens, phns, phn_lens, 'forward'
  ... )
  >>> forward_scores.shape
  torch.Size([2])
  >>> viterbi_scores, alignments = aligner(
  ...        log_posteriors, lens, phns, phn_lens, 'viterbi'
  ... )
  >>> alignments
  [[0, 1, 2], [0, 1]]
  >>> viterbi_scores.shape
  torch.Size([2])
docstr: Calculates mean accuracy between predicted alignments and ground truth
  alignments. Ground truth alignments are derived from ground truth phns
  and their ends in the audio sample.
  
  Arguments
  ---------
  alignments : list of lists of ints/floats
      The predicted alignments for each utterance in the batch.
  ends : list of lists of ints
      A list of lists of sample indices where each ground truth phoneme
      ends, according to the transcription.
      Note: current implementation assumes that 'ends' mark the index
      where the next phoneme begins.
  phns : list of lists of ints/floats
      The unpadded list of lists of ground truth phonemes in the batch.
  ind2labs : tuple
      (Optional)
      Contains the original index-to-label dicts for the first and second
      sequence of phonemes.
  
  Returns
  -------
  mean_acc : float
      The mean percentage of times that the upsampled predicted alignment
      matches the ground truth alignment.
  
  Example
  -------
  >>> aligner = HMMAligner()
  >>> alignments = [[0., 0., 0., 1.]]
  >>> phns = [[0., 1.]]
  >>> ends = [[2, 4]]
  >>> mean_acc = aligner.calc_accuracy(alignments, ends, phns)
  >>> mean_acc.item()
  75.0
docstr_tok: ["Calculates","mean","accuracy","between","predicted","alignments","and","ground","truth","alignments",".","Ground","truth","alignments","are","derived","from","ground","truth","phns","and","their","ends","in","the","audio","sample",".","Arguments","--","--","--","--","-","alignments",":","list","of","lists","of","ints\/floats","The","predicted","alignments","for","each","utterance","in","the","batch",".","ends",":","list","of","lists","of","ints","A","list","of","lists","of","sample","indices","where","each","ground","truth","phoneme","ends",",","according","to","the","transcription",".","Note",":","current","implementation","assumes","that","'ends","'","mark","the","index","where","the","next","phoneme","begins",".","phns",":","list","of","lists","of","ints\/floats","The","unpadded","list","of","lists","of","ground","truth","phonemes","in","the","batch",".","ind2labs",":","tuple","(","Optional",")","Contains","the","original","index-to-label","dicts","for","the","first","and","second","sequence","of","phonemes",".","Returns","--","--","--","-","mean_acc",":","float","The","mean","percentage","of","times","that","the","upsampled","predicted","alignment","matches","the","ground","truth","alignment",".","Example","--","--","--","-",">",">",">","aligner","=","HMMAligner","(",")",">",">",">","alignments","=","[","[","0.",",","0.",",","0.",",","1",".","]","]",">",">",">","phns","=","[","[","0.",",","1",".","]","]",">",">",">","ends","=","[","[","2",",","4","]","]",">",">",">","mean_acc","=","aligner.calc_accuracy","(","alignments",",","ends",",","phns",")",">",">",">","mean_acc.item","(",")","75.0"]
code: def calc_accuracy(self, alignments, ends, phns, ind2labs=None):
      """Calculates mean accuracy between predicted alignments and ground truth
      alignments. Ground truth alignments are derived from ground truth phns
      and their ends in the audio sample.
  
      Arguments
      ---------
      alignments : list of lists of ints/floats
          The predicted alignments for each utterance in the batch.
      ends : list of lists of ints
          A list of lists of sample indices where each ground truth phoneme
          ends, according to the transcription.
          Note: current implementation assumes that 'ends' mark the index
          where the next phoneme begins.
      phns : list of lists of ints/floats
          The unpadded list of lists of ground truth phonemes in the batch.
      ind2labs : tuple
          (Optional)
          Contains the original index-to-label dicts for the first and second
          sequence of phonemes.
  
      Returns
      -------
      mean_acc : float
          The mean percentage of times that the upsampled predicted alignment
          matches the ground truth alignment.
  
      Example
      -------
      >>> aligner = HMMAligner()
      >>> alignments = [[0., 0., 0., 1.]]
      >>> phns = [[0., 1.]]
      >>> ends = [[2, 4]]
      >>> mean_acc = aligner.calc_accuracy(alignments, ends, phns)
      >>> mean_acc.item()
      75.0
      """
      acc_hist = []
  
      # Do conversion if states_per_phoneme > 1
      if self.states_per_phoneme > 1:
          alignments = [
              [i // self.states_per_phoneme for i in utt]
              for utt in alignments
          ]
  
      # convert to common alphabet if need be
      if ind2labs is not None:
          alignments, phns = map_inds_to_intersect(
              alignments, phns, ind2labs
          )
  
      for alignments_, ends_, phns_ in zip(alignments, ends, phns):
          acc = self._calc_accuracy_sent(alignments_, ends_, phns_)
          acc_hist.append(acc)
  
      acc_hist = torch.tensor(acc_hist)
      mean_acc = acc_hist.mean()
  
      return mean_acc.unsqueeze(0)
  
imports: import torch
  import random
  from speechbrain.utils.checkpoints import register_checkpoint_hooks
  from speechbrain.utils.checkpoints import mark_as_saver
  from speechbrain.utils.checkpoints import mark_as_loader
  from speechbrain.utils.data_utils import undo_padding
label_desc: Use image node_id 17 for calling the HMMAligner obj's underlying member method code with example usage: obj.calc_accuracy(alignments, ends, phns, ind2labs) and returns: mean_acc