docstr: Samples negatives, computes contrastive loss and accuracy.
          
docstr_tok: ["Samples","negatives",",","computes","contrastive","loss","and","accuracy","."]
code: def compute_objectives(self, forward_outputs, batch, stage):
      """Samples negatives, computes contrastive loss and accuracy."""
  
      embeddings = forward_outputs["embeddings"]
      targets = forward_outputs["targets"]
  
      negs = sample_negatives(targets, self.hparams.num_negatives)
  
      loss, accuracy = self.hparams.loss(embeddings, targets, negs)
  
      # This is only used for logging purpose
      if stage != sb.Stage.TRAIN:
          self.acc_metric.append(accuracy)
  
      objectives = {
          "loss": loss,
          "accuracy": accuracy,
          "num_masked": forward_outputs["num_masked"],
          "ratio_masked": forward_outputs["ratio_masked"],
      }
      if (
          "diversity_loss" in forward_outputs
      ):  # only quantised model has these
          objectives.update(
              {
                  "diversity_loss": forward_outputs["diversity_loss"],
                  "prob_perplex": forward_outputs["prob_perplex"],
                  "code_perplex": forward_outputs["code_perplex"],
                  "num_vars": forward_outputs["num_vars"],
                  "temp": forward_outputs["temp"],
              }
          )
  
      # Compute the loss given the original equation from the paper
      loss = objectives["loss"]
      if self.hparams.diversity_loss_weight == 0.0:
          objectives["backprop_loss"] = loss
      else:
          objectives["backprop_loss"] = (
              loss
              + objectives["diversity_loss"]
              * self.hparams.diversity_loss_weight
              * objectives["num_masked"]
          )
      return objectives
  
imports: import logging
  import sys
  import time
  from functools import partial
  import speechbrain
  import torch
  import torch.nn.functional
  from torch.nn.parallel import DistributedDataParallel
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain import Stage
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.dataio.dataloader import SaveableDataLoader
  from speechbrain.dataio.sampler import DynamicBatchSampler
  from speechbrain.lobes.models.wav2vec import w2v_mask_collate_fn
  from speechbrain.lobes.models.wav2vec import sample_negatives
  from speechbrain.core import AMPConfig
label_desc: Use image node_id 2 for calling the W2V2Brain obj's underlying member method code with example usage: obj.compute_objectives(forward_outputs, batch, stage) and returns: objectives