docstr: Computes the loss (CTC+NLL) given predictions and targets.
  
  
  Arguments
  ---------
  predictions: G2PPredictions
      the predictions (as computed by compute_forward)
  batch: PaddedBatch
      a raw G2P data batch
  stage: speechbrain.Stage
      the training stage
docstr_tok: ["Computes","the","loss","(","CTC+NLL",")","given","predictions","and","targets",".","Arguments","--","--","--","--","-","predictions",":","G2PPredictions","the","predictions","(","as","computed","by","compute_forward",")","batch",":","PaddedBatch","a","raw","G2P","data","batch","stage",":","speechbrain.Stage","the","training","stage"]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss (CTC+NLL) given predictions and targets.
  
  
      Arguments
      ---------
      predictions: G2PPredictions
          the predictions (as computed by compute_forward)
      batch: PaddedBatch
          a raw G2P data batch
      stage: speechbrain.Stage
          the training stage
      """
      phns_eos, phn_lens_eos = batch.phn_encoded_eos
      phns, phn_lens = batch.phn_encoded
      loss_seq = self.hparams.seq_cost(
          predictions.p_seq, phns_eos, phn_lens_eos
      )
      if self.is_ctc_active(stage):
          seq_weight = 1 - self.hparams.ctc_weight
          loss_ctc = self.hparams.ctc_cost(
              predictions.ctc_logprobs,
              phns_eos,
              predictions.char_lens,
              phn_lens_eos,
          )
          loss = (
              seq_weight * loss_seq + self.hparams.ctc_weight * loss_ctc
          )
      else:
          loss = loss_seq
      if self.mode == TrainMode.HOMOGRAPH:
          # When tokenization is used, the length of the words
          # in the original phoneme space is not equal to the tokenized
          # words but the raw data only supplies non-tokenized information
          phns_base, phn_lens_base = (
              batch.phn_raw_encoded
              if self.hparams.phn_tokenize
              else (None, None)
          )
          homograph_loss = (
              self.hparams.homograph_loss_weight
              * self.hparams.homograph_cost(
                  phns=phns,
                  phn_lens=phn_lens,
                  p_seq=predictions.p_seq,
                  subsequence_phn_start=batch.homograph_phn_start,
                  subsequence_phn_end=batch.homograph_phn_end,
                  phns_base=phns_base,
                  phn_lens_base=phn_lens_base,
              )
          )
          loss += homograph_loss
  
      # Record losses for posterity
      if self.hparams.enable_metrics and stage != sb.Stage.TRAIN:
          self._add_sequence_metrics(predictions, batch)
          if self.mode == TrainMode.HOMOGRAPH:
              self._add_homograph_metrics(predictions, batch)
  
      return loss
  
imports: from speechbrain.dataio.dataset import FilteredSortedDynamicItemDataset, DynamicItemDataset
  from speechbrain.dataio.sampler import BalancingDataSampler
  from speechbrain.utils.data_utils import undo_padding
  import datasets
  import logging
  import os
  import random
  import speechbrain
  import sys
  from enum import Enum
  from collections import namedtuple
  from hyperpyyaml import load_hyperpyyaml
  from functools import partial
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.utils.pretrained import save_for_pretrained
  from speechbrain.lobes.models.g2p.dataio import enable_eos_bos, grapheme_pipeline, phoneme_pipeline, tokenizer_encode_pipeline, add_bos_eos, get_sequence_key, phonemes_to_label
  from speechbrain.dataio.wer import print_alignments
  from speechbrain.wordemb.util import expand_to_chars
  from io import StringIO
  from speechbrain.utils import hpopt
  import numpy
label_desc: Use image node_id 6 for calling the G2PBrain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss