class_docstr: 给定一个 :class:`~fastNLP.modules.torch.decoder.Seq2SeqDecoder` ，decode出句子。输入的 decoder 对象需要有 :meth:`decode` 函数，接受的第一个参数为 decode 的到目前位置的所有输出，
  第二个参数为 state 。:class:`SequenceGenerator` 不会对 state 进行任何操作。
  
  :param decoder: Decoder对象
  :param max_length: 生成句子的最大长度, 每句话的 decode 长度为 ``max_length + max_len_a * src_len``
  :param max_len_a: 每句话的 decode 长度为 ``max_length + max_len_a*src_len``。如果不为 0，需要保证 State 中包含 encoder_mask
  :param num_beams: **beam search** 的大小
  :param do_sample: 是否通过采样的方式生成
  :param temperature: 只有在 do_sample 为 ``True`` 才有意义
  :param top_k: 只从 ``top_k`` 中采样
  :param top_p: 只从 ``top_p`` 的 token 中采样（ **nucleus sampling** ）
  :param bos_token_id: 句子开头的 token id
  :param eos_token_id: 句子结束的 token id
  :param repetition_penalty: 多大程度上惩罚重复的 token
  :param length_penalty: 对长度的惩罚，**小于 1** 鼓励长句，**大于 1** 鼓励短句
  :param pad_token_id: 当某句话生成结束之后，之后生成的内容用 ``pad_token_id`` 补充
class_docstr_tok: ["\u7ed9\u5b9a\u4e00\u4e2a",":","class",":","`","~fastNLP.modules.torch.decoder.Seq2SeqDecoder","`","\uff0cdecode\u51fa\u53e5\u5b50\u3002\u8f93\u5165\u7684","decoder","\u5bf9\u8c61\u9700\u8981\u6709",":","meth",":","`","decode","`","\u51fd\u6570\uff0c\u63a5\u53d7\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a","decode","\u7684\u5230\u76ee\u524d\u4f4d\u7f6e\u7684\u6240\u6709\u8f93\u51fa\uff0c","\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a","state","\u3002",":","class",":","`","SequenceGenerator","`","\u4e0d\u4f1a\u5bf9","state","\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002",":","param","decoder",":","Decoder\u5bf9\u8c61",":","param","max_length",":","\u751f\u6210\u53e5\u5b50\u7684\u6700\u5927\u957f\u5ea6",",","\u6bcf\u53e5\u8bdd\u7684","decode","\u957f\u5ea6\u4e3a","``","max_length","+","max_len_a","*","src_len","``",":","param","max_len_a",":","\u6bcf\u53e5\u8bdd\u7684","decode","\u957f\u5ea6\u4e3a","``","max_length","+","max_len_a","*","src_len","``","\u3002\u5982\u679c\u4e0d\u4e3a","0\uff0c\u9700\u8981\u4fdd\u8bc1","State","\u4e2d\u5305\u542b","encoder_mask",":","param","num_beams",":","*","*","beam","search","*","*","\u7684\u5927\u5c0f",":","param","do_sample",":","\u662f\u5426\u901a\u8fc7\u91c7\u6837\u7684\u65b9\u5f0f\u751f\u6210",":","param","temperature",":","\u53ea\u6709\u5728","do_sample","\u4e3a","``","True","``","\u624d\u6709\u610f\u4e49",":","param","top_k",":","\u53ea\u4ece","``","top_k","``","\u4e2d\u91c7\u6837",":","param","top_p",":","\u53ea\u4ece","``","top_p","``","\u7684","token","\u4e2d\u91c7\u6837\uff08","*","*","nucleus","sampling","*","*","\uff09",":","param","bos_token_id",":","\u53e5\u5b50\u5f00\u5934\u7684","token","id",":","param","eos_token_id",":","\u53e5\u5b50\u7ed3\u675f\u7684","token","id",":","param","repetition_penalty",":","\u591a\u5927\u7a0b\u5ea6\u4e0a\u60e9\u7f5a\u91cd\u590d\u7684","token",":","param","length_penalty",":","\u5bf9\u957f\u5ea6\u7684\u60e9\u7f5a\uff0c","*","*","\u5c0f\u4e8e","1","*","*","\u9f13\u52b1\u957f\u53e5\uff0c","*","*","\u5927\u4e8e","1","*","*","\u9f13\u52b1\u77ed\u53e5",":","param","pad_token_id",":","\u5f53\u67d0\u53e5\u8bdd\u751f\u6210\u7ed3\u675f\u4e4b\u540e\uff0c\u4e4b\u540e\u751f\u6210\u7684\u5185\u5bb9\u7528","``","pad_token_id","``","\u8865\u5145"]
code: def __init__(
      self,
      decoder: Seq2SeqDecoder,
      max_length: int = 20,
      max_len_a: float = 0.0,
      num_beams: int = 1,
      do_sample: bool = True,
      temperature: float = 1.0,
      top_k: int = 50,
      top_p: float = 1.0,
      bos_token_id: int = None,
      eos_token_id: int = None,
      repetition_penalty: float = 1,
      length_penalty: float = 1.0,
      pad_token_id: int = 0,
  ):
      if do_sample:
          self.generate_func = partial(
              sample_generate,
              decoder=decoder,
              max_length=max_length,
              max_len_a=max_len_a,
              num_beams=num_beams,
              temperature=temperature,
              top_k=top_k,
              top_p=top_p,
              bos_token_id=bos_token_id,
              eos_token_id=eos_token_id,
              repetition_penalty=repetition_penalty,
              length_penalty=length_penalty,
              pad_token_id=pad_token_id,
          )
      else:
          self.generate_func = partial(
              greedy_generate,
              decoder=decoder,
              max_length=max_length,
              max_len_a=max_len_a,
              num_beams=num_beams,
              bos_token_id=bos_token_id,
              eos_token_id=eos_token_id,
              repetition_penalty=repetition_penalty,
              length_penalty=length_penalty,
              pad_token_id=pad_token_id,
          )
      self.do_sample = do_sample
      self.max_length = max_length
      self.num_beams = num_beams
      self.temperature = temperature
      self.top_k = top_k
      self.top_p = top_p
      self.bos_token_id = bos_token_id
      self.eos_token_id = eos_token_id
      self.repetition_penalty = repetition_penalty
      self.length_penalty = length_penalty
      self.decoder = decoder
  
file_path: fastNLP/fastNLP/modules/torch/generator/seq2seq_generator.py
imports: import torch
  from torch import nn
  import torch.nn.functional
  from ..decoder.seq2seq_decoder import Seq2SeqDecoder, State
  from functools import partial
label_desc: Use image node_id 1 to create a new SequenceGenerator object  with example: obj = SequenceGenerator(decoder, max_length, max_len_a, num_beams, do_sample, temperature, top_k, top_p, bos_token_id, eos_token_id, repetition_penalty, length_penalty, pad_token_id)