code: def compute_generator_loss(self, generator_scores, batch):
      bsz = batch.batchsize
      device = generator_scores.device
  
      generator_losses = torch.zeros((bsz,), device=device)
      num_target_tokens = torch.zeros(
          (bsz,), device=device, dtype=torch.long
      )
  
      generation_idxs = batch.train_generator[:, 0]
      self.record_local_metric(
          "pct_generator_exs", AverageMetric.many(generation_idxs)
      )
  
      # If there are no generation exs in the batch,
      # returrn zero generator loss.
      if not torch.any(generation_idxs):
          return generator_losses, num_target_tokens
  
      # Copied verbatim from TGA.compute_loss.
      generator_scores = generator_scores[generation_idxs]
      generator_label_vec = batch.label_vec[generation_idxs]
  
      generator_scores_view = generator_scores.reshape(
          -1, generator_scores.size(-1)
      )
      generator_losses = self.criterion(
          generator_scores_view, generator_label_vec.view(-1)
      )
  
      # cross entropy loss
      generator_losses = generator_losses.view(
          generator_scores.shape[:-1]
      ).sum(dim=1)
  
      notnull = generator_label_vec.ne(self.NULL_IDX)
      num_target_tokens = notnull.long().sum(dim=-1)
  
      (
          reshaped_generator_losses,
          num_target_tokens_reshaped,
      ) = self._reshape_to_record_metrics(
          batch, generator_losses, num_target_tokens, generation_idxs
      )
  
      # save loss to metrics
      self.record_local_metric(
          "generator_loss",
          AverageMetric.many(
              reshaped_generator_losses, num_target_tokens_reshaped
          ),
      )
  
      #  save perplexity to metrics
      self.record_local_metric(
          "generator_ppl",
          PPLMetric.many(
              reshaped_generator_losses, num_target_tokens_reshaped
          ),
      )
  
      return reshaped_generator_losses, num_target_tokens_reshaped
  
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 11 for calling the DirectorAgent obj's underlying member method code with example usage: obj.compute_generator_loss(generator_scores, batch) and returns: reshaped_generator_losses,  num_target_tokens_reshaped, generator_losses,  num_target_tokens