class_docstr: Base class for criteria with need to run beam search.
class_docstr_tok: ["Base","class","for","criteria","with","need","to","run","beam","search","."]
docstr: Compute negative log-likelihoods for the translations.
  
  This function computes p(u|x) for each element in U(x) (see Edunov et
  al., 2017 for notation) for computing gradients.
  
  Args:
      model: FairseqModel to use (passed via FairseqCriterion.forward())
      sample: Training batch (passed via FairseqCriterion.forward())
      translations: A [batch_size, beam_size, max_trg_len] int tensor of
          generated translation (as produced by generate_translations())
  
  Returns:
      A [bsz, beam_size] float tensor containing the negative loglikelihoods
      of the sentences in `translations`.
docstr_tok: ["Compute","negative","log-likelihoods","for","the","translations",".","This","function","computes","p","(","u|x",")","for","each","element","in","U","(","x",")","(","see","Edunov","et","al.",",","2017","for","notation",")","for","computing","gradients",".","Args",":","model",":","FairseqModel","to","use","(","passed","via","FairseqCriterion.forward","(",")",")","sample",":","Training","batch","(","passed","via","FairseqCriterion.forward","(",")",")","translations",":","A","[","batch_size",",","beam_size",",","max_trg_len","]","int","tensor","of","generated","translation","(","as","produced","by","generate_translations","(",")",")","Returns",":","A","[","bsz",",","beam_size","]","float","tensor","containing","the","negative","loglikelihoods","of","the","sentences","in","`","translations","`","."]
code: def compute_nll(self, model, sample, translations):
      """Compute negative log-likelihoods for the translations.
  
      This function computes p(u|x) for each element in U(x) (see Edunov et
      al., 2017 for notation) for computing gradients.
  
      Args:
          model: FairseqModel to use (passed via FairseqCriterion.forward())
          sample: Training batch (passed via FairseqCriterion.forward())
          translations: A [batch_size, beam_size, max_trg_len] int tensor of
              generated translation (as produced by generate_translations())
  
      Returns:
          A [bsz, beam_size] float tensor containing the negative loglikelihoods
          of the sentences in `translations`.
      """
      bsz, beam_size, max_trans_len = translations.size()
      total = bsz * beam_size
      translations = translations.view(total, max_trans_len)
      prev_output_tokens = translations.new(total, max_trans_len)
      prev_output_tokens[:, 0] = self.scorer.eos
      prev_output_tokens[:, 1:] = translations[:, :-1]
      src_tokens = (
          sample["net_input"]["src_tokens"]
          .unsqueeze(1)
          .repeat(1, beam_size, 1)
          .view(total, -1)
      )
      src_lengths = (
          sample["net_input"]["src_lengths"]
          .unsqueeze(1)
          .repeat(1, beam_size)
          .view(total)
      )
      all_losses = []
      for f in range(0, total, bsz):
          t = min(f + bsz, total)
          net_output = model(
              src_tokens=src_tokens[f:t],
              src_lengths=src_lengths[f:t],
              prev_output_tokens=prev_output_tokens[f:t],
          )
          lprobs = model.get_normalized_probs(
              net_output, log_probs=True
          )
          nll_loss = -(
              lprobs.gather(
                  dim=-1, index=translations[f:t].unsqueeze(2)
              )
          )
          non_pad_mask = translations[f:t].ne(self.padding_idx).float()
          all_losses.append(
              torch.sum(nll_loss.squeeze(2) * non_pad_mask, dim=1)
          )
      return torch.cat(all_losses).view(bsz, beam_size)
  
imports: import copy
  import math
  import torch
  from fairseq import utils
  from fairseq.criterions import LegacyFairseqCriterion, register_criterion
  from fairseq.scoring import bleu
  from pytorch_translate import generate
label_desc: Use image node_id 5 for calling the BaseSequenceLossCriterion obj's underlying member method code with example usage: obj.compute_nll(model, sample, translations) and returns: torch