class_docstr: Base class for the losses in order to combine commonly used methods.
class_docstr_tok: ["Base","class","for","the","losses","in","order","to","combine","commonly","used","methods","."]
docstr: Computes the loss for the given sample.
  
  This method uses the inheriting classes' `predictor_loss_function`.
  
  Args:
      model: Model to use for the loss computation.
      sample: Chosen sample as a dict with at least the following keys:
              'net_input', 'target', 'ntokens'
      reduce: Boolean flag to reduce the result to per batch elements.
      args, kwargs: Positional/Keyword arguments are passed through to the
                    `predictor_loss_function`.
  Returns:
      loss: Total loss as a Variable
      sample_size: Sample size - used for the gradient denominator.
      logging_output: Logging outputs to display during training.
  
  Raises:
      AssertionError:
          - prediction and target batch numbers are different
          - prediction shape is not [BATCH_SIZE, N_LABELS]
          - losses for translation and prediction are not the same shape
docstr_tok: ["Computes","the","loss","for","the","given","sample",".","This","method","uses","the","inheriting","classes","'","`","predictor_loss_function","`",".","Args",":","model",":","Model","to","use","for","the","loss","computation",".","sample",":","Chosen","sample","as","a","dict","with","at","least","the","following","keys",":","'net_input","'",",","'target","'",",","'ntokens'","reduce",":","Boolean","flag","to","reduce","the","result","to","per","batch","elements",".","args",",","kwargs",":","Positional\/Keyword","arguments","are","passed","through","to","the","`","predictor_loss_function","`",".","Returns",":","loss",":","Total","loss","as","a","Variable","sample_size",":","Sample","size","-","used","for","the","gradient","denominator",".","logging_output",":","Logging","outputs","to","display","during","training",".","Raises",":","AssertionError",":","-","prediction","and","target","batch","numbers","are","different","-","prediction","shape","is","not","[","BATCH_SIZE",",","N_LABELS","]","-","losses","for","translation","and","prediction","are","not","the","same","shape"]
code: def forward(self, model, sample, reduce=True, *args, **kwargs):
      """Computes the loss for the given sample.
  
      This method uses the inheriting classes' `predictor_loss_function`.
  
      Args:
          model: Model to use for the loss computation.
          sample: Chosen sample as a dict with at least the following keys:
                  'net_input', 'target', 'ntokens'
          reduce: Boolean flag to reduce the result to per batch elements.
          args, kwargs: Positional/Keyword arguments are passed through to the
                        `predictor_loss_function`.
      Returns:
          loss: Total loss as a Variable
          sample_size: Sample size - used for the gradient denominator.
          logging_output: Logging outputs to display during training.
  
      Raises:
          AssertionError:
              - prediction and target batch numbers are different
              - prediction shape is not [BATCH_SIZE, N_LABELS]
              - losses for translation and prediction are not the same shape
      """
      predictor_output, decoder_output = model(**sample["net_input"])
      # translation loss
      translation_loss, nll_loss = super().compute_loss(
          model, decoder_output, sample, reduce
      )
      prediction_target = model.get_target_words(sample)
      # predictor loss
      prediction_lprobs = model.get_predictor_normalized_probs(
          predictor_output, log_probs=True
      )
      prediction_lprobs = prediction_lprobs.view(
          -1, prediction_lprobs.size(-1)
      )
  
      assert prediction_lprobs.size(0) == prediction_target.size(0)
      assert prediction_lprobs.dim() == 2
  
      prediction_loss = self.predictor_loss_function(
          prediction_lprobs, prediction_target, *args, **kwargs
      )
  
      # prevent domination of padding idx
      non_pad_mask = prediction_target.ne(model.encoder.padding_idx)
      prediction_loss = prediction_loss[non_pad_mask]
  
      # TODO: normalize , sentence avg
      if reduce:
          prediction_loss = prediction_loss.sum()
      else:
          prediction_loss = prediction_loss.sum(
              1
          )  # loss per batch element
  
      assert translation_loss.size() == prediction_loss.size()
      loss = translation_loss + prediction_loss
  
      if self.sentence_avg:
          sample_size = sample["target"].size(0)
      else:
          sample_size = sample["ntokens"]
  
      logging_output = {
          "nll_loss": nll_loss,
          "translation_loss": translation_loss.data,
          "prediction_loss": prediction_loss.data,
          "ntokens": sample["ntokens"],
          "nsentences": sample["target"].size(0),
          "sample_size": sample_size,
      }
  
      if reduce:
          logging_output["translation_loss"] = utils.item(
              logging_output["translation_loss"]
          )
          logging_output["prediction_loss"] = utils.item(
              logging_output["prediction_loss"]
          )
          logging_output["nll_loss"] = utils.item(
              logging_output["nll_loss"]
          )
      logging_output["loss"] = utils.item(
          logging_output["translation_loss"]
      )
  
      return loss, sample_size, logging_output
  
imports: import abc
  import math
  import torch
  from fairseq import utils
  from fairseq.criterions import register_criterion
  from fairseq.criterions.label_smoothed_cross_entropy import LabelSmoothedCrossEntropyCriterion
  from pytorch_translate.utils import maybe_cuda
label_desc: Use image node_id 2 for calling the _BasePredictionCriterion obj's underlying member method code with example usage: obj.forward(model, sample, reduce) and returns: loss,  sample_size,  logging_output