class_docstr: Risk loss from https://arxiv.org/pdf/1711.04956.pdf.
class_docstr_tok: ["Risk","loss","from","https",":","\/\/arxiv.org\/pdf\/1711.04956.pdf","."]
docstr: Compute the loss for the given sample.
  
  Returns a tuple with three elements:
  1) the loss, as a Variable
  2) the sample size, which is used as the denominator for the gradient
  3) logging outputs to display while training
docstr_tok: ["Compute","the","loss","for","the","given","sample",".","Returns","a","tuple","with","three","elements",":","1",")","the","loss",",","as","a","Variable","2",")","the","sample","size",",","which","is","used","as","the","denominator","for","the","gradient","3",")","logging","outputs","to","display","while","training"]
code: def forward(self, model, sample, reduce=True):
      """Compute the loss for the given sample.
  
      Returns a tuple with three elements:
      1) the loss, as a Variable
      2) the sample size, which is used as the denominator for the gradient
      3) logging outputs to display while training
      """
      translations, bleu_scores = self.generate_translations(
          model, sample
      )
      nll_loss = self.compute_nll(model, sample, translations)
      partition = torch.logsumexp(-nll_loss, 1)
      probs = torch.exp(-nll_loss - partition.unsqueeze(1))
      loss = torch.sum((1 - bleu_scores.cuda() / 100) * probs, dim=1)
      if reduce:
          loss = loss.sum()
      sample_size = (
          sample["target"].size(0)
          if self.args.sentence_avg
          else sample["ntokens"]
      )
      logging_output = {
          "loss": utils.item(loss.data) if reduce else loss.data,
          "ntokens": sample["ntokens"],
          "nsentences": sample["target"].size(0),
          "sample_size": sample_size,
      }
      return loss, sample_size, logging_output
  
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 1 for calling the SequenceRiskCriterion obj's underlying member method code with example usage: obj.forward(model, sample, reduce) and returns: loss,  sample_size,  logging_output