docstr: Computes the loss (CTC) given predictions and targets.
docstr_tok: ["Computes","the","loss","(","CTC",")","given","predictions","and","targets","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss (CTC) given predictions and targets."""
  
      # Get data.
      batch = batch.to(self.device)
      chars, char_lens = batch.char_encoded
      ids = batch.id
  
      # Get predictions & loss.
      p_ctc, wav_lens = predictions
      loss = self.hparams.ctc_cost(p_ctc, chars, wav_lens, char_lens)
  
      # Get metrics.
      if stage != sb.Stage.TRAIN:
          # Generate sequences with CTC greedy decoder.
          sequence = sb.decoders.ctc_greedy_decode(
              p_ctc, wav_lens, self.hparams.blank_index
          )
          # Update metrics.
          self.cer_metric.append(
              ids=ids,
              predict=sequence,
              target=chars,
              target_len=char_lens,
              ind2lab=self.tokenizer.decode_ndim,
          )
          self.coer_metric.append(
              ids=ids,
              predict=sequence,
              target=chars,
              target_len=char_lens,
              ind2lab=self.tokenizer.decode_ndim,
          )
          self.cver_metric.append(
              ids=ids,
              predict=sequence,
              target=chars,
              target_len=char_lens,
              ind2lab=self.tokenizer.decode_ndim,
          )
          self.ctc_metric.append(ids, p_ctc, chars, wav_lens, char_lens)
  
      return loss
  
file_path: speechbrain/recipes/MEDIA/SLU/CTC/train_hf_wav2vec.py
imports: import sys
  import torch
  import logging
  import speechbrain
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.dataio.batch import PaddedBatch
  from speechbrain.utils.distributed import run_on_main
  from media_prepare import prepare_media
label_desc: Use image node_id 2 for calling the SLU obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss