docstr: Generates translations of a given source sentence,
     using beam search with noisy channel decoding.
  
  Args:
      combine_method (string, optional): Method to combine direct, LM and
          channel model scores (default: None)
      tgt_dict (~fairseq.data.Dictionary): target dictionary
      src_dict (~fairseq.data.Dictionary): source dictionary
      beam_size (int, optional): beam width (default: 1)
      max_len_a/b (int, optional): generate sequences of maximum length
          ax + b, where x is the source length
      min_len (int, optional): the minimum length of the generated output
          (not including end-of-sentence)
      len_penalty (float, optional): length penalty, where <1.0 favors
          shorter, >1.0 favors longer sentences (default: 1.0)
      unk_penalty (float, optional): unknown word penalty, where <0
          produces more unks, >0 produces fewer (default: 0.0)
      retain_dropout (bool, optional): use dropout when generating
          (default: False)
      temperature (float, optional): temperature, where values
          >1.0 produce more uniform samples and values <1.0 produce
          sharper samples (default: 1.0)
      match_source_len (bool, optional): outputs should match the source
          length (default: False)
      no_repeat_ngram_size (int, optional): Size of n-grams that we avoid
          repeating in the generation (default: 0)
      normalize_scores (bool, optional): normalize scores by the length
          of the output (default: True)
      channel_models (List[~fairseq.models.FairseqModel]): ensemble of models
          translating from the target to the source
      k2 (int, optional): Top K2 candidates to score per beam at each step (default:10)
      ch_weight (int, optional): Weight associated with the channel model score
          assuming that the direct model score has weight 1.0 (default: 1.0)
      channel_scoring_type (str, optional): String specifying how to score
          the channel model (default: 'log_norm')
      top_k_vocab (int, optional): If `channel_scoring_type` is `'src_vocab'` or
          `'src_vocab_batched'`, then this parameter specifies the number of
          most frequent tokens to include in the channel model output vocabulary,
          in addition to the source tokens in the input batch (default: 0)
      lm_models (List[~fairseq.models.FairseqModel]): ensemble of models
          generating text in the target language
      lm_dict (~fairseq.data.Dictionary): LM Model dictionary
      lm_weight (int, optional): Weight associated with the LM model score
          assuming that the direct model score has weight 1.0 (default: 1.0)
      normalize_lm_scores_by_tgt_len (bool, optional): Should we normalize LM scores
          by the target length? By default, we normalize the combination of
          LM and channel model scores by the source length
docstr_tok: ["Generates","translations","of","a","given","source","sentence",",","using","beam","search","with","noisy","channel","decoding",".","Args",":","combine_method","(","string",",","optional",")",":","Method","to","combine","direct",",","LM","and","channel","model","scores","(","default",":","None",")","tgt_dict","(","~fairseq.data.Dictionary",")",":","target","dictionary","src_dict","(","~fairseq.data.Dictionary",")",":","source","dictionary","beam_size","(","int",",","optional",")",":","beam","width","(","default",":","1",")","max_len_a\/b","(","int",",","optional",")",":","generate","sequences","of","maximum","length","ax","+","b",",","where","x","is","the","source","length","min_len","(","int",",","optional",")",":","the","minimum","length","of","the","generated","output","(","not","including","end-of-sentence",")","len_penalty","(","float",",","optional",")",":","length","penalty",",","where","<","1.0","favors","shorter",",",">","1.0","favors","longer","sentences","(","default",":","1.0",")","unk_penalty","(","float",",","optional",")",":","unknown","word","penalty",",","where","<","0","produces","more","unks",",",">","0","produces","fewer","(","default",":","0.0",")","retain_dropout","(","bool",",","optional",")",":","use","dropout","when","generating","(","default",":","False",")","temperature","(","float",",","optional",")",":","temperature",",","where","values",">","1.0","produce","more","uniform","samples","and","values","<","1.0","produce","sharper","samples","(","default",":","1.0",")","match_source_len","(","bool",",","optional",")",":","outputs","should","match","the","source","length","(","default",":","False",")","no_repeat_ngram_size","(","int",",","optional",")",":","Size","of","n-grams","that","we","avoid","repeating","in","the","generation","(","default",":","0",")","normalize_scores","(","bool",",","optional",")",":","normalize","scores","by","the","length","of","the","output","(","default",":","True",")","channel_models","(","List","[","~fairseq.models.FairseqModel","]",")",":","ensemble","of","models","translating","from","the","target","to","the","source","k2","(","int",",","optional",")",":","Top","K2","candidates","to","score","per","beam","at","each","step","(","default:10",")","ch_weight","(","int",",","optional",")",":","Weight","associated","with","the","channel","model","score","assuming","that","the","direct","model","score","has","weight","1.0","(","default",":","1.0",")","channel_scoring_type","(","str",",","optional",")",":","String","specifying","how","to","score","the","channel","model","(","default",":","'log_norm","'",")","top_k_vocab","(","int",",","optional",")",":","If","`","channel_scoring_type","`","is","`","'src_vocab","'","`","or","`","'src_vocab_batched","'","`",",","then","this","parameter","specifies","the","number","of","most","frequent","tokens","to","include","in","the","channel","model","output","vocabulary",",","in","addition","to","the","source","tokens","in","the","input","batch","(","default",":","0",")","lm_models","(","List","[","~fairseq.models.FairseqModel","]",")",":","ensemble","of","models","generating","text","in","the","target","language","lm_dict","(","~fairseq.data.Dictionary",")",":","LM","Model","dictionary","lm_weight","(","int",",","optional",")",":","Weight","associated","with","the","LM","model","score","assuming","that","the","direct","model","score","has","weight","1.0","(","default",":","1.0",")","normalize_lm_scores_by_tgt_len","(","bool",",","optional",")",":","Should","we","normalize","LM","scores","by","the","target","length","?","By","default",",","we","normalize","the","combination","of","LM","and","channel","model","scores","by","the","source","length"]
code: def __init__(
      self,
      combine_method,
      tgt_dict,
      src_dict=None,
      beam_size=1,
      max_len_a=0,
      max_len_b=200,
      min_len=1,
      len_penalty=1.0,
      unk_penalty=0.0,
      retain_dropout=False,
      temperature=1.0,
      match_source_len=False,
      no_repeat_ngram_size=0,
      normalize_scores=True,
      channel_models=None,
      k2=10,
      ch_weight=1.0,
      channel_scoring_type="log_norm",
      top_k_vocab=0,
      lm_models=None,
      lm_dict=None,
      lm_weight=1.0,
      normalize_lm_scores_by_tgt_len=False,
  ):
      """Generates translations of a given source sentence,
         using beam search with noisy channel decoding.
  
      Args:
          combine_method (string, optional): Method to combine direct, LM and
              channel model scores (default: None)
          tgt_dict (~fairseq.data.Dictionary): target dictionary
          src_dict (~fairseq.data.Dictionary): source dictionary
          beam_size (int, optional): beam width (default: 1)
          max_len_a/b (int, optional): generate sequences of maximum length
              ax + b, where x is the source length
          min_len (int, optional): the minimum length of the generated output
              (not including end-of-sentence)
          len_penalty (float, optional): length penalty, where <1.0 favors
              shorter, >1.0 favors longer sentences (default: 1.0)
          unk_penalty (float, optional): unknown word penalty, where <0
              produces more unks, >0 produces fewer (default: 0.0)
          retain_dropout (bool, optional): use dropout when generating
              (default: False)
          temperature (float, optional): temperature, where values
              >1.0 produce more uniform samples and values <1.0 produce
              sharper samples (default: 1.0)
          match_source_len (bool, optional): outputs should match the source
              length (default: False)
          no_repeat_ngram_size (int, optional): Size of n-grams that we avoid
              repeating in the generation (default: 0)
          normalize_scores (bool, optional): normalize scores by the length
              of the output (default: True)
          channel_models (List[~fairseq.models.FairseqModel]): ensemble of models
              translating from the target to the source
          k2 (int, optional): Top K2 candidates to score per beam at each step (default:10)
          ch_weight (int, optional): Weight associated with the channel model score
              assuming that the direct model score has weight 1.0 (default: 1.0)
          channel_scoring_type (str, optional): String specifying how to score
              the channel model (default: 'log_norm')
          top_k_vocab (int, optional): If `channel_scoring_type` is `'src_vocab'` or
              `'src_vocab_batched'`, then this parameter specifies the number of
              most frequent tokens to include in the channel model output vocabulary,
              in addition to the source tokens in the input batch (default: 0)
          lm_models (List[~fairseq.models.FairseqModel]): ensemble of models
              generating text in the target language
          lm_dict (~fairseq.data.Dictionary): LM Model dictionary
          lm_weight (int, optional): Weight associated with the LM model score
              assuming that the direct model score has weight 1.0 (default: 1.0)
          normalize_lm_scores_by_tgt_len (bool, optional): Should we normalize LM scores
              by the target length? By default, we normalize the combination of
              LM and channel model scores by the source length
      """
      self.pad = tgt_dict.pad()
      self.unk = tgt_dict.unk()
      self.eos = tgt_dict.eos()
      self.vocab_size = len(tgt_dict)
      self.beam_size = beam_size
      # the max beam size is the dictionary size - 1, since we never select pad
      self.beam_size = min(beam_size, self.vocab_size - 1)
      self.max_len_a = max_len_a
      self.max_len_b = max_len_b
      self.min_len = min_len
      self.normalize_scores = normalize_scores
      self.len_penalty = len_penalty
      self.unk_penalty = unk_penalty
      self.retain_dropout = retain_dropout
      self.temperature = temperature
      self.match_source_len = match_source_len
      self.no_repeat_ngram_size = no_repeat_ngram_size
      self.channel_models = channel_models
      self.src_dict = src_dict
      self.tgt_dict = tgt_dict
      self.combine_method = combine_method
      self.k2 = k2
      self.ch_weight = ch_weight
      self.channel_scoring_type = channel_scoring_type
      self.top_k_vocab = top_k_vocab
      self.lm_models = lm_models
      self.lm_dict = lm_dict
      self.lm_weight = lm_weight
      self.log_softmax_fn = torch.nn.LogSoftmax(dim=1)
      self.normalize_lm_scores_by_tgt_len = (
          normalize_lm_scores_by_tgt_len
      )
  
      self.share_tgt_dict = self.lm_dict == self.tgt_dict
      self.tgt_to_lm = make_dict2dict(tgt_dict, lm_dict)
  
      self.ch_scoring_bsz = 3072
  
      assert temperature > 0, "--temperature must be greater than 0"
  
      self.search = NoisyChannelBeamSearch(tgt_dict)
  
file_path: fairseq/examples/fast_noisy_channel/noisy_channel_sequence_generator.py
imports: from typing import Dict, List, Optional
  import math
  import numpy
  import torch
  import torch.nn.functional
  from torch import Tensor
  from .noisy_channel_beam_search import NoisyChannelBeamSearch
  from fairseq.sequence_generator import EnsembleModel
label_desc: Use image node_id 1 to create a new NoisyChannelSequenceGenerator object from inherited base classes: object with example: obj = NoisyChannelSequenceGenerator(combine_method, tgt_dict, src_dict, beam_size, max_len_a, max_len_b, min_len, len_penalty, unk_penalty, retain_dropout, temperature, match_source_len, no_repeat_ngram_size, normalize_scores, channel_models, k2, ch_weight, channel_scoring_type, top_k_vocab, lm_models, lm_dict, lm_weight, normalize_lm_scores_by_tgt_len)