docstr: Computes the loss (NLL) given predictions and targets.
docstr_tok: ["Computes","the","loss","(","NLL",")","given","predictions","and","targets","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss (NLL) given predictions and targets."""
  
      if (
          stage == sb.Stage.TRAIN
          and self.step % show_results_every != 0
      ):
          p_seq, transcript_tokens_lens = predictions
      else:
          p_seq, transcript_tokens_lens, predicted_tokens = predictions
  
      ids = batch.id
      (
          semantics_tokens_eos,
          semantics_tokens_eos_lens,
      ) = batch.semantics_tokens_eos
      semantics_tokens, semantics_tokens_lens = batch.semantics_tokens
  
      loss_seq = self.hparams.seq_cost(
          p_seq, semantics_tokens_eos, length=semantics_tokens_eos_lens
      )
  
      # (No ctc loss)
      loss = loss_seq
  
      if (stage != sb.Stage.TRAIN) or (
          self.step % show_results_every == 0
      ):
          # Decode token terms to words
          predicted_semantics = [
              slu_tokenizer.decode_ids(utt_seq).split(" ")
              for utt_seq in predicted_tokens
          ]
  
          target_semantics = [wrd.split(" ") for wrd in batch.semantics]
  
          self.log_outputs(predicted_semantics, target_semantics)
  
          if stage != sb.Stage.TRAIN:
              self.wer_metric.append(
                  ids, predicted_semantics, target_semantics
              )
              self.cer_metric.append(
                  ids, predicted_semantics, target_semantics
              )
  
          if stage == sb.Stage.TEST:
              # write to "predictions.jsonl"
              with jsonlines.open(
                  hparams["output_folder"] + "/predictions.jsonl",
                  mode="a",
              ) as writer:
                  for i in range(len(predicted_semantics)):
                      try:
                          dict = ast.literal_eval(
                              " ".join(predicted_semantics[i]).replace(
                                  "|", ","
                              )
                          )
                      except (
                          SyntaxError
                      ):  # need this if the output is not a valid dictionary
                          dict = {
                              "scenario": "none",
                              "action": "none",
                              "entities": [],
                          }
                      dict["file"] = id_to_file[ids[i]]
                      writer.write(dict)
  
      return loss
  
file_path: speechbrain/recipes/SLURP/NLU/train.py
imports: import sys
  import torch
  import speechbrain
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.distributed import run_on_main, if_main_process
  import jsonlines
  import ast
  import pandas
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