class_docstr: This criterion computes losses from input (monolingual data in
  translation) with two components:
  1. Reconstruction loss:
  2. LM loss:
  The total loss is a weighted sum of these two components.
class_docstr_tok: ["This","criterion","computes","losses","from","input","(","monolingual","data","in","translation",")","with","two","components",":","1",".","Reconstruction","loss",":","2",".","LM","loss",":","The","total","loss","is","a","weighted","sum","of","these","two","components","."]
code: def _generate_translation(
      self, model, tgt_dict, sample, beam_size, **kwargs
  ):
      translator_class = beam_decode.SequenceGenerator
      translator = translator_class(
          models=[model], tgt_dict=tgt_dict, **kwargs
      )
      translator.cuda()
      s = utils.move_to_cuda(sample)
  
      # TODO: nbest
      input = s["net_input"]
      srclen = input["src_tokens"].size(1)
      if self.task.use_char_source:
          encoder_input = {
              k: v
              for k, v in input.items()
              if k
              in [
                  "src_tokens",
                  "src_lengths",
                  "char_inds",
                  "word_lengths",
              ]
          }
      else:
          encoder_input = {
              k: v
              for k, v in input.items()
              if k in ["src_tokens", "src_lengths"]
          }
      with torch.no_grad():
          hypos = translator.generate(
              encoder_input=encoder_input,
              beam_size=beam_size,
              maxlen=int(
                  self.args.max_len_a * srclen + self.args.max_len_b
              ),
          )
          for i, id in enumerate(s["id"]):
              # remove padding
              src = utils.strip_pad(
                  input["src_tokens"][i, :], tgt_dict.pad()
              )
              yield id, src, hypos[i]
  
file_path: translate/pytorch_translate/dual_learning/dual_learning_criterion.py
imports: import math
  import torch
  from fairseq import utils
  from fairseq.criterions import LegacyFairseqCriterion, register_criterion
  from fairseq.scoring import bleu
  from pytorch_translate import beam_decode
  from pytorch_translate.data.weighted_data import WeightedLanguagePairDataset
label_desc: Use image node_id 2 for calling the UnsupervisedCriterion obj's underlying member method code with example usage: obj._generate_translation(model, tgt_dict, sample, beam_size) without return types