docstr: Overrides compute_loss for multi-objective Director loss computation.
docstr_tok: ["Overrides","compute_loss","for","multi-objective","Director","loss","computation","."]
code: def compute_loss(self, batch, return_output=False):
      """
      Overrides compute_loss for multi-objective Director loss computation.
      """
      if batch.label_vec is None:
          raise ValueError("Cannot compute loss without a label.")
  
      model_output = self.model(
          *self._model_input(batch), ys=batch.label_vec
      )
      generator_scores, _, classifier_scores, *_ = model_output
  
      (
          generator_losses,
          generator_num_target_tokens,
      ) = self.compute_generator_loss(generator_scores, batch)
      (
          classifier_losses,
          classifier_num_target_tokens,
      ) = self.compute_classifier_loss(classifier_scores, batch)
  
      losses = generator_losses + self.train_gamma * classifier_losses
  
      num_target_tokens = (
          generator_num_target_tokens + classifier_num_target_tokens
      )
  
      self.record_local_metric(
          "loss", AverageMetric.many(losses, num_target_tokens)
      )
  
      # This unweighted_loss ignores mixing weights and weighs
      # generator and classifier losses equally. This can be
      # used to do validation across various mixing coeffs (train_gamma).
      self.record_local_metric(
          "unweighted_loss",
          AverageMetric.many(
              (generator_losses + classifier_losses), num_target_tokens
          ),
      )
  
      loss = (losses / num_target_tokens).sum()
  
      if return_output:
          return (loss, model_output)
      return loss
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from typing import Optional, Any, Dict, Tuple, Union
  from parlai.agents.transformer.modules import TransformerGeneratorModel
  from parlai.agents.transformer.transformer import TransformerGeneratorAgent
  from parlai.core.dict import DictionaryAgent
  from parlai.core.message import Message
  from parlai.core.metrics import AverageMetric
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
  from parlai.core.torch_generator_agent import PPLMetric
  import parlai.utils.logging
label_desc: Use image node_id 12 for calling the DirectorAgent obj's underlying member method code with example usage: obj.compute_loss(batch, return_output) and returns: loss, loss,  model_output